HTML was not designed to control the form or appearance of Web pages. It defines the logical structure and function of elements on a page. The Web browser decides how those elements should actually be rendered. Tags such as <font> and attributes such as bkgrnd and align were introduced because designers wanted to control page appearance. Designers used (and still use) structural elements such as tables and lists to control layout unrelated to logical structure or function.
Style Sheets define specifications that can control the presentation of the rendered documents. They do not affect the contents of the document, only the appearance (or equivalent). With style sheets,
Styles let you
There are 3 basic ways to incorporate style rules into a document:
HTML Styles are referred to as Cascading Style Sheets because they are implemented in a "cascading" order. The style definition "closest" to the element is used. Inline Style takes precedence over Embedding Style, which takes precedence over External Style. For example, if an external style sheet defines a heading 1 tag as 16pt blue and the top-of-page embedded style sheet defines it as red, the text will be displayed as 16pt red (assuming size and color can be displayed). If in addition, an inline h1 tag has a style attribute saying it is 12pt, then the displayed text will be 12pt red. (The font-family would be whatever the default is.)
Style rules are defned in external and embedded style sheets, in the form
selector {attribute_1: value_1a value_1b;
attribute_2: value_2a value_2b;
etc.}
External style sheets are simply files containing style rules. One example is the one for this class's home page.
The file can be linked into the document with a tag of the form
<link rel="relation"
href="URL of file"
title="title for style sheet"
media="display medium"
type="text/css">
in the document's head section.
relation will be replaced with stylesheet or alternate stylesheet. The first will spectfy the default sheet to be used.
If more than one (non-alternate) style sheet is designated, they will be loaded in the order they appear to form a single style sheet, with later defined rules taking precedence over earlier ones.
If a document has alternate style sheets, they can be used by selecting "Page Style" from the Firefox browser's view menu. For example:
<!-- The primary style sheet for screen display -->
<link rel="stylesheet" title="default"
href="common/default_pagestyle.css" type="text/css" media="screen" />
<!-- The alternate style sheet -->
<!-- The word alternate (in the rel value means that it will not be used automatically -->
<link rel="alternate stylesheet" title="silly alternate"
href="common/silly_pagestyle.css" type="text/css" media="screen" />
<!-- The style sheet especially for printing -->
<link rel="stylesheet"
href="common/printer_pagestyle.css" type="text/css" media="print" />
With most browsers, the first style sheet will be the one used when the document is loaded. The third style sheet is used only when the document is printed.
As an example, look at the wyrd.hood.edu index page and select "Page Style" from the view menu. The default style is named "wyrd style". Choose the alternate style, named W3C. (This is one of the W3C demonstration styles, and the file is actually located on a W3C server, not on wyrd.)
As an example, Weekly Class Topicspage for this course.
Then choose "Print Preview" from the file menu.
Notice that the page is different for printing - some parts, such as the picture and the bottom menubar, are not shown. However other identifying information is shown at the bottom. Also, the font is different - a serif printer-friendly font instead of a sans-serif one that is larger and better for screen viewing.
is called a selector. The simplest selector is simply the symbol for an element. In IT 280, we also looked at classes, which let you create categories of elements.
The selector identifies the elements to which the rule will apply. It is the first part of a CSS rule, outside the { } curly braces.
Selectors can be defined in a number of ways. We will look at just a few of them here. See the documentation(http://www.w3.org/TR/CSS2/selector.html#q2) for more a comprehensive discussion.
The simplest selector is just the symbol for an element. In the following example, the selector is the paragraph element p.
p {
color: black;
font-family: "Times New Roman", Times, Serif; /* list of choices */
font-size: 12pt;
font-style: italic
border: solid thin #f00; /* values for several sub-attributes in in rule */
}
This rule specifies how text enclosed in the p element should be displayed. Spaces are delimiters in CSS, so multi-word terms such as "Times New Roman" must be quoted. Notice that the style specifies several alternatives for the font-family property. This is good practice because not all fonts are available on all systems. For instance, Times New Roman is a proprietary Microsoft font that may not be available on Macintosh or Unix systems.
The div and span elements, provide a generic mechanism for adding your own structure to documents.
They do not have any intrinsic structural meaning of their own, other than that
div is a block-level element.span is an inline element.Recall that a block is a complete unit of information, such as a paragraph, heading, or list, while an inline element is a group of characters within a block.
You can use div and span in conjunction with style sheets and classes to make, in effect, custom HTML elements.
*.item {
color: #800040;
font-size: 10pt;
font-weight: bold;
}
div.item {
color: #0000ff;
font-size: 16px;
font-weight: normal;
font-style: italic;
}
*.gaudy {
color: #ff0000;
font-size: large;
font-family: "ITC Zapf Chancery", "Lucida Handwriting", cursive;
}
The first rule defines a class named item (notice the asterisk (*) and the dot ( . ). The asterisk means any element can be used for the class, the dot indicates a class. (Such a pattern place holder is refered to as a wild card or glob.) The second rule will only be applied to divs with class item. The example
<p class="item">This is a paragraph of class item. The rule for *.item applies.</p>
<p><span class="item">This is an inline span of text with class item </span> within
a paragraph. The rule for *.item applies.</p>
<div class="item">This is a div with class item. The rule for div.item applies
to the entire div. <span class="gaudy">This span adds some additional rules</span>
within the div. </div>
renders as
This is a paragraph with class item. The rule for *.item applies.
This is an inline span of text with class item within a paragraph. The rule for *.item applies.
On the WebDev multibar CSS 2.1 reference, you can click the small tab marked "Sel" for an index of selectors.
Elements in HTML documents can be assigned an identifying name with the id attribute. For example, <h2 id="topics>. The id is used to identify the fragment in a URL. It can also be used to single out an element for a style rule.
Note: Although you can type the same id for more than one element, you should not. To work correctly, ids should be unique - only assigned once per document. If you assign the same id value twice in the same document, the validator will report an error.
A rule with an id selector applies only to the unique element with that specific id. It is indicated by a # symbol.
#foot_menu {
width: 90%;
border-top: dotted 3px rgb(255, 127, 127);
text-align: center;
}
h1#first_page {
font-size: 3em
}
div#screen_only { background-image: url(../graphics/beigepaper.jpg);
@media print{
#screen_only {
display: none;
}
}
The first rule will be applied to the element whose id is foot_menu. such as <div id="foot_menu"> or <p id="foot_menu">, or <h2 id="foot_menu"> or whichever element is identified by that id (there must be only one of them).
The second rule will be applied only to an h1 element with the id value of first_page, such as <h1 id="first_page"> ; if the id belongs to a different element the rule will not be used.
Similarly, the third rule will be applied only to a div element with the id value of screen_only.
The fourth rule will be applied to the designated element if and only if the media type is print, hiding it on the printed page but not the screen.
The structure of a valid XHTML document can be represented as a tree structure. Consider the following short document.
The following short HTML document
<html>
<head>
<title>The Emmel's home page</title>
</head>
<body>
<h1>The Emmel's home page</h1>
<p>We are a multi-generation
<strong>family</strong>. Our names are
<ul>
<li>Essgy</li>
<li>Aitchtee</li>
<li>Exx</li>
</ul>
</body>
</html>
could be diagrammed as:
![[Image missing]](lessons/week02/tree1.gif)
The document itself (html) is the root element.
Elements higher on the tree are ancestors of those below them (descendents). Elements directly connected on the diagram are parent and child. Each element has exactly one parent, with the exception of the root element, which has none. In the example, body is the parent of h1, p, and ul; strong is a child of p.
The parse-tree determines the selectors for style rules and the structure for rendering the page. If the document is not valid, the parse-tree may not be constructable or usable. For example if tags are nested incorrectly, it is impossible to determine which is parent and which is child.
<a href=gotopage.html"><strong>My homepage</a></strong>
In the HTML
<body> <h1>The <em>beginning</em> <strong>of the <em>end</em></strong></h1> <p><strong>And so it <em>begins </em></strong>, the <strong>search</strong> for better <em>style</em></p> </body>
![[Image missing]](lessons/week02/tree2.gif)
In the first line, the first em is a child of h1. The second em is a child of strong and a grandchild of h1.
in an id selector, the ancestor is separated from the descendent by whitespace.
h1 { color: red }
em { color: green }
p { color: teal }
p em { color: blue }
In the example, h1 text will be red and all em text will be green except for any em that is a descendent of p; that will be blue. Here is an example with that HTML and style rules.
The "ancestor" could be an id selector. For example, a rule selected by #demo strong would apply only to strong elements contained within the element with id "demo".
A descendant selector of the form A B matches when an element B is a descendant of some ancestor element A. In the descendent example above, p em would match both of the em elements in the paragraph text. But what if you want only grandchildren - descendents with at least one intervening element.
In the specific example, where the only intermediate element is strong, we could write p strong em. But we can also allow for any intermediate element.
An asterisk in a selector is a wild card, representing any element. Use one to indicate intervening generations. For example
p * em {background-color: yellow;}
Whitespace around the asterisk is a necessary part of the complete selector.
With this rule, only em elements that are a grandchild or later descendant of a paragraph element will be given a yellow background. Here is the example with that rule added.
A child selector matches when an element is the child of some element. A child selector is made up of two or more selectors separated by the character >. For example
p>em {background-color: teal; color: lime}
This rule applies only to em elements that are an immediate child of a paragraph element, with no intervening descendants. Here is the example with that rule added.
An element B is an adjacent sibling of an element A if A and B share the same parent and A immediately precedes B. The adjacent sibling selector has the form A+B. For example,
h1+p {color: #c9c; }
p+p {font-size: smaller; }
In this case, the text color of the paragraph immediately following the h1 will be changed. Any pragraph that has one immediately before it will have smaller text. Here is the example with those rules and extra paragraphs added.
Style is normally attached to an element based on its identity and relation to other elements in the document. Pseudo-elements are based on relationships beyond those in the document tree, such as the first letter or first line of an element. Pseudo-classes classify elements on characteristics other than their name, attributes or content, such as whether a link has been visited or not. Neither pseudo-elements nor pseudo-classes actually appear in the document source code or document tree. They are abstract concepts.
Pseuso-classes are written after the element using a colon between the element symbol and the class name. Recall that classes use a dot. The following table lists some (not all) of them.
| pseudo-class | description |
|---|---|
| :link | designates an unvisited link |
| :visited | designates a visited link |
| :hover | applies while the user selects an element, but does not activate it; for example, when the mouse pointer hovers over a link. |
| :active | applies while an element is being activated; for example, between the times the user presses the mouse button and releases it. |
| first-child | matches an element that is the first child element of some other element. |
<style type="text/css">
a:link { color: red }
a:visited { color: green }
a:hover { color: yellow }
a:active { color: blue }
li:first-child { font-weight: bold }
</style>
Unvisited links will be red text; visited ones green. When the mouse hovers over a link it will turn yellow, and if the mouse button is pressed but not released it will be blue. The last rule specifies that the first item in a list will be bold, but not the others. /p>
| pseudo-element | description |
|---|---|
| first-line | applies special styles to the contents of the first formatted line of a paragraph, division, or orher block element. (The specific text styled may change if the window is resized.) |
| first-letter | Applies to the first character of text in an element |
<style type="text/css">
p:first-line { text-transform: uppercase }
p:first-letter { font-size: 200%; font-weight: normal }
</style>
The example also illustrates the text-transform property. In this case, the first line of each paragraph will be shown with all uppercase text. The first letter will be twice as large as the others.
Here is an example illustrating both pseudo-class and pseudo-element rules.
Hood College Department of Computer Science: Course materials © 1997-2006 by Elizabeth Chang.