Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

Introduction to The CSS Detective Guide

📄 Contents

  1. HTML Document Structure
  2. CSS Document Structure
  • Print
  • + Share This
Denise R. Jacobs introduces HTML best practices: document structure, good semantics, and tag structure. From there, she moves on to CSS, including rule structure, getting the styles into your documents, and commenting.
From the book

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:

  1. All elements are closed.
  2. All tags are in lowercase.
  3. All attributes values are enclosed by quotes.
  4. 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:

<tagname attribute="value"></tagname>

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.

  1. 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.
  2. Check that the tag has its ending bracket (>). You’ll know it’s missing if:
    • The tag name shows up as text with a < in front of it.
  3. Make sure the start tag has a closing tag—for example, <tag>word</tag>. You’ll know this is the problem if:
    • 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.
  4. Check the <tag attribute="value"> syntax. Check the spelling of the tag name, attributes, and values, and make sure the attribute value has an ending quotation mark. You’ll know this is the problem if:
    • 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.
  5. Check that you have placed the attribute you want in the proper tag. You’ll know this is the problem if:
    • 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.
  6. Check the order of nested tags. Make sure that tags are nested properly, like parentheses: ( [{ word }] ). For example, <a href=http://www.something.com><em>this is a link</em></a>. You’ll know this is the problem if:
    • This may not show up as a problem—the browser may render it anyway.
    • The text may not show up.
  7. Check that you have placed the tags in the proper place. You’ll know this is the problem if:
    • 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.

  • + Share This
  • 🔖 Save To Your Account

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020