All of the widths and margins come out to a total of 99.96%, so it should fit just fine in the 100%. I understand that borders add width, so I've done box-sizing: border-box; but that doesn't seem to fix it. If I float: left then it works okay in Chrome, but I feel like that's a crappy workaround anyway and doesn't actually fix the problem.
Why doesn't it all fit on one line?
Here's my jsfiddle.
HTML
<div id="container">
<a href="#">
1
</a>
<a href="#">
2
</a>
<a href="#">
3
</a>
<a href="#">
4
</a>
</div>
CSS
body {
border: 1px solid black;
background-color: #BCCDDC;
margin: 5% 25%
}
#container a {
font-family: 'Open Sans', sans-serif;
text-align: center;
box-sizing: border-box;
-webkit-box-sizing: border-box;
border: 2px solid #fff;
display: inline-block;
width: 15%;
margin: 0 6.66%;
padding: 20px 0;
}
#container a:first-of-type {
margin-left: 0 !important;;
}
#container a:last-of-type {
margin-right: 0 !important;
}
inline-block elements aren't floated; they're inline, which means white space between elements is preserved.
You can fix this by removing the whitespace entirely between <a> tags: http://jsfiddle.net/mblase75/y1Ltq1fc/4/
<a href="#">
1
</a><a href="#">
2
</a><a href="#">
3
</a><a href="#">
4
</a>
Alternatively, make the anchors float: left and put overflow: auto on the parent #container: http://jsfiddle.net/mblase75/y1Ltq1fc/9/
#container {
overflow: auto;
}
#container a {
float: left; /* automatically sets display:block */
/* etc. */
}
Here are some other techniques, if you have a problem implementing the above.
display: inline-block; honors whitespace between elements, to fix it you can either:
Remove the whitespace between the elements in the HTML
<div id="container">
<a href="#">1</a><a href="#">2</a><a href="#">3</a><a href="#">4</a>
</div>
http://jsfiddle.net/1sgjh5c3/
Make the font-size zero on the container in CSS
#container {
font-size: 0;
}
#container a {
font-size: 12px;
}
http://jsfiddle.net/jLhr8xcn/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With