I've been recently looking into an CSS layout that will display a single centered column with fixed-width (min-width, expandable preferably) that occupied the whole height (minus header and footer).
Any suggestions for this? I have tried several approaches posted here on so but none meets my criteria. Also, I do not want to use JS for this, so it has to be pure CSS.
I'm no expert so I don't know which approach to take:
three columns with each side columns margin minus half of center column width together with a faux center column to stretch to 100% height? I somewhat dislike this idea because my side columns will have no content whatsoever
single column with margin 0 auto 0 auto to center it and top: xx px to make room for header? Then how do I stretch it to 100% height?
Any help highly appreciated.
Cheers, chross
Update
Simple way to do it for modern browsers (2015) using
display:flex
:The above allows for both fixed height header and footer (just add a height to the styles) as well as variable height (as shown currently - can change depending on the content of header and footer) with the content taking up the rest of the space.
If the content is longer than the document, the footer will be pushed down.
Old post:
There are a few ways to do this with pure css. Basically you need to start off with the html structure like this:
Version 1 uses border-box so won't be compatible with older browsers (and you may need to add the moz, webkit and ms prefixes to get it working across all browsers):
Version 1
Version 1 with content
Version 1 centred column
Version 2 uses absolute positioning and is a bit more cross browser friendly:
Version 2
Version 2 with content
Version 2 centred column
Version 3 changes the html slightly but is more robust for if you have variable height header and footer:
Css
Version 3
Version 3 with different height header and footer
Version 3 with content
Version 3 centred column
If you want to do it through jQuery You can use Something Like this
This Script will put height equals the div height,
Is it helpful to you, or You are trying to Ask something else ?
You can make use of absolute positioning.
Have an absolutely positioned container with
top
andbottom
values equal to the height of header and footer respectively, this will stretch the container to remaining heightHave an
inline-block
child inside having100%
heightApply
text-align:center
for the parent to align theinline-block
child to centerHTML
CSS
JSFiddle Demo
Or if browser compatibility is not an issue, you can use
css3 calc()
function as another answer pointed outHm I am very surprised that anybody doesnt know how to solve it with pure CSS and good browser support (without any calc () - it is good method but it is really early to use it)
HTML
CSS
So I created Fiddle
The only way to do this with pure css is using the css calc() function:
Where 250px is the height of your header+footer combined.