Draw a static line between two divs

后端 未结 3 895
南旧
南旧 2020-12-10 19:01

I have three divs on the same line and want to connect them with a line:

Unfortunately, every way I tried collided with the method of display, e.g. inline-blo

相关标签:
3条回答
  • 2020-12-10 19:35

    You could add a div with the width of your margin:

    <div class="boxItem">1</div>
    <div class="emptyDiv"></div>
    <div class="boxItem">2</div>
    <div class="emptyDiv"></div>
    <div class="boxItem">3</div>
    

    CSS:

    div {
      display: inline-block;
    }
    
    div.emptyDiv{
     border: 1px solid black;
     width:25em;
    }
    
    div.boxItem { 
      border: 1px solid black;
      padding: 1em;
    }
    
    0 讨论(0)
  • 2020-12-10 19:49

    if it stands on 1 line, you could add pseudo element and filter first and last box, to draw or not a line aside.

    div.boxItem { 
      display: inline-block;
      border: 1px solid black;
      padding: 1em;  
      margin-right: 5em;
      position:relative
    }
    
    .boxItem:before,
    .boxItem:after
    {
      content:'';
      width:5em;/* size of your margin */
      border-bottom:1px solid;
      position:absolute;
      top:50%;
    
    }
    :after {
      left:100%;
    }
    :before {
      right:100%;
    }
    .boxItem:first-of-type:before,
    .boxItem:last-of-type:after {
      display:none;
    }
    .myBox {
      white-space:nowrap; 
    /* */ text-align:center;
      }
    body {
    
    }
    <div class="myBox">
      <div class="boxItem">1</div>
      <div class="boxItem">2</div>
      <div class="boxItem">3</div>
      <div class="boxItem">4</div>
    </div>
    <div class="myBox">
      <div class="boxItem">1</div>
      <div class="boxItem">2</div>
      <div class="boxItem">3</div>
    </div>
    <div class="myBox">
      <div class="boxItem">1</div>
      <div class="boxItem">2</div>
    </div>
    <div class="myBox">
      <div class="boxItem">1</div>
    </div>

    fork of your pen

    0 讨论(0)
  • 2020-12-10 20:02

    Try this:

    div.boxItem { 
      display: inline-block;
      border: 1px solid black;
      padding: 1em;
    }
    
    div.line {
      display: inline-block;
      border-top: 1px solid black;
      width: 2em;
    }
    <div class="boxItem">1</div><!--
    --><div class="line"></div><!--
    --><div class="boxItem">2</div><!--
    --><div class="line"></div><!--
    --><div class="boxItem">3</div>

    Note: I used <!-- and --> to comment out the white space ensuring the line actually touches the divs. More info on that bit: https://stackoverflow.com/a/19038859/2037924

    EDIT: Same in CodePen, for the case you like that more for some reason: http://codepen.io/anon/pen/wBPPRz

    0 讨论(0)
提交回复
热议问题