Starting Good CSS Habits: Part 1
I am often surprised by how much bad HTML is out there. Especially HTML that does not use Cascading Style Sheets yet. CSS has been around for over 10 years now! It was added to HTML in 1997 and well-supported (at least reasonably) in web browsers by 2000. I think the reason many people never upgraded is that they either learned HTML before 2000, they learned it from sources (friends, coworkers, internet, legacy code) from before CSS, or they learned just enough to get a site online and never bothered learning the “right way”.
Legacy sites are not always worth the trouble to update—I know I have plenty of old code still floating around out there—so their flaws are understandable. But some folks are carrying around their bad HTML habits still. If you ask them to code a page they probably know that frames should not be used, so they use a table to layout the page instead. They might use <center> when they want text to center. I see bad code and misused tags all the time. You can “view-source” on many major websites and see bad code by people who should know better.
If that sounds like you or someone you know, keep reading, I will try to help you break your bad habits and start good ones.
“Good CSS habits” can mean a lot of things. But at its most basic, it means writing clean, clear, syntactic HTML pages and keeping all style information in external stylesheets. It will take me a few posts, but I want to walk you through all the steps to go from bad HTML to good XHTML/CSS. Walking through each step will give me a chance to show the code while giving you a process you can use to make these changes painlessly.
Here is the HTML we will improve. It contains many of the most common mistakes. You can copy and paste the HTML into a text document to work along with me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <html> <head> <title>My Site</title> </head> <body> <table cellPadding="10" cellSpacing="0" border="0" bgcolor="#FFFFFF"> <tr> <td height="100" colspan="2" bgcolor="#1166FF" valign="middle"> <center> <font size="6" color="#FFFFFF" face="Helvetica"> My Site </font> </center> </td> </tr> <tr> <td width="150" valign="top"> <br><br> <b><font size="4">Menu</font></b><br> <a href="">Homepage</a><br> <a href="">Next Page</a><br> <a href="">Last Page</a><br> </td> <td height="500" width="750" valign="top"> <br> <h1> <font color="#1166FF" face="Helvetica"> Homepage </font> </h1> <p><i>Lorem ipsum dolor sit amet,</i> consectetur adipiscing elit. Pellentesque ullamcorper est id tortor. Praesent fermentum vehicula tellus. Duis sem sem, hendrerit eu, porta at, dapibus quis, urna. Aenean non dui eu purus elementum blandit. Duis eu urna. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus sit amet quam non erat vehicula condimentum. In eu lacus et sapien vulputate lobortis. Aenean elit. Etiam tristique elit sed turpis. Sed pharetra. Aenean ornare laoreet urna. Integer tincidunt. Nunc condimentum ultricies sem. Aenean imperdiet, elit sollicitudin blandit convallis, orci ligula porta diam, et ultrices elit leo at ipsum. <p>Praesent accumsan sodales orci. Ut sit amet diam. Mauris nec augue sed lectus aliquet cursus. Nulla quam. Quisque non risus. Morbi rutrum odio vitae nulla. Integer mi. In ullamcorper fringilla eros. Cras tristique luctus tellus. Vivamus rhoncus. Nulla varius pretium magna. Mauris aliquam, turpis nec condimentum lacinia, tellus leo eleifend ante, at aliquet nisi ipsum nec dui. Curabitur erat sapien, iaculis fermentum, suscipit non, pulvinar non, nisl. Fusce mi. Sed tincidunt nisi. Curabitur sit amet tortor ac urna congue lacinia. In auctor tincidunt augue. In hac habitasse platea dictumst. <p>In magna. Praesent euismod ligula ac mi. Morbi condimentum, enim sit amet consequat blandit, mi nibh tincidunt turpis, eu pulvinar sem nulla quis lorem. In id eros. Aliquam quis orci. Praesent nec lacus. Phasellus dictum orci et urna. Pellentesque ac diam. Vivamus tincidunt. Fusce condimentum risus eu turpis volutpat viverra. Nulla facilisi. Donec varius neque a eros. Curabitur semper feugiat leo. Curabitur tellus tellus, convallis laoreet, malesuada nec, accumsan ac, felis. In at lorem. </td> </tr> <tr> <td height="50" colspan="2" valign="top" align="center"> <hr> © 2009 My Site, Inc. </td> </tr> </table> </body> </html> |
We will start by improving the HTML, then look at CSS techniques later.
Using <p> Tags Correctly
There are many things wrong with this document, but I would say the worst is the incorrect use of <p> tags. <p> is a paragraph tag and it should used around paragraphs of text.
In our page’s menu, it is being used to create a double-line return. Many people mistakenly use <p> this way, as if <br> means one line return and <p> means two.
19 20 21 22 23 | <b><font size="4">Menu</font></b> <p> <a href="">Homepage</a><br> <a href="">Next Page</a><br> <a href="">Last Page</a><br> |
For now, replace it two <br> tags instead. We will learn an even better way, but at least it actually means “line break”.
19 20 21 22 23 | <b><font size="4">Menu</font></b> <br><br> <a href="">Homepage</a><br> <a href="">Next Page</a><br> <a href="">Last Page</a><br> |
Our page uses <p> tags to correctly denote the three paragraphs, but it does not have a closing </p> tag. All tags in HTML that get opened should also be closed.
31 32 33 34 35 36 37 38 39 40 41 42 43 | <p><i>Lorem ipsum dolor sit amet,</i> consectetur adipiscing ... paragraph body omitted ... orci ligula porta diam, et ultrices elit leo at ipsum.</p> <p>Praesent accumsan sodales orci. Ut sit amet diam. Mauris ... paragraph body omitted ... lacinia. In auctor tincidunt augue. In hac habitasse platea dictumst.</p> <p>In magna. Praesent euismod ligula ac mi. Morbi ... paragraph body omitted ... convallis laoreet, malesuada nec, accumsan ac, felis. In at lorem.</p> |
The appearance did not change, but now it is very clear where each paragraph starts and ends.
These changes are both steps towards our ultimate goal, semantic markup. There are two principles we will follow to get semantic markup:
- HTML should correctly label the content elements on the page according to their use or purpose. In this case, a paragraph of content is accurately being marked as being a paragraph.
- HTML should not be used to indicate how content should look. That will become the job of CSS. In our example, we were using a paragraph tag just for the visual effect it gave, even when there was no paragraph in sight.
We will come back to these two principles often.
Self-Closing <br> and <hr> Tags
We now know that: All tags in HTML that get opened should also be closed. So what should we do about our <br> and <hr> tags? Even though it may seem like a stand-alone element, we are opening a tag without closing it.
Using <br></br> is no good—it gives you another line break. Using <hr></hr> works but requires a lot of needless typing.
The correct answer is that these two tags are “self-closing” and you should always write them like <br /> and <hr />. A simple find and replace can help you change them all quickly.
18 19 20 21 22 23 | <br /><br /> <b><font size="4">Menu</font></b> <br /><br /> <a href="">Homepage</a><br /> <a href="">Next Page</a><br /> <a href="">Last Page</a><br /> |
69 70 71 72 | <td height="50" colspan="2" valign="top" align="center"> <hr /> © 2009 My Site, Inc. </td> |
Bold and Italic vs. Strong and Emphasis
Many people still use <bold>, <b> <italic> and <i> to indicate the text they want to be bold and italic. It seems like common sense. What is the problem?
Our goal is fully semantic mark up. What we really mean by “bold” is that we want the text to be “stronger than normal”. In other words, “strong” describes the element’s semantic usage, while “bold” describes its appearance. In some cases, “strong” might be a bolder font weight, but in other cases, it might mean text that is larger, in a different color, with a different background or in a different font-face.
So instead of <bold> and <b> tags, use <strong> tags. By default, <strong> elements will have a bold style. We can use CSS to change their behavior if we decide on a different visual presentation, and our HTML and its semantic meaning will not change. The same is true for <italic> and <i>: use <em> (short for emphasis) instead.
19 | <strong><font size="4">Menu</font></strong> |
31 | <p><em>Lorem ipsum dolor sit amet,</em> consectetur adipiscing |
In fact, bold- and italic-tags are no longer part of the latest version of HTML. All browsers still support them and probably will forever, but other applications (search engines, web site analysis tools, IDEs, Dreamweaver, etc.) might not someday. Better to get rid of them now and start a good habit.
Lists
In keeping with the idea of correctly describing the content in our HTML document, let’s take a look at our menu. We have a series of links, indented, with line breaks. It is a list of the pages available for site navigation. A list is a single entity made up of multiple entities. Our original HTML is just one entity after another and there is no single HTML entity to “hold them together”, to indicate that they belong to the same group. It would be useful if we could address (via CSS or JavaScript) both the individuals members of our list and the list as a whole. For example, imagine a JavaScript that would tell all our menu links to appear or disappear.
Well, if it is a list, then why not mark it up as being a list? HTML gives us <ol> (ordered list) and <ul> (unordered list) for that purpose. Since we do not want our list numbered, we should use an unordered list. In our example, we can also remove the two <br /> tags right before the links because the list has a default top margin.
19 20 21 22 23 24 | <strong><font size="4">Menu</font></strong> <ul> <li><a href="">Homepage</a></li> <li><a href="">Next Page</a></li> <li><a href="">Last Page</a></li> </ul> |
Now it is clear that it is a list of links, our code is much cleaner, and we could use CSS or JavaScript to address the entire list.
You probably noticed we introduced a style change. The default styling for a list is to put a bullet in front of each list item. Even though we are focusing on HTML-only improvements right now, we can sneak in a quick inline-style to correct it.
19 20 21 | <ul style="list-style-type: none; padding-left: 10px;"> ... </ul> |
Clearer Character Entity References
Before we move on to style improvements, let’s look at how to make character entity references clearer. Our document has a character entity reference in the footer (©) which outputs a copyright symbol. While there is nothing technically wrong with using the numeric notation, it is a bit opaque. If you had HTML with both a trademark symbol and a registered trademark symbol, it is hard to tell at a glance which is which.
Each numeric version also has a named version which is easier to remember and easier to read. The meaning of © and ™ and ® are instantly clear.
71 | © 2009 My Site, Inc. |
I won’t list all the entities here, but you can go to Google or Wikipedia and look for “character entity references” to find a complete list.
Next, we will improve our page by starting to use CSS. More soon…

April 2nd, 2009 at 6:33 pm
This is so clear and useful - thank you! I love your code tutorials, they’re the best I’ve come across.
September 8th, 2009 at 12:51 am
Excellent! very clear and concise. I plan on enrolling in the monthly Lynda.com training program in order to study the essentials of MySQL and PHP from you. Thanks a billion
Percy
September 13th, 2009 at 2:43 am
Kevin,
Your training is the best. I haven’t found a better trainer on lynda or anywhere else. Keep up the good fight buddy. I look forward to another “Series By Kevin” Maybe some Advanced Series.