Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three Column DIV layout dynamics; left = fixed, center = fluid, right = fluid

Tags:

html

css

layout

The best solution I found so far was:

http://jsfiddle.net/kizu/UUzE9/

But that wasn't quite it. You see, I have three columns; two of which need to avoid being explicitly sized. Well, the second one does need to be sized; but not quite.

Allow me to clarify by establishing the conditions I've been trying to meet.

  • All three columns have fixed height: 65px, to be precise.
  • The first column's width is set to 285px.
  • The center column has no size defined; it simply takes up whatever space is left.
  • The right column will size itself to whatever content is in there. There is no size explicitly set, it simply resizes based on content, leaving the center column to take up whatever space is left.
  • There is no whitespace above, below, and in between each column.

The end result would roughly look something like this:

   Logo     |            Player          |    Name     
-----------------------------------------------------

For tables, I'd have simply done this:

<table width="100%" height="65px" cellspacing=0 cellpadding=0>
    <tr>
        <td width="285px" height="65px">
            Logo
        </td>
        <td height="65px">
            Player
        </td>
        <td width="1px" height="65px">
            Account
        </td>
    </tr>
</table>

Result of above table code: http://jsfiddle.net/zMNYb/

But I'm trying to move away from using tables and using a DIV-based layout. Any ideas?

like image 734
Don Cullen Avatar asked Jul 09 '12 05:07

Don Cullen


People also ask

How to create 3 column layout using CSS Div?

All you need to do is divide the div section which you want as 3 columns as follows Please go through How to create 3 column layout using css div to know more about this. Show activity on this post. You can adjust width % as required.

How do I make a column in a Div flexible?

If the right has a float (and a width), and if the left column doesn't have a width and no float, it will be flexible :) Also apply an overflow: hidden and some height (can be auto) to the outer div, so that it surrounds both inner divs.

How do I set the width of the center column?

The first column's width is set to 285px. The center column has no size defined; it simply takes up whatever space is left. The right column will size itself to whatever content is in there. There is no size explicitly set, it simply resizes based on content, leaving the center column to take up whatever space is left.

What is the difference between the center column and right column?

The center column has no size defined; it simply takes up whatever space is left. The right column will size itself to whatever content is in there. There is no size explicitly set, it simply resizes based on content, leaving the center column to take up whatever space is left.


1 Answers

You can do this by using float:left for first column, float:right for the last column and making the center column float:none

Updated Demo: http://jsfiddle.net/L85rp/3/

HTML

<div class="col1">Column1</div>
<div class="col3">Column3</div>
<div class="col2">Column2</div>

CSS

.col1 {
    background-color: #ddf;
    float: left;
}
.col2 {
    background-color: #dfd;
    float: none;
}
.col3 {
    background-color: #fdd;
    float: right;
}

NOTE: It is necessary to place your right column before your center column in HTML (see above, col3 comes before col2 in the HTML) to make sure that when the browser renders the center column, it knows how to render correctly around the existing floating elements.


Updated CSS

.col1 {
    background-color: #ddf;
    float: left;
    width: 285px;
    height: 65px;
}
.col2 {
    background-color: green;
    float: none;
    height: 65px;
    overflow: hidden;
    display: table-cell; /* turn this off to lock height at 65px */
}
.col3 {
    background-color: cyan;
    float: right;
    height: 65px;
}

Updated demo: http://jsfiddle.net/ew65G/1/

like image 119
techfoobar Avatar answered Oct 01 '22 17:10

techfoobar