Instructional Module X20d

CSS Anatomy


to Top Overview

to Top

We're looking at CSS and HTML anatomy in this module. So... Anatomy - what is it? Here are the relevant definitions from Merriam-Webster OnLine:

anat·o·my

1 : a branch of morphology that deals with the structure of organisms
6 : a separating or dividing into parts for detailed examination : ANALYSIS

In other words, we'll be looking at the structure of the "organism" (CSS and HTML), dividing it into parts, and giving each part a detailed examination or analysis.

It will be necessary to start with the anatomy of HTML, because the way CSS behaves is based on how HTML is put together. Once we understand that, many features of CSS will be clearer.


to Top Anatomy of HTML

to Top

By now, of course, you're familiar with the general anatomy of an HTML file: it's divided into a <head> section and a <body> section. Here, we'll look at HTML anatomy in more detail.

By the way - We're using the term user agent here. A "user agent" is any program that renders an HTML file for a user. We generally use the term browser because that's the type of user agent we're most familiar with. Well, we're going to have to get over that. With people accessing the Web from many different types of devices, there are new kinds of user agents deployed all the time. And what about "intelligent agents" - software "bots" that search for information autonomously? They're user agents too, and will become more common as they become more capable - and as Web content becomes more friendly to a variety of user agents.

We'll see first at how user agents (UAs) look at HTML pages, then zero in on "block" and "inline" elements of HTML.

How a User Agent Looks at HTML

to Top

When a user agent (UA) renders a Web page, its first task is to figure out what is part of what. The reason is that an element that's inside another element may be rendered differently than it would when it's not inside that element.

For example, how is a simple paragraph of text affected by what it's inside of?

  • A paragraph that's "loose" in the body of a page has to stay inside the window margins;
  • A paragraph that's inside a table cell can't have its words go beyond the cellpadding of that cell.

The UA has to consult different width specifications in order to wrap the lines of a paragraph correctly, and if CSS is used in the file, the paragraph may "inherit" characteristics from the element it's part of. All this means it's essential for the UA to know what the paragraph - or any other part of the page - is contained in.

This is often done by constructing a "tree" as the UA software reads in the HTML text. (A tree is a hierarchical data structure that allows very rapid and effective use of computer resources.)

The "tree" is based on what HTML structures (shown by the tags) can "contain" other structures. Here is an example:

Browser View

The Bat, the Birds and the Beasts

A great conflict was about to come off between the Birds and the Beasts. When the two armies were collected together the Bat hesitated which to join.

  • The Birds that passed his perch said:
    • "Come with us";
    • but he said: "I am a Beast."
  • Later on, some Beasts who were passing underneath him looked up and said:
    • "Come with us";
    • but he said: "I am a Bird."

Luckily at the last moment peace was made, and no battle took place, so the Bat came to the Birds and wished to join in the rejoicings, but they all turned against him and he had to fly away. He then went to the Beasts, but soon had to beat a retreat, or else they would have torn him to pieces.

"Ah," said the Bat, "I see now,

Moral: He that is neither one thing nor the other has no friends.
Code View

<html> <head> <title>A Fable of Aesop</title> </head> <body> <h1>The Bat, the Birds and the Beasts</h1> <p>A great conflict was about to come off between the Birds and the Beasts. When the two armies were collected together the Bat hesitated which to join. </p> <ul> <li>The Birds that passed his perch said: <ul> <li>"Come with us"; </li> <li>but he said: "I am a Beast."</li> </ul> </li> <li>Later on, some Beasts who were passing underneath him looked up and said: <ul> <li>"Come with us";</li> <li>but he said: "I am a Bird." </li> </ul> </li> </ul> <p>Luckily at the last moment peace was made, and no battle took place, so the Bat came to the Birds and wished to join in the rejoicings, but they all turned against him and he had to fly away. He then went to the Beasts, but soon had to beat a retreat, or else they would have torn him to pieces.</p> <p>"Ah," said the Bat, "I see now,</p> <blockquote><em><strong>Moral</strong>: He that is neither one thing nor the other has no friends.</i> </blockquote> </body> </html>

Tree View
 

By constructing a tree such as this, the UA can tell exactly what each piece of the page is a part of, and if that's a part of the another element, it will be able to tell that, too.

To make this clearer, let's look at one piece of the text in the example: the string,

"Come with us";

in the lower left of the tree illustration. The UA sees that piece this way:

"Come with us";

is part of <li> 2

is part of <ul> 2

is part of <li> 1

is part of <ul> 1

is part of <body>

is part of <html>

 

Each element of the page influcences the elements contained inside it, so something in the <body> tag could easily influcence the way "Come with us"; is rendered.

What is the influence (or potential influcence) of each element on "Come with us"; ?

What can Contain What?

An essential piece of information the UA needs to construct a tree like this is, "What can contain what?" Not every element of HTML can contain every other element. Some elements are "too big" to fit into others; other elements are picky about what they'll accept.

Consider each of these elements. Based on your understanding of HTML, what can they hold directly?

<html> <head>, <body>, <frameset>
<p> <b>, <i>, <strong>, <em>, <font ...>
<font...> <b>, <i>, <strong>, <em>
<li> <b>, <i>, <strong>, <em>, <font ...>

(Highlight to the right of the tag, or print the page, to see some of the possible elements they can hold. These are not intended to list all the possible content elements.)

When a Web page is validated, a frequent error is the inclusion of a tag in an element that can't (officially) hold it. The validator will give a list of elements that are allowable in that context. When that happens, it's usually possible to correct the problem by putting an element that was outside another element, inside it.

Block and Inline Elements of HTML

to Top

One of the most important distinctions in what can contain what, is block elements versus inline elements. One of the most common causes of validation errors is attempting to put a block element inside an inline element.

In general:

  • Block elements can contain:
    • (some) other block elements, and
    • inline elements;
  • Inline elements can contain:
    • only other inline elements.

How can you tell the difference between block and inline? Here's a simple test:

If putting in the tags causes a new line to be started, it's probably a block element.

Example Block and Inline Elements

Here are some examples of block and inline elements. (Again, these are examples - not a comprehensive list.)

Block Inline
<html>
<strong>
<body>
<em>
<h1>, <h2>, <h3>, <h4>, <h5>, <h6>
<b>
<p>
<i>
<ul>, <ol>
<font...>
<li>
<tt>
<div>
<span>

 


 
to Top CSS Anatomy
General

to Top

Let's start CSS anatomy with an example of a style:

h1, h2 {font-family: Verdana, Arial, Helvetica; color: blue;}

Each part of this has a technical name. This diagram illustrates the names:

What Goes Where

A style consists of a selector and a declaration

  1. A selector can contain one or more of the following:
    1. an html element name (shown in the diagram above)
    2. a class name
    3. an ID name
    4. a pseudo-class name
    5. a pseudo-element name
    6. a list of the above (a list of elements is shown in the diagram above)
    7. one or more contextual selectors
  2. A declaration contains one or more rules
    1. a rule consists of a property and a value
      1. a property: any of about 53 properties in CSS1, more in CSS2.
      2. a value: any of several values specific to a property, such as the display property's block value, or common to several properties, such as colors and measures.

We'll go into more detail on these in just a moment...

In-Line CSS

to Top

In-line CSS has a slightly different anatomy. For example:

<h1 style="font-family: Verdana, Arial, Helvetica; color: blue; ">Hello!</h1>

  • Starts with style=
  • No selector - not needed, because the style applies directly to the element it's in.
  • Quotes "" instead of curly braces {}.

Inside the quotes, the rules are built just the same.

 
to Top Selectors
Types of Selectors

to Top

In this section we'll discuss each of the possible selector types listed in the previous section. This is the part of a style that comes before the curly braces, and doesn't happen at all in in-line styles.

HTML Element Names

Just about any HTML tag can appear as an element name. The exceptions are <head> and the tags that can only appear in the head section, <frameset>, <frame>, and possibly a few others.

An HTML element whose name appears in the selector of a style will be rendered according to that style everywhere in the document. You don't have to do anything more to the HTML tag to make this work.

Classes

Classes allow us to define a style that can be applied to any HTML element (with the exception of those excluded from being selector element names). A UA recognizes a name as a class when we put a dot in front of it.

Here's an example of a class used as a selector name and applied to HTML tags:

Code View
<html> <head> <style type="text/css"> .redletter {color: red;} </style> </head> <body> <h3 class="redletter"> This is a special red-letter title</h3> <p class="redletter"> This paragraph is special enough to have red letters, too!</p> </body> </html>
Browser View

This is a special red-letter title

This paragraph is special enough to have red letters, too!

IDs

IDs are very much like classes, except that an ID is supposed to occur only once in a document. An ID also has higher priority than a class, if the two happen to be in conflict. Here's a brief example:

Code View
<html> <head> <style type="text/css"> #para1 {font-weight: bold;} </style> </head> <body> <p ID=para1>This paragraph should be in bold type because of its ID.</p> <p>This paragraph will be in normal type.</p> </body> </html>
Browser View

This paragraph should be in bold type because of its ID.

This paragraph will be in normal type.

 

Pseudo-classes

Pseudo-classes are used to modify how links appear. There are four commonly used pseudo-classes:

  • :link is the initial appearance of links;
  • :hover is what the link does when a user holds the pointer over it;
  • :active is when the link has just been clicked;
  • :visited is after you've returned from a link.

These are always used with a in front of them, since they apply only to Anchor <a ...> tags.

Note: Older browsers don't support all the pseudo-classes equally - in particular hover was rarely supported before browser version 6.

Code View
<html> <head> <style type="text/css"> a:link {color:#000000; text-decoration:underline;} a:visited {color:#660066; text-decoration:underline;} a:hover {color:#660099; text-decoration:underline; background-color: #FFFFFF} a:active {color:#ff0000; text-decoration:underline;} </style> </head> <body> <a href="http://www.wccnet.edu">Wcc Home Page</a> </body> </html>
Browser View
WCC Home Page

 

Pseudo-elements

Two pseudo-elements were introduced with CSS1 to help designers make Web pages look more like printed books. They are:

  • :first-letter of a block
  • :first-line of a block

These are used with the selector for the type of element for which you want special treatment.

Note: pseudo-elements are poorly supported in most browsers.

Code View
<html> <head> <style type="text/css"> section:first-line {color: green;} section:first-letter {color: #999900; font-size: 200%;} </style> </head> <body> <section>Luckily at the last moment peace was made, and no battle took place, so the Bat came to the Birds and wished to join in the rejoicings, but they all turned against him and he had to fly away. He then went to the Beasts, but soon had to beat a retreat, or else they would have torn him to pieces.</section> </body> </html>
Browser View
Luckily at the last moment peace was made, and no battle took place, so the Bat came to the Birds and wished to join in the rejoicings, but they all turned against him and he had to fly away. He then went to the Beasts, but soon had to beat a retreat, or else they would have torn him to pieces.

 

 
Grouping Selectors

to Top
Lists of Selectors: Grouping of Equals

Rather than do each selector individually, it's a good idea to group together selectors that have the same features. That can be done by separating them with commas, like this:

h1, h2, h3, h4, h5, h6 {font-family: Arial, Helvetica, sans-serif;}

In this way, the font for all headers is set at the same time, and can be changed at the same time.

Contextual Selectors: Unequal Grouping

Suppose you want a certain effect for an HTML element only when it's part of another element? CSS provides for this with contextual selectors. This is done by putting two elements in a selector with no comma between them.

For example, to make <strong> text inside an <h1> capitalized (since <h1> is normally bold already):

h1 strong {text-transform: uppercase;}

Another example would be when you want to specify the type of numbering for ordered lists at each level of nesting:

ol li {list-style-type: upper-alpha}
ol li li {list-style-type: decimal}
ol li li li {list-style-type: lower-alpha}
ol li li li li {list-style-type: lower-roman}

 

 

 
to Top Rules
   
Properties

to Top
General definition

A property in CSS is one of the list of about 55 whose modification allows us to affect how a document is rendered.

In other words, a property is what you change to make your Web page come out the way you want it.

Characteristics

Properties can be modified in certain ways and have certain types of influence on elements.

  • Possible values: depending on the type of property, you may be able to assign a color, a measurement, a border, margins, or special values unique to one property.
  • Initial value: the default. This is usually determined by the CSS specifications, but may be modified by UA or individual preferences.
  • Inheritance: Whether a property's value will be used in elements contained within the one where it's used, and if so, how much weight does it have with respect to other properties.
  • Applicability: What type of element can this property be used on? Not all properties can be used on all HTML elements. For example, vertical-align can only be used with inline elements.
List of possible properties

For a list of possible properties, see the CSS1 specifications: http://www.w3.org/TR/REC-CSS1#css1-properties

Values

to Top

In order to know what value you can assign to a property, you generally need to look it up, either in a book or in an on-line reference, such as the CSS1 specifications.

Each property has possible values appropriate to its purpose. For example, you can give a color and a measurement to a border, but you can't give it the name of a font. Font names can only be used with the font-family property.

Interesting Reading about CSS

to Top About This Document
Review Button

Click here for review questions.

Audience

to Top

This module is for people who are familiar with the general concepts of Cascading Style Sheets (see module X20c) and want to understand the structure of CSS.

 

Objectives

On successful completion of this module, you will be able to:

  1. Explain the structure of HTML documents;
  2. Differentiate between block and in-line HTML elements.
  3. Identify a CSS rule and its parts;
  4. Differentiate between selectors for elements, classes, and IDs;
  5. Differentiate between classes, IDs, pseudo-classes, and pseudo-elements;
to Top
Module X20d: CSS Anatomy
This document is part of a modular instruction series in Computer Instruction. For more information, see the overview or the list of modules in this series, X: "XML, XHTML, DHTML, CSS". This document has been used in the following classes: INP 270.
History:
Original: 16 March 2003, by Laurence J. Krieg
Last modification: Monday, 31-Aug-2009 11:48:08 EDT
Copyright
Copyright © 2003, Laurence J. Krieg, Washtenaw Community College
Instructors: You may point to this file in your Web-based materials; however, its location may change without notice.
Students: You are welcome to make a copy for your personal use.
All other uses: Please contact the author, Laurence J. Krieg, for permission: krieg@ieee.org.

to Top