Reputation: 145
I have the following CSS
code written;
#container {
width: 1300px;
background-color:green;
margin:0 auto;
overflow:hidden;
}
#menu {
float:left;
width:20%;
background-color: yellow;
}
After searching google for a long time I couldn't find an explaination why the container background color is disappearing when the container overflow
attribute is visible.
Can someone help me understand why ?
Thanks alot for your answers.... :)
I don't mind using overflow:hidden, ijust want to understand its purpose and how to use it.
As i unserstand, the overflow property specifies what happens if content overflows an element's box, so i dont understand why would its visibilty make the container background color disappear or why would it change the container height.
Upvotes: 2
Views: 3381
Reputation: 5577
This is because of the floating child element. If your container only contains floated elements, its height will be equal to zero.
You need to include a clear element, different possibilities exists:
<div style="clear: both;"></div>
as latest child element.overflow: hidden
on the container elementThe Easy Clearing Method: By adding extra CSS and a class on the parent element (clearfix'
)
.clearfix:after { content: "."; visibility: hidden; display: block; height: 0; clear: both; }
Upvotes: 2
Reputation: 125433
Since the elements within the container are have float:left
- the container had a height of 0 - which is also what is causing you not to see any background.
In order to fix this there are a few solutions out there:
One of them is called clearfix
<div id="container" class="clearfix">
<!-- floated elements here -->
</div>
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
Another is by setting overflow:hidden
on the container element - this establishes a new block formatting context - which in effect clears the floats. (See this post)
From the spec:
Floats, absolutely positioned elements, block containers (such as inline-blocks, table-cells, and table-captions) that are not block boxes, and block boxes with 'overflow' other than 'visible' (except when that value has been propagated to the viewport) establish new block formatting contexts for their contents.
In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block. The vertical distance between two sibling boxes is determined by the 'margin' properties. Vertical margins between adjacent block-level boxes in a block formatting context collapse.
In a block formatting context, each box's left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). This is true even in the presence of floats (although a box's line boxes may shrink due to the floats), unless the box establishes a new block formatting context (in which case the box itself may become narrower due to the floats).
Upvotes: 5
Reputation: 690
You can set the height of the container div to be equal with the height of the menu. This way you don't need the overflow: hidden setting.
$("#container").height($("#menu").height());
Demo here: http://jsfiddle.net/er144/ZV6pb/
Upvotes: 0
Reputation: 15699
It is happening because you have not given any height to #menu
.
As, #container
has height of #menu
, background is not visible.
Give some height to it.
#menu {
float:left;
width:20%;
background-color: yellow;
height:50px;
}
Upvotes: 0