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
Use the flex
declaration - see here: http://www.w3schools.com/cssref/css3_pr_flex.asp
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;
}
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/
You can use flexboxes, just appy these proprieties to the container of divs:
.container {
display: flex;
justify-content: space-between;
}
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>
You can use flexboxes, this solustion is for IE 10+ and latest other browsers. https://css-tricks.com/snippets/css/a-guide-to-flexbox/