Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapper div height is 0 with floated elements inside

I have a wrapper <div> which contains two inner <div>s that are floating.

<div class="outside" style="border:1px solid #555;">
  <div class="inside" style="float:left; width:40px;">CONTENT</div>
  <div class="inside2" style="float:left; width:40px;">CONTENT</div>
</div>

The problem is the wrapper <div> has width of 80px since two inner <div> are 40px each. But always the wrapper <div> is 0px in height which makes the border appear like a line at the top.

So I placed the two inner <div>s inside a <table>. It worked well. But how do I avoid this problem without going for a <table>?

like image 811
Sarvap Praharanayuthan Avatar asked May 23 '12 16:05

Sarvap Praharanayuthan


People also ask

Why does my div have 0 height?

It has zero height because the content inside it is floated. Floated elements are ignored when calculating the height of their parent. This can be easily solved. Setting its overflow property to "hidden" will force it to wrap around floated elements.

Can you float a div?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

Can a div have a height?

This seems like the ideal candidate for transition from a table-based layout to a CSS layout. It makes sense: both DIVs and tables can be nested, have HEIGHT and WIDTH attributes set, contain borders, etc.

What does div id wrapper mean?

A wrapper is an element, commonly a div, that encloses one or more other. elements in the HTML markup, e.g.: <div id="wrap"> <h1>Headline</h1> <p>Paragraph</p>


1 Answers

Set overflow: hidden on the parent.

<div class="outside" style="border:1px solid #555;overflow:hidden;">
  <div class="inside" style="float:left; width:40px;">CONTENT</div>
  <div class="inside2" style="float:left; width:40px;">CONTENT</div>
</div>
like image 51
Ana Avatar answered Sep 19 '22 14:09

Ana