HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
How to achieve 100% height (tutorial)
Post new topic   Reply to topic    HTML Help Forum Index -> Member Tutorials
View previous topic :: View next topic  
Author Message
sticks464



Joined: 31 Dec 2006
Posts: 2308

PostPosted: Mon Dec 08, 2008 6:03 am     How to achieve 100% height (tutorial) Reply with quote

A lot of people want 100% height but most times they want more than 100% height!
What they usually want is an element such as a navbar that will stretch 100% down the side of the page and keep stretching even when the document is longer than the page.

The easiest way to accomplish this is to use a small bg image for the column colour and repeat it down the y-axis of the body on the left hand side. In this way the column appears to extend forever and is a simple solution that works in nearly all browsers.

Code:
body {
padding:0;
margin:0;
background:#ffc0cb  url(leftcolbg.jpg) repeat-y left top;
color: #000000;
}


The above image is 150px wide and 5px high. It is repeated on the left side of the body and will form a column that is as long as the viewport or as long as the document if greater than viewport. A border can be added to the repeated gif to give the illusion of a continuous border. You could also add a screen fade effect for a more interesting column colour.

The background is tiled quite quickly and the image is small enough not to make much difference to page weight.

Another way to achieve 100% height without background images is to use the following techniques. (This doesn’t work for ie5 mac and a few other minor browsers.)

The first thing we need is 100% height to work with and this must be declared in the html element and the body element to satisfy various browsers. While we’re there we will set padding and margins to zero so they don’t influence our design.

We also need to hide this from ie5 mac users so use the commented backslash hack.

Code:
/* commented backslash hack \*/
html, body{height:100%;}
/* end hack */
html,body {margin:0;padding:0}


The next important step is to create a whole page wrapper that will hold everything else on the page. This outer element is set to 100% height for IE browsers and 100% min-height for mozilla browsers. IE6 and under treat height as min-height anyway but mozilla needs to be specific.(IE7 now implements min-height and the correct version of height so treat it like firefox when using this method.)

We also then need to set mozilla to height auto so that it doesn’t remain at 100% only but will expand as required. This height auto needs to be hidden from <=IE6 and is the only real hack in this code. (The star (universal) selector hack is good for passing values to only <=IE6 browsers.)

Code:
#outer{min-height:100%;background:#ffffcc}
* html #outer{height:100%;}/* ie6 and under*/


Here is the code for the whole page:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
/* commented backslash hack \*/
html, body{height:100%;}
/* end hack */
html,body {margin:0;padding:0}
#outer{
min-height:100%;
height:auto;
background:#ffffcc;
width:200px;
}
* html #outer{height:100%;}
</style>
</head>
<body>
<div id="outer">
<p>content goes here</p>
</div>
</body>
</html>


While the code achieves 100% height and allows for the element to expand it does have some drawbacks.

The container element (#outer) is the only element that will inherit the 100% properly so you have to keep everything inside this element. The element will only expand when content inside it expands it past the bottom of the viewport. A lot of people expect it to expand automatically to the document length or the length of longer elements on the same page. When you think about it this can’t really happen unless all elements are wrapped inside this 100% element.

As mentioned above you can only have one of these elements on your page because inner elements will not inherit from the 100% min-height and default to height auto. (Of course IE6 and under may in some circumstances apply 100% height on inner elements but it is not guaranteed and not advisable because it does not work in other browsers.)
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> Member Tutorials All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
DARFUR
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial