I have some HTML that looks like this:
<div class="TheContainer">
<div class="TheData">this is some text inline-block with clear</div>
<div class="TheData">this is some other text inline-block but not clearing</div>
</div>
The CSS looks like this:
.TheContainer{
margin:20px 20px;
background:red;}
.TheData{
display:inline-block;
clear:both;
background:yellow;
padding:5px 5px;
margin:10px 10px;}
I'm using inline-block
so that the TheData
divs wrap nicely around their content instead of extending the total width of TheContainer
. I'm also using clear:both
so that these TheData divs get stacked one above the other.
However, it seems that clear:both
doesn't apply when elements are set to inline-block
. The JSFiddle here demonstrates this.
How do I use inline-block
AND make the divs stack vertically?
Thanks.
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.
Compared to display: inline , the major difference is that inline-block allows to set a width and height on the element. Also, with display: inline , top and bottom margins & paddings are not respected, and with display: inline-block they are.
The most common and traditional way (inline-block) The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.
The problem of aligning 2 divs horizontally: Since <div> tag is a block element, only one div can be placed in a line, and all the space to the left and right is taken by it.
clear
is only for clearing float
To get the effect you want, put float:left
on .TheData
. You will most probably also want an element below those div.TheData
s with clear
but no float
to make sure the container renders with the correct height.
Updated fiddle
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