How to Make the Content 100% height and equal height columns in this layout?
Asked Answered
C

4

2

I have this layout where the my_menu is fixed at the top, followed by header, followed by content with 960px and centered, in which 3 columns have been floated left and finally followed by a sticky footer.

  1. The problem is How to make the content stretch to the 100% height even if there is not enough content in any one of the child columns?

  2. How make the 3 columns equal height regardless of any column's content?

  3. Maintain the sticky footer! Pure CSS, No JavaScript, No JQuery!

The HTML Markup

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>Document Title</title> 
<link rel="stylesheet" href="main.css" type="text/css" /> 
</head> 
<body id="index"> 
<div id="wrapper"> 
    <div id="my_menu"> 
    FIXED MENU WIDTH 100% 
    </div> 
    <div id="my_header"> 
    HEADER WIDTH 100% 
    </div> 
    <div id="content"> 
        <p>CONTENT 960px</p> 
        <div id="col1" class="content_columns"> 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        </div> 
        <div id="col2" class="content_columns"> 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        </div> 
        <div id="col3" class="content_columns"> 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        </div> 
        <div class="clear_floats"></div> <!-- For Clearing Floats --> 
    </div> 
    <div class="push"></div> <!-- For Sticky Footer --> 
</div> 
    <div id="my_footer"> 
    STICKY FOOTER WIDTH 100% 
    </div> 
</body> 
</html> 

The CSS Styles

* /* For CSS Reset */ 
{ 
padding: 0; 
margin: 0; 
} 

html, body 
{ 
width: 100%; 
height: 100%; 
} 

div#wrapper 
{ 
width: 100%; 
height: 100%; 
min-height: 100%; /* For Sticky Footer */ 
height: auto !important; /* For Sticky Footer */ 
margin: 0 auto -70px; /* For Sticky Footer */ 
} 

div#my_menu 
{ 
width: 100%; 
height: 50px; 
outline: 1px solid black; 
background-color: grey; 
text-align: center; 
position: fixed; 
} 

div#my_header 
{ 
width: 100%; 
height: 100px; 
outline: 1px solid black; 
background-color: yellow; 
text-align: center; 
padding-top: 50px; 
} 

div#content 
{ 
width: 960px; 
margin: 0 auto; 
outline: 1px solid black; 
background-color: brown; 
text-align: center; 
} 

div.content_columns 
{ 
width: 320px; 
outline: 1px solid black; 
background-color: gold; 
text-align: center; 
float: left; 
} 

div.clear_floats /* For Clearing Floats */ 
{ 
clear: both; 
} 

div#my_footer 
{ 
width: 100%; 
height: 70px; 
outline: 1px solid black; 
background-color: pink; 
text-align: center; 
} 

div.push /* For Sticky Footer */ 
{ 
height: 70px; 
} 
Cheyennecheyne answered 6/2, 2012 at 10:53 Comment(2)
I also remmbered it can be acheived easily with tables, but tables for layout? This one is for Ktash!Cheyennecheyne
Why did you offer 3x500 bounties on seemingly random questions?Archetype
F
3

A solution with full IE6+ support. I'll pull out and explain the relevant bits of code.

Modern support

div.content_columns {
    width: 320px;
    outline: 1px solid black;
    background-color: gold;
    text-align: center;
    display: table-cell; /* No floats, this instead */
}

So, since modern browsers make this task easy, all we need to do is use display: table-cell to get this to work. It makes the columns equal height, and acts as a table-cell. Easy peasy lemon squeezy.

IE 6 & 7 support

<!--[if lte IE 7]>
<style>
    div#content {
        overflow: hidden;
    }
    div.content_columns {
        vertical-align: top;
        display: inline;
        zoom:1;
        padding-bottom: 9999px;
        margin-bottom: -9999px;
    }
</style>
<![endif]-->

Now, for IE 6 and 7 support we're going to use a few tricks. First off, it should be noted that this doesn't have to be done in a conditional comment, but I like it better that way. Cleaner to me. But you can use CSS hacks to get these values to only work in IE.

What we're doing is getting IE 6 & 7 to treat each column as an inline-block element, but since they don't support that (for block level elements at least) we use inline display, and a zoom: 1; fix to trigger IE's hasLayout property. This will treat it like it is an inline-block elmenent. Then, we set them all to be aligned at the top with each other, and use another little trick. We use the padding: 9999px to extend the bottom of the element so far that the other elements are not likely to be longer than it, and we use margin: -9999px; to not change the rendering of the page when we do so. We don't want to extend the page, just the background. 9999px is arbitrary and can be any value as long as it is high enough to be greater than the difference between the longest and shortest columns. And for the final touches, we set overflow: hidden on the container element so that the backgrounds don't extend the page by bleeding out the bottom.

And there you have it, full IE 6+ support for multicolumn fixed width layout using pure CSS 2 (plus MS zoom).

Footrace answered 6/2, 2012 at 18:41 Comment(2)
HaHa, Returing to tables even if it's display: table-cell; HeHe. And I was hoping to have a long heated debate. LoL. Nevertheless, Great one Mate!Cheyennecheyne
Heh, this is not using tables for display, it is simulating the display style of a table. It's about the semantics of the page :)Footrace
S
1

bfrohs came up with a solution that works in none-IE browsers: Full height columns without scroll

@Jawad - Thanks again for your time and your help! :)

Suppositious answered 7/2, 2012 at 8:39 Comment(0)
C
0

AFAIK this isn't possible with pure CSS without using grid layout or flexbox which are both not very much supported in the browsers. (Flexbox is at least already w3c working draft...)

You can work around this by applying min-height, which has several drawback, or set the height via js. I don't know any reasonable crossbrowser css-solution atm.

Congratulation answered 6/2, 2012 at 11:0 Comment(6)
I am very sure this is possible with pure CSS. I did it before, but for the life of me, I can't remmber how I did it before. The only difference is the sticky footer which I suspect is causing the problem. I stand to be corrected though.Cheyennecheyne
I dealt with this problem earlier several times, and I'm pretty sure, there is no css-solution without setting fixed heights to the divs. But I'd be more than glad to get corrected^^Congratulation
You mean I give a fixed height to the div#content as well as div.content_columns ?Cheyennecheyne
I already tried that but does not see to work beacuse of the sticky footer - positioniseverything.net/articles/onetruelayout/equalheightCheyennecheyne
Depending on how "sticky" you need your footer, you could apply a position absolute/fixed to your footer to resolce this?Congratulation
Adding position absolute/fixed will although solve the problem, but what happens if the content overflows the footer? Thay will overlap!Cheyennecheyne
S
0

You could always try the ol' faux column technique. That is, the element that contains your three divs has a background image that tiles vertically. that image would have lines for your column divisions, and would create the illusion of equal heights.

It's clunky in terms of maintainability - you have to edit the image to make changes to the layout - but it is reliable.

Sansom answered 6/2, 2012 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.