
How To Build a Basic CSS Layout
Designing without tables by using CSS layouts is fast becoming the new
standard on the Web because of the benefits of CSS mentioned in my
previous article. Web browsers used these days are now able to render web
pages more proficiently. In this article I will endeavor to create a basic
2 column CSS layout which you can use for future design projects.
1. Divide your page into sections - the <div id>
tags allows you to create distinct divisions on your web page. They are
identified with a unique id. You can then add a style (css selector) that
specifically applies to the div of that id.
Remember to include the DOCTYPE (to render your page accurately in
browsers) and include meta tags in the header of your document to enable
search engines to spider your pages.
-wrapper: is the div that wraps around all the other divs like a container
for the page elements.
-header: defines the top banner of the page
-main: defines the main content of the page
-nav: defines the navigation of the page
-footer: defines the footer and sub-navigation of the page
<!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">
<html>
<head>
<title>How to Build a Basic CSS Layout</title>
<meta name="Description" content="How to Build a Basic
CSS Layout" />
<meta name="Keywords" content="css layout" />
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<div id="wrapper">
<div id="header">
<h1>How To Build a Basic CSS Layout</h1>
</div>
<div id="nav">
<h2>Navigation</h2>
</div>
<div id="main"></div>
<div id="footer"></div>
</div>
</body>
</html>
2. Create the CSS code - below is the CSS code that
styles the page as a centered 2 column CSS layout with a navigation bar and
a footer. The div#wrapper style creates the centered box which acts as a
container for the rest of the page content. The width: 80% rule sets the
width of the div. The background-color:#FFFFFF rule creates a white
background for the div. The margin-top: 50px and margin-bottom: 50px rules
create a space of 50 pixels for the top and bottom margins for the div
itself.
The proper way to center a block-level element with CSS is to set
margin-left: auto and margin-right: auto. This instructs the browser to
automatically calculate equal margins for both sides, thus centering the
div. The border: thin solid #000000 rule adds a border around the outer
div. The rest of the CSS code styles the divs for the header, footer, nav,
and main content.
The div#header and div#footer styles set margins and padding for those
divs. In addition, div#header includes the text-align: center rule to
center the header text, and div#footer includes the border-top: thin solid
#000000 rule to create a border along the top edge of the div to replace
the horizontal rule above the footer in the table-based layout.
The div#nav and div#main styles create the two columns in the middle of
the centered box. In the div#nav style, the float: left rule pushes the div
to the left side of its parent element (the wrapper div), and the width:
25% rule sets the div's width to 25 percent of the parent element. With
the nav div floated to the left and limited to a set width, it leaves room
for the main div to move up to the right of the nav div, thus creating the
two-column effect. The div#main style includes the margin-left: 30% rule to
keep the main text aligned in a neat column instead of spreading out below
the nav column. The main div's left margin is set to a value slightly
larger than the width of the nav div.
<style type="text/css">
<!--
body {
background-color: #999999;
font-size:12px;
font-family:Verdana, Arial, Helvetica, sans-serif;
}
div#wrapper {
width: 80%;
background-color:#FFFFFF;
margin-top: 50px;
margin-bottom: 50px;
margin-left: auto;
margin-right: auto;
padding: 0px;
border: thin solid #000000;
}
div#header {
padding: 15px;
margin: 0px;
text-align: center;
}
div#nav {
width: 25%;
padding: 10px;
margin-top: 1px;
float: left;
}
div#main {
margin-left: 30%;
margin-top: 1px;
padding: 10px;
}
div#footer {
padding: 15px;
margin: 0px;
border-top: thin solid #000000;
}
-->
</style>
3. Create the side navigation menu - to build the left
side navigation I use the normal CSS code for the different links ie
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
color: #FF0000;
}
a:active {
text-decoration: none;
}
If links are placed elsewhere on the page they will inherit the same
properties as above..a blue link that hovers to red.
What if you wish to create another set of different colored links that
change color and are underlined when your mouse passes over them?
4. Create the bottom navigation - to include the
navigation in the <div id="footer">
section of the page, I use div#footer and code each link accordingly. To
make the list go horizontally I use: display: inline;
div#footer ul li{
color : #000000;
background-color : transparent;
display: inline;
}
div#footer ul li a{
color : #115EAC;
background-color : transparent;
text-decoration : none;
}
div#footer ul li a:hover{
text-decoration : underline;
}
-->
Now that I have finished creating my style sheet I want to shorten the
code on page by linking it to my external style sheet. Here's how:
5. Create an external style sheet - copy and paste all
the css code (without these tags: <style
type="text/css"><!-- --></style>) into notepad and
name it something like "style sheet". Place this style sheet
between the head tags of your web page like this:
<head>
<link rel="stylesheet" href="stylesheet.css"
type="text/css" />
</head>
This will reduce the code on your page so it will load fast plus the
search engines can more easily spider your web page.
6. Add content to your page - after you have got your
page looking correctly, you can add more content to it. Adjustments can
easily be made to any style on the page (or your whole site) by simply
editing one style sheet.
7. Upload your files - be sure to upload your web pages
and style sheet to the root directory of your server.
8. Validate your code - be sure to validate your xhtml
code: http://validator.w3.org/ and css code:
http://jigsaw.w3.org/css-validator/ and make corrections where
necessary.
9. Check browser compatibility and screen resolution -
check that your page renders well in the popular browsers (IE6, NN7,
Firefox)
If you are beginning with CSS layouts, implement then slowly by making
small changes to your pages i.e. creating a style sheet for your main
headers and fonts only. As you become more familiar with CSS you may
eventually build all your future sites with CSS layouts.
|
This page was last modified on 16 Sep 2010 at 21:17:20. |
Vikash Singh FreeLancer w3schools.in |
![]() |
» CSS Text
» CSS Padding
» CSS Margin
» CSS Border
» Styling tables with CSS
» How To Build a Basic CSS Layout
» How to create css forms
» Tableless Web Page Design Using CSS

