How do I automatically add spacing between divs without using percentage?

前端 未结 7 1338
别跟我提以往
别跟我提以往 2021-01-22 19:25

I have a few divs aligned horizontally. How do I make the spacing between them automatic so that if I resize the screen or add another div, there will be equal spacing between d

相关标签:
7条回答
  • 2021-01-22 19:56

    Use the flex declaration - see here: http://www.w3schools.com/cssref/css3_pr_flex.asp

    0 讨论(0)
  • 2021-01-22 19:58

    Flexbox can do that https://jsfiddle.net/2Lzo9vfc/210/

    HTML

    <div class="content">
      <div class="box">Box</div>
      <div class="box">Box</div>
      <div class="box">Box</div>
    </div>
    

    CSS

    .content {
        display: -webkit-flex;
        display: flex;
        -webkit-justify-content: space-around; 
        justify-content: space-around;
        width: 100%;
    }
    
    .box {
        background: black;
        padding: 25px;
        color: white;
    }
    
    0 讨论(0)
  • 2021-01-22 20:07

    Here you can find a solution with flexbox:

    .container {
        display:flex;
        justify-content:space-between;
    }
    .item {
        background:#000;
        height:50px;
        width:120px;
    }
    <div class="container">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>

    More information about using flexbox you can find here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    0 讨论(0)
  • 2021-01-22 20:17

    You can use flexboxes, just appy these proprieties to the container of divs:

    .container {
      display: flex; 
      justify-content: space-between;
    }
    
    0 讨论(0)
  • 2021-01-22 20:17

    You may use inline-block + text-align:justify; for older browser generating an extra last invisible line with :after, or flex + justify-content:space-betwween;

    .ib {
    text-align:justify;
      }
    .ib:after {
      content:'';
      display:inline-block;
      width:99%;
      }
    .flex {
      display:flex;
      justify-content:space-between;
      }
    .d100 {
      width:100px;
      height:2em;
      background:blue;
      display:inline-block;
      }
    <div class="ib">
      <div class="d100"></div>
      <div class="d100"></div>
      <div class="d100"></div>
      </div>
    <div class="flex">
      <div class="d100"></div>
      <div class="d100"></div>
      <div class="d100"></div>
      </div>

    0 讨论(0)
  • 2021-01-22 20:18

    You can use flexboxes, this solustion is for IE 10+ and latest other browsers. https://css-tricks.com/snippets/css/a-guide-to-flexbox/

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