开发者

4 Column Div Layout

I am trying to create a 4 column <div> layout.

Why are the ro开发者_StackOverflow社区w containers not drawing a border around the respective row? Also, is this a good approach, as in is my css written well to be fluid and for dynamic resizing of the browser window?

Any suggestions or help would be most appreciated.

Here is my current attempt.


You need to set the overflow to auto when using float. http://jsfiddle.net/gJJHs/


The problem seems to be that you are floating your columns, and when you float things, they take up effectively zero space.

I think the solution is to cancel the float in you "last" class and add a "dummy column" to each row.

This CSS seems to work:

.col
{
    float: left;
    width: 25%;
}

.last{
    clear: left;
}

.row{
    border: 1px solid green;
}

Revised HTML (with dummy last column):

<div class="row">
    <div class="col">1</div>
    <div class="col">2</div>
    <div class="col">3</div>
    <div class="col">4</div>
    <div class="last" />
</div>

<div class="row">
    <div class="col">5</div>
    <div class="col">6</div>
    <div class="col">7</div>
    <div class="col">8</div>
    <div class="last" />
</div>


When an element is floated, its parent no longer contains it because the float is removed from the flow. The floated element is out of the natural flow, so all block elements will render as if the floated element is not even there, so a parent container will not fully expand to hold the floated child element.

As such, the border will seem like it is not bordering anything :( Take a look at the following article to get a better idea of how the CSS Float property works:

The Mystery Of The CSS Float Property

As others have said, if you add overflow: auto; to your .row class, it'll take care of the problem. Here's another article that explains why to use overflow.

http://www.quirksmode.org/css/clearing.html

I hope this helps.
Hristo


it's the float left. That takes the divs "out of flow" and it's drawing the border around empty space essentially


Yet another option, in addition to the other answers, is to add overflow: hidden; to your .row.

The reason for the behavior you saw is that float takes the div outside of the normal flow. The div then essentially takes up no space in the document.

This makes sense if you think about the ostensible purpose of floating an image in order to wrap text around it. The next p tag (for example) is positioned as if the floated image wasn't there, i.e. overlapping the image. Then, the browser wraps the text within the 'p' tag around the image. (If the floated image was not "removed from the flow", the p tag would naturally appear below the image—not giving the desired effect.)

Here's how I'd write the code.

HTML:

<div class="row">
    <div class="col">1</div>
    <div class="col">2</div>
    <div class="col">3</div>
    <div class="col">4</div>
</div>
<div class="row">
    <div class="col">5</div>
    <div class="col">6</div>
    <div class="col">7</div>
    <div class="last">8</div>
</div>

CSS:

.col
{
    float: left;
    width: 25%;
}
.row{
    border: 1px solid green;
    overflow: hidden;  /* "overflow: auto;" works just as well instead */
    width:100%;        /* Helps older versions of IE */
}


Add a "float:none;clear:both" to your .row and you'll see the rows appropriately. But for the fluid behavior and design that you are looking for, you'll want to apply some javascript (like jQuery Equal Height: http://www.jainaewen.com/files/javascript/jquery/equal-height-columns/) to be consistent across browsers without a ton of CSS hacking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜