English 中文(简体)
Why is my "container" div not containing my floated elements?
原标题:

I m creating a website for my schools "Math Relay" competition.

I have a "Container" div (with a white background), then a top-bar, left-bar, and right-bar div inside the container.

left-bar and right-bar are both floated inside "Container".

However, if you look in the image below you can see the right-bar has the grey background showing beneath it. If "Container" is truly containing both top, left and right bar then it should be the containers background which shows through and the bottom should all be at a uniform level with a white color.

Instead, it seems that container isn t fully containing the left & right bar and therefore the actual body background shows through on the bottom of the right bar.

alt text

Here is my CSS:

#container {
    margin: 0 auto;
    width: 750px;
    background-color: #ffffff; }

#top-panel {
    background-color: #000000;
    text-align: left;
    width: 100%;
    height: 88px;
    float: left; }

#left-panel {
     clear: left;
     text-align: center;
     background-color: #ffffff;
     border-right: 1px dashed #000000;
     float: left;
     width: 250; }

#right-panel {
    background-color: #ffffff;
    float: left;
    width: 499; }

How can I make the "container" truly contain the divs inside it so the grey background will not show up underneath my right-panel and create my uneven level at the bottom?

最佳回答

Both of your panels are floated and therefore are taken out of the normal flow of the page. To make the container encapsulate them you have to clear the float. The way I do is is make class clear for br:

.clear {
    clear:both;
    line-height:0;
}

This way it takes up no space and is a clear. Then put it under the two divs in the container div as such

<br class="clear" />

Should work!

问题回答
overflow: auto 

should fix it.

overflow:hidden; height:100%; <-- for ie

On the container will do it.

There is a css only method to deal with the problem called clearfix. It s best to use it as a class you apply selectively to the divs you want:

.clearfix {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}

For your specific markup, the following css will do:

#left-panel:after,
#right-panel:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}

Reference http://www.positioniseverything.net/easyclearing.html

Nothing is forcing the containment, you can specify a clear on the last element to force the conatinment. An common hack is to add an additional blocking element with clear:both ie: <div style="clear:both"></div>





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签