Introduction to The CSS Detective Guide
You sit looking at the screen, trying to understand why your code is giving you the visual equivalent of gibberish instead of the clear visual diction of your original design. Criminal CSS and browser rendering have gotten the best of you again, but for the last time. You are ready to start your training with the CSS Detective.
In Chapter 1, “Investigating the Scene of the Crime,” you’ll learn how to go over the evidence in the code, discovering what you’re looking at and what you’re looking for.
In Chapter 2, “The Tools of the Trade,” you’ll learn techniques and tips that will go a long way toward preventing coding crimes before they happen.
In Chapter 3, “Giving the Third Degree,” we will cover methods of isolating suspicious rules and lines of questioning techniques to get your CSS to ’fess up as to where the rendering problems are coming from.
By Chapter 4, you’ll be ready to see the lineup of “The Usual Suspects”: common bugs and problems that almost everybody who wrangles CSS has had the misfortune of encountering face-to-face.
Rushing into a crime scene too hastily can cause us to miss picking up important pieces of evidence, so we’ll start your apprenticeship by going over HTML best practices: document structure, good semantics, and tag structure. From there, we’ll move on to CSS, including rule structure, getting the styles into your documents, and commenting.
We’ll also take a good look at the clues posed by inheritance, the cascade, and the vast array of selectors you can employ to target the desired elements in your HTML document.
HTML Document Structure
As you know, HTML (Hypertext Markup Language) is the basis for all things web. And you also probably know that HTML has different version numbers, and that there is a character on the block known as XHTML. Without going into lengthy detail about the version histories and differences, I’ll cut to the chase: HTML 4.01 is the latest version of HTML. The W3C (World Wide Web Consortium) is working on a draft of HTML 5.0, which is slated to be released “soon.” XHTML was created to be an “extensible” version of HTML, which means that it conforms to the XML syntax and can be made modular (divided into usable components).
To learn more about HTML, see http://www.w3.org/TR/html4/.
The main difference between the two forms of markup is that XHTML by definition needs to be well formed; therefore, all elements need to be in lowercase, all elements need to be closed, and attributes are case-sensitive. By contrast, HTML, technically, does not need to be all in lowercase, empty elements do not have to be closed, nor are the attributes case-sensitive. However, just because the specification says you can be loosey-goosey about those items doesn’t mean you should be. To conform to best practices and industry standards, you should create consistent, well-formed, semantically correct documents.
Here are the underpinnings of a “well-formed” HTML document in a nutshell:
- All elements are closed.
- All tags are in lowercase.
- All attributes values are enclosed by quotes.
- All elements are properly nested.
A basic HTML 4.01 document using the strict doctype definition looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>HTML 4.01 Strict Document</title> </head> <body> </body> </html>
A basic XHTML 1.0 document using the transitional doctype definition looks like this:
<!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> XHTML 1.0 Transitional Document</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> </body> </html>
HTML TAG STRUCTURE
HTML tags can be distilled into this syntax:
The tag always has a tag name, may have an attribute, and when there is an attribute, the best practice is to always give the attribute a value.
Keep this syntax in mind for later; being able to recognize patterns like this one makes it easy to detect when tags fall outside the pattern.
Here’s a little quiz for you. What’s wrong with the HTML tag below?
<p class, highlight>Hunting for clues</p>
I know you caught it: the attribute and value were in the wrong format. Rather, it should be like this:
<p class="highlight">Clues found!</p>
The CSS pattern is analogous, which you will soon see. With both HTML and CSS, once you have the patterns down, you’ll be able to recognize them and know when a tag or a style declaration has gone wrong.
POSH, OR PLAIN OLD SEMANTIC HTML
Standards advocates have coined the phrase “Plain Old Semantic HTML,” or POSH, as a mnemonic term to encapsulate the idea of using HTML as it was originally intended: to present information so that it conveys meaning and significance to the reader as well as the reader agent.
So what does that mean for you? It means that you must remember and practice the key concept: semantics over presentation. You’ve heard the term “separating presentation from content” before, right? It simply means making sure the markup that creates visual effects, but lends no meaning to the structure of the document, is stripped out and put into a style sheet.
To support separating content from presentation, you need to use your tags for their meaning, not for how you would like them rendered by the browser. Think of using the correct tags to convey meaning as adding the right intonation and facial expressions when you talk. Proper semantics are the key to getting the point across with HTML documents.
For example, while the following code snippet is syntactically correct (there are no actual errors), from a semantics standpoint it needs major help:
<p>Greatest Detectives of All Times</p> <p>These have proven to be some of the best detectives to read and learn from in literature.</p> <p>Sherlock Holmes<br> Encyclopedia Brown<br> Hercule Poirot<br> The CSS Detective</p>
What’s wrong with it? There is no indication of what the elements are in relationship to each other, and what they truly are themselves. With the corrected snippet, you can clearly see their identities and the code hierarchy.
<h1>Greatest Detectives of All Times</h1> <p>These have proven to be some of the best detectives to read and learn from in literature.</p> <ol> <li>Sherlock Holmes</li> <li>Encyclopedia Brown</li> <li>Hercule Poirot</li> <li>The CSS Detective</li> </ol> And trust me, it is truly a boon for both you and your markup. Your HTML will be easier to read, you will be able to better control the visual display, and you will be that much farther on the road to becoming not only a CSS detective, but a CSS pro.
What’s in it for me?
POSH isn’t just a nice idea, nor is it solely promoted by a small contingent of well-intentioned groupies. Semantic HTML has become the standard, not the exception, for serious web professionals who care about their craft. What’s more, standards-based markup has many immediate and far-reaching benefits such as these:
- Your pages will be easier to maintain.
- Authors, users, and browser agents will find it easier to determine document and content hierarchies and relationships.
- Your pages will get a better search-engine ranking, as document content hierarchy is distinguishable.
- Your pages will load faster thanks to less code.
- Your pages will be more accessible to people seeing the document in an environment where CSS cannot be applied (text-only readers, other media).
- Your pages will be understandable to users who have it read to them through a screen reader.
But finally, writing HTML any other way makes it harder to see where the problems are in your code. Make life easy on yourself and write semantic code!
General HTML troubleshooting tips
When I troubleshoot, I start with the area that I think the problem is in and then work my way out of it in a spiral or concentric circles. I also tell myself “it’s something simple,” which helps me relax and find problems more easily.
Here are my guidelines for troubleshooting HTML code.
- Check the <tag> name—is it spelled correctly? You’ll know it isn’t if you have one or both of these problems:
- Tag contents (ie, the tagname itself) show up as text.
- The text or section of the document is not affected by the tag the way you intended.
- The tag name shows up as text with a < in front of it.
- text—text from the start tag on has that formatting.
- lists—any new lists indent after the initial one.
- tables—the new table is nested within the first table.
- Contents of the tag don’t show up at all.
- Contents of the tag don’t have any of the formatting.
- Contents of the tag have some of the formatting, but not all of it.
- Contents of the tag don’t have any of the formatting you wanted.
- Contents of the tag have some of the formatting, but not all of it.
- This may not show up as a problem—the browser may render it anyway.
- The text may not show up.
- Content is affected in a different area than you intended.
If you are really stumped and can’t find the errors in your markup, then validate your page using an HTML validator (see the Resources section for a complete list). Validation for both markup and CSS code is covered in further detail in Chapter 3.