Sams Teach Yourself HTML and CSS in 24 Hours (7th Edition)

This is a "teach yourself" book, so you don't have to go to a single class to learn how to give your pages great style, although you do need to learn what a style class is. Whenever you want some of the text on your pages to look different from the other text, you can create what amounts to a custom-built HTML tag. Each type of specially formatted text you define is called a style class. A style class is a custom set of formatting specifications that can be applied to any element in a web page.

Before showing you a style class, I need to take a quick step back and clarify some CSS terminology. First off, a CSS style property is a specific style that can be assigned a value, such as color or font-size. You associate a style property and its respective value with elements on a web page by using a selector. A selector is used to identify tags on a page to which you apply styles. Following is an example of a selector, a property, and a value all included in a basic style rule:

h1 { font:36pt Courier; }

In this code, h1 is the selector, font, is the style property, and 36pt Courier is the value. The selector is important because it means that the font setting will be applied to all H1 elements in the web page. But maybe you want to differentiate between some of the H1 elementswhat then? The answer lies in style classes.

Suppose you want two different kinds of <h1> headings in your documents. You would create a style class for each one by putting the following CSS code in a style sheet:

h1.silly { font:36pt Comic Sans; } h1.serious { font:36pt Arial; }

Notice that these selectors include a period (.) after H1, followed by a descriptive class name. To choose between the two style classes in an HTML page, you would use the class attribute, like this:

<h1 >Marvin's Munchies Inc.</h1> <p> Text about Marvin's Munchies goes here. </p> <h1 >MMI Investor Information</h1> <p> Text for business investors goes here. </p>

When referencing a style class in HTML code, you simply specify the class name in the class attribute of an element. In this example, the words Marvin's Munchies Inc. would appear in a 36-point Comic Sans font, assuming that you included a <link /> to the style sheet at the top of the web page and assuming that the user has the Comic Sans font installed). The words MMI Investor Information would appear in the 36-point Arial font instead.

What if you want to create a style class that could be applied to any element, rather than just headings or some other particular tag? You can associate a style class with the <div> tag (which, as you may recall from Hour 5, "Basic Text Alignment and Formatting," is used to enclose any text in a block that is somewhat similar to a paragraph of text).

You can essentially create your own custom HTML tag by using the div selector followed by a period (.) followed by any style class name you make up and any style specifications you choose. That tag can control any number of font, spacing, and margin settings all at once. Wherever you want to apply your custom tag in a page, use a <div> tag with the class= attribute followed by the class name you created.

For example, the style sheet in Listing 12.1 includes the following style class specification:

div.footnote { font-size:9pt; line-height:12pt; text-align:center; }

Did you Know?

You may have noticed a change in the coding style when multiple properties are included in a style rule. For style rules with a single style, you'll commonly see the property placed on the same line as the rule, like this:

div.footnote { font-size:9pt; }

However, when a style rule contains multiple style properties, it's much easier to read and understand the code if you list the properties one-per-line, like this:

div.footnote { font-size:9pt; line-height:12pt; text-align:center; }

This style class is applied in Listing 12.2 with the following tag:

<div >

Everything between that tag and the closing </div> tag in Listing 12.2 appears in 9-point centered text with 12-point vertical line spacing.

What makes style classes so valuable is how they isolate style code from web pages, effectively allowing you to focus your HTML code on the actual content in a page, not how it is going to appear on the screen. Then you can focus on how the content is rendered to the screen by fine-tuning the style sheet. You may be surprised by how a relatively small amount of code in a style sheet can have significant effects across an entire web site. This makes your pages much easier to maintain and manipulate.

Категории