|
Instructional Module X20d
|
|
||
| |
|---|
|
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 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. |
|
|||||||||||||||||
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,
in the lower left of the tree illustration. The UA sees that piece this way: |
|||||||||||||||||
|
|||||||||||||||||
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?
(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
|
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:
How can you tell the difference between block and inline? Here's a simple test:
Example Block and Inline ElementsHere are some examples of block and inline elements. (Again, these are examples - not a comprehensive list.)
|
||||||||||||||||
| |
|
|---|---|
|
General
|
Let's start CSS anatomy with an example of a style:
Each part of this has a technical name. This diagram illustrates the names:
What Goes WhereA style consists of a selector and a declaration
We'll go into more detail on these in just a moment... |
|
In-Line CSS
|
In-line CSS has a slightly different anatomy. For example:
Inside the quotes, the rules are built just the same. |
| |
|||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Types of Selectors
|
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 NamesJust 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. ClassesClasses 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:
IDsIDs 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:
Pseudo-classesPseudo-classes are used to modify how links appear. There are four commonly used pseudo-classes:
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.
Pseudo-elementsTwo pseudo-elements were introduced with CSS1 to help designers make Web pages look more like printed books. They are:
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.
|
||||||||||||||||
|
Grouping Selectors
|
Lists of Selectors: Grouping of EqualsRather 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:
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 GroupingSuppose 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):
Another example would be when you want to specify the type of numbering for ordered lists at each level of nesting:
|
||||||||||||||||
| |
|
|---|---|
| Properties
|
General definitionA 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. CharacteristicsProperties can be modified in certain ways and have certain types of influence on elements.
List of possible propertiesFor a list of possible properties, see the CSS1 specifications: http://www.w3.org/TR/REC-CSS1#css1-properties |
| Values
|
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 |
|
| |
|
|---|---|
![]() |
Click here for review questions. |
|
Audience |
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:
|
| 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. |