http://www.webmasterworld.com Welcome site search, glossary, subscribe, help, library, PubConference, conference, recent posts Search
Engine
World
Home / Forums Index / Browser Side World / CSS
Forum Library, Charter, Moderators: SuzyUK & DrDoc

CSS

  
CSS 100% Height DIV's tutorial
Here is how to assign 100% height for your CSS divs!
papabaer
Senior Member
 
view user profile
joined-Dec 31, 2001
posts:1238
msg #:1
 6:49 pm on June 13, 2002 (utc 0)
Several members here at Webmaster World have asked if there was a method to assign 100% height to <div>'s used as left and/or right navigation menus. In some cases, members also wished to assign background-images to their div's that would always display at 100% of the resized browser window.

Most attempts to accomplish this were made by assigning the property and value: div{height:100%} - this alone will not work. The reason is that without a parent defined height, the div{height:100%;} has nothing to factor 100% percent of, and will default to a value of div{height:auto;} - auto is an "as needed value" which is governed by the actual content, so that the div{height:100%} will a=only extend as far as the content demands.

The solution to the problem is found by assigning a height value to the parent container, in this case, the body element. Writing your body stlye to include height 100% supplies the needed value.

body {
margin:0;
padding:0;
height:100%;
}

Now when height:100%; is applied to divs (or other elements) contained withing the body, the height percentage has a containing (body) value to work with.

The following example is a three column, liquid layout with 100% height assigned to both flanking divs. Background images can be assigned to these divs and will render at 100% of the window height.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>One Hundred Percent Height Divs</title>
<style type="text/css" media="screen">
body {
margin:0;
padding:0;
height:100%; /* this is the key! */
}
#left {
position:absolute;
left:0;
top:0;
padding:0;
width:200px;
height:100%; /* works only if parent container is assigned a height value */
color:#333;
background:#eaeaea;
border:1px solid #333;
}
.content {
margin-left:220px;
margin-right:220px;
margin-bottom:20px;
color:#333;
background:#ffc;
border:1px solid #333;
padding:0 10px;
}
#right {
position:absolute;
right:0;
top:0;
padding:0;
width:200px;
height:100%; /* works only if parent container is assigned a height value */
color:#333;
background:#eaeaea;
border:1px solid #333;
}

#left p {
padding:0 10px;
}
#right p {
padding:0 10px;
}
p.top {
margin-top:20px;
}
</style>
</head>

<body>
<div id="left">
<p class="top">This design uses a defined body height of 100% which allows setting the
contained left and right divs at 100% height.</p>

<p>This design uses a defined body height of 100% which allows setting the contained left and
right divs at 100% height.</p>

<p>This design uses a defined body height of 100% which allows setting the contained left and
right divs at 100% height.</p>
</div>

<div class="content">
<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>

<div class="content">
<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>

<div class="content">
<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>

<div id="right">
<p class="top">To solve an inheritance issue displayed in div #right as rendered in Opera, class p.top
using margin-top:20; is applied to the first paragraph of each outer divs.</p>

<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>

<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>
</body>
</html>

Tiling backgrounds may also be deployed:

#right {
position:absolute;
right:0;
top:0;
padding:0;
width:200px;
height:100%; /* works only parent container is assigned a height value */
color:#333;
background:url(images/mytile.gif) repeat;
border:1px solid #333;
}

The above three column layout will not display properly in Netscape 4.x because of the position:absolute; right:0; styles of the div #right. A three column liquid layout was used for demonstration purposes, the 100% height can be applied as required.

Remember to protect Netscape 4 from styles it cannot understand by using the @import rule.

The above has been tested in Opera6, IE6, and Mozilla 1.1 - it should work in most modern browsers.

Have fun!

XHTML + CSS + WebStandards = FREEDOM!

- papabaer ;)

Nick_W
Senior Member
 
view user profile
joined:Feb 4, 2002
posts:5025
msg #:2
 7:34 pm on June 13, 2002 (utc 0)
Hey! Fantastic, cheers papa bear. I'll go test it in IE5 in a moment.

You know that if you put that xml declaration under the Doctype it will still validate and won't trigger IE6's 'quirks' mode?

The design may not suffer from being in 'quirks' mode but it's a point worth noting ;)

I never knew how to do this, I've never needed too but it's a lot simpler than I would have thought, great demo...

Nick

madcat
Senior Member
 
view user profile
joined:Mar 15, 2002
posts:887
msg #:3
 7:46 pm on June 13, 2002 (utc 0)
I second that...Thanks Papabaer!
Nick_W
Senior Member
 
view user profile
joined:Feb 4, 2002
posts:5025
msg #:4
 7:57 pm on June 13, 2002 (utc 0)
Yep, works okay in IE5 (but not konqueror) and I've got a passable workaround for that naughty little NS4 fella....

dump all the styles in a sheet but change the #right div to this:


#right {
float: right;
position: absolute;
top: 0;
padding:0;
width:200px;
height:100%; /* works only if parent container is assigned a height value */
color:#333;
background:#eaeaea;
border:1px solid #333;
}

then @import the original #right and everybody's happy ;)

Nick

Purple Martin
Senior Member
 
view user profile
joined:Jan 21, 2002
posts:899
msg #:5
 12:19 am on June 14, 2002 (utc 0)
Thanks, that's great! Another gem for my little code library :)
papabaer
Senior Member
 
view user profile
joined-Dec 31, 2001
posts:1238
msg #:6
 2:43 am on June 14, 2002 (utc 0)
There are lots of ways to beat the IE5.5 BOX-MODEL error (and IE6 in quirk mode).

http://www.w3.org/TR/REC-CSS1#formatting-model

The above example shows that no padding or margins have been applied to the fixed-width right & left divs; the 1px border is negligible. Padding (and margins) have been applied to the <p> contained within the divs, where they will adjust to the width automatically.

The center .content div's are liquid and also will adjust for padding without conflict.

Nick, nice tip regarding the xml declaration and IE's quirk mode! :)

- papabaer

rcjordan
Senior Member
 
view user profile
joined:Apr 22, 2000
posts:9138
msg #:7
 2:55 am on June 14, 2002 (utc 0)
Now if you could just get this 3-column model to handle a header and footer div that spanned all 3 columns....
DrDoc
Moderator
 
view user profile
joined:Mar 15, 2002
posts:3987
msg #:8
 3:02 am on June 14, 2002 (utc 0)
Woohoo! Fankies dad ;)
LOL

Nick_W
Senior Member
 
view user profile
joined:Feb 4, 2002
posts:5025
msg #:9
 7:28 am on June 14, 2002 (utc 0)
Well, heres a passable version with a header, had trouble with the footer but I really must do some work this morning ;)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>One Hundred Percent Height Divs</title>
<style type="text/css" media="screen">
body {
margin:0;
padding:0;
height:100%; /* this is the key! */
}
#header {
height: 50px;
background-color: #EAEAEA;
border:1px solid #333;
padding:4px;
}
#left {
position:absolute;
left:0;
top:80px;
padding:0;
width:200px;
height:60%; /* works only if parent container is assigned a height value */
color:#333;
background:#eaeaea;
border:1px solid #333;
}
.content {
position: relative;
top: 30px;
margin-left:220px;
margin-right:220px;
margin-bottom:20px;
color:#333;
background:#ffc;
border:1px solid #333;
padding:0 10px;
}
#right {
position:absolute;
right:0;
top: 80px;
padding:0;
width:200px;
height:60%; /* works only if parent container is assigned a height value */
color:#333;
background:#eaeaea;
border:1px solid #333;
}

#left p {
padding:0 10px;
}
#right p {
padding:0 10px;
}
p.top {
margin-top:20px;
}
</style>
</head>

<body>
<div id="header">
<p>Here is the header: 50px high, no positioning.</p>
</div>

<div id="left">
<p class="top">This design uses a defined body height of 100% which allows setting the
contained left and right divs at 100% height.</p>

</div>

<div class="content">
<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>

<div class="content">
<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>

<div class="content">

<p>This design uses a defined body height which of 100% allows setting the contained left and
right divs at 100% height.</p>
</div>

<div id="right">
<p class="top">To solve an inheritance issue displayed in div #right as rendered in Opera, class p.top
using margin-top:20; is applied to the first paragraph of each outer divs.</p>
</div>
</body>
</html>

Nick

Read Messages:
This Forum
:
Newer: Benefits of XHTML and CSS Older: CSS layout problems
Global: Older:
 

In association with Search Engine World
Terms of service ¦ Privacy Policy ¦ Report Problem ¦ About
All trademarks and copyrights held by respective owners.
Member comments are owned by the poster.
BestBBS v3.15 (c) WebmasterWorld.com 1998-2004 all rights reserved

Email Newsletter: