W3C Web Standards - Second Component: CSS2
(Page 5 of 7 )
If you're not familiar with CSS level 2, and are hoping that this article will make you a guru, I'm sorry to disappoint you! I will point you to W3C's site for CSS2 (http://www.w3.org/TR/REC-CSS2/) if you want a truly in-depth understanding. But honestly, we just need a few basic concepts to work with here.
You can either use one of your old web pages/templates you may have from a previous project, or you may just wish to start from scratch. For me, I figured that upgrading my site to the new standards was the perfect time to just give it a fresh overhaul. Although, there is a huge advantage in working with an existing page, in that you will get to see with your own eyes that the end result in about 40-60% of the size of the original. Of course, you could just take my word for it!
Basically, let's go with the assumption that all the pages in our site will be based on one template, just to make this tutorial easier. That means that every page will have the aforementioned DTD and XML declaration at the very top of the page. We are going to lay out all of the elements in the page, and then uniquely identify each. Then, within our global style sheet, we will describe the appearance of the elements, and it will all come together in a wonderfully magical way.
Let's assume that we have a few basic elements: a logo, a navigation bar, a title, one main area of content, and a footer. Now I know you've already started to plan out your table, your row and column spans, and a few nested tables, but stop! Our HTML code... oops, sorry, our XHTML code could be as simple as this:
<?xml version="1.0" encoding="UTF-8"? >
<!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>my standards-compliant page</title>
</head>
<body>
<div id="logo"><img src="/img/logo.gif" /></div>
<div id="nav">Home | Contact me</div>
<h1 id="header">Home Page</h1>
<div id="content" align="center">content</div>
<div id="footer">© 2004 Acme</div>
</body>
</html>
The first thing we want to do is assign a default font for our site. Black Chancery might be a bit out there, so let's just stick with our good ol' Arial for now, shall we? And we'll set a few other page attributes while we're at it. So the first few lines of our styles.css could be:
body
{
font-family: Arial, sans-serif;
font-size: 12px;
color: #000000;
background-color: #FFFFFF;
margin: 0px;
padding: 0px;
}
Seeing as our cascading style sheet will be cached on the client's browser, these few lines will save us from typing a million <font> tags, or even typing anything whatsoever within our body tags! You could even configure a background image if you're planning to use one. Now let's see what we can do to configure the individual areas of the pages. We'll target each specifically by using the '#' prefix.
#logo {
float:left;
padding: 5px 20px 10px 10px;
}
#nav {
text-align: right;
font-weight: bold;
font-size: 13px;
background-image:url("/img/navBox.gif") no-repeat top;
}
#header {
font-size: 15px;
font-weight:bold;
margin: 5px;
}
#content {
width: 400px; /* more readable paragraphs */
border: 1px solid #666666;
padding: 10px;
text-align: left;
}
#footer {
color:#333333;
background-color:#CCCCCC;
text-align: center;
font-size: 10px;
}
Next: Code Explanation >>
More HTML Articles
More By Justin Cook