Beginner CSS Layout
February 5th, 2007 | Design & Development |Firstly, if you don't know what CSS is then I recommend you go to W3Schools and have a look at their simple tutorials. They do a very good job of explaining a lot of the simple stuff.
When you're ready to abandon tables for layouts and embrace CSS then you here is where you want to be.
CSS has loads of little pitfalls and things you end up finding out on your own that you wish someone had told you about. I'm going to try and address some of those in my posts on CSS. First however the basics. I know you can find tutorials like this all over the web, it's just a basic layout. It's important however that you get the simple stuff right before you venture on any further.
You can copy the source for the html and css files below or download the zip here.
When you understand everything that's going on here, have a look at my more advanced CSS tutorial.
I've commented the css as much as possible, if you have any trouble please let me know.
The HTML
-
<!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">
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>CSS Example</title>
-
<link href="style.css" rel="stylesheet" type="text/css" />
-
</head>
-
<div id="border">
-
<div id="header">header</div>
-
<div id="left">left</div>
-
<div id="center">center</div>
-
<div id="right">right</div>
-
<div id="footer">footer</div>
-
</div>
-
</body>
-
</html>
The CSS
-
body {
-
background-color: #e5e5e5;
-
color: #FFFFFF;
-
font-family: Verdana, Arial, Helvetica, sans-serif;
-
-
font-size: 0.7em;
-
/*I'm using em here because it defines the base font size for my site.
-
all other font sizes will be defined relative to this.
-
i.e a h2 might be defined as twice the size of normal body text so that would be 1.4em
-
*/
-
-
line-height: 1.4em;
-
/*this sets double line spacing (0.7 * 2 = 1.4)*/
-
-
margin-top: 0px;
-
/* Some browsers give the body a default top margin of about 10px, if you
-
want to get rid of that, set it to 0*/
-
}
-
-
/*this style is very important when you start to float blocks around your layout.
-
A clear element is not visible in your site, it is just used to ensure that blocks
-
containing floating objects expand to encapsulate them properly.
-
*/
-
.clear {
-
margin: 0px;
-
padding: 0px;
-
clear: both;
-
float: none;
-
}
-
-
/*now to set up the elements*/
-
#border{
-
width:780px;
-
/*
-
Our site is going to have a fixed with of 780px.
-
When setting heights and widths don't forget the "px" or they wont work.
-
*/
-
-
margin-left:auto;
-
margin-right:auto;
-
/*setting the left and right margin to auto will force the browser to centre the border block*/
-
-
background-color:#FFFFFF;
-
}
-
#header{
-
height:120px;
-
background-color:#6699CC;
-
}
-
#left{
-
width:420px;
-
-
height:500px;
-
/*
-
i'm setting the height just for testing purposes.
-
the height of this element would normally be set
-
by the content within.
-
it is possible to set the min-height for an element
-
but it is not supported by Internet Explorer
-
*/
-
-
background-color:#336699;
-
-
float:left;
-
/*
-
A float tells an element to move as far left or right as possible until
-
its boundaries meet the sides of the element that contains it, or another
-
floating element.
-
This block is going to float to the far left until it meets the sides
-
of the border element which keeps it from going any further.
-
*/
-
clear:left;
-
/* this makes sure nothing else can float to the left of the element */
-
}
-
#center{
-
width:180px;
-
-
height:500px;
-
/*
-
i'm setting the height just for testing purposes.
-
the height of this element would normally be set
-
by the content within.
-
it is possible to set the min-height for an element
-
but it is not supported by Internet Explorer
-
*/
-
-
background-color:#3366bb;
-
-
float:left;
-
clear:none;
-
-
}
-
#right{
-
width:180px;
-
-
height:500px;
-
/*
-
i'm setting the height just for testing purposes.
-
the height of this element would normally be set
-
by the content within.
-
it is possible to set the min-height for an element
-
but it is not supported by Internet Explorer
-
*/
-
-
float:right;
-
clear:right;
-
background-color:#3366dd;
-
}
-
#footer {
-
height: 60px;
-
background-color: #CCCCCC;
-
color: #333333;
-
/*
-
the following float and clear make the footer block shift to below
-
the previous floating elements.
-
*/
-
clear: both;
-
float: none;
-
}










February 5th, 2007 at 5:27 pm
[...] Beginner CSS Layout [...]