Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combining display:inline-block with clear:both not vertically stacking divs

Tags:

html

css

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.

like image 660
frenchie Avatar asked Dec 11 '12 01:12

frenchie


People also ask

What is the difference between display inline and display inline-block?

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.

What's one advantage to using inline-block over inline display?

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.

How do you make two block elements inline?

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.

Can we place two div blocks at same line?

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.


1 Answers

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.TheDatas with clear but no float to make sure the container renders with the correct height.

Updated fiddle

like image 150
TwiNight Avatar answered Oct 27 '22 21:10

TwiNight