I have my HTML like this
<div id="wrapper">
<div id="main">
<p>test</p>
</div>
<div id="sidebar">
<p>test</p>
</div>
</div>
And CSS
#wrapper {
width: 960px;
margin: 0px auto;
}
#main {
width: 790px;
display: inline-block;
padding: 0px;
margin: 0px;
}
#sidebar {
width: 170px;
display: inline-block;
vertical-align: top;
padding: 0px;
margin: 0px;
}
Example: http://jsfiddle.net/Hpwff/
The problem is that even though the sum of both divs is 960px, which is the same width as the parent container's (#wrapper), they do not float next to each other. I have to shrink either the sidebar or main containers width back by 4px so they fit. Why is this, and is there a way around it?
CSS Layout - display: inline-block. ❮ Previous Next ❯. Compared to display: inline, the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block, the top and bottom margins/paddings are respected, but with display: inline they are not.
With the flexbox styles applied, we can have three inline divs of equal width – just as we can with the inline-block solutions. One of the great things about CSS is that there is usually more than one way to solve a problem. Hopefully some of the suggestions above will help you with your future inline element spacing issues.
We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes. Let us understand this with the help of an example: Attention reader! Don’t stop learning now.
CSS display: inline-block: why it rocks, and why it sucks - Robert's talkRobert's talk – Web development and Internet trends Usually when you want a horizontal list, you need to use float in the CSS code to make it work, with all its drawbacks.
You have a newline between the two div
s; since they are inline-block
, the newline between them is rendered as a space. Without space it works as you expect.
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