Home > Articles > Web Development > HTML/CSS

  • Print
  • + Share This
This chapter is from the book

Using Cascading Style Sheets

Earlier today, I mentioned Cascading Style Sheets as a way you could control the look and feel of your pages. Styles are a way to control how the browser renders HTML tags (or elements, as they're called in standards documents). For example, in today's lesson, I've used the <h1> tag a number of times. Most browsers print text enclosed inside an <h1> tag in a large, boldface font and leave some white space after the heading before printing something else. Using Cascading Style Sheets, you can tell the browser to render the <h1> tag differently than it normally would. CSS provides a lot of flexibility in how you can alter the appearance of any type of element, and can be used in a number of different ways.

The advantage of CSS is that it can be used at varying levels of specificity. For example, you can put all your styles into a separate file, and link to that file from your Web page. That way, if you want to change the appearance of your site, you can simply edit your CSS file and make changes that span every page that links to your style sheet. Or, if you prefer, you can include styles at the top of your page so that they apply only to that page. You can include styles inside the tags themselves using the style attribute (which I'll discuss later).

You can also control the specificity of the styles you create based on how you define them. For example, you can write rules that apply to all tags of a specific type, such as all <h1> elements. Or you can specify classes for your elements and then write rules that apply only to members of that class. For example, you could create a class called headline and then make all <h1> elements in the headline class red. You can also write rules that apply to specific elements by assigning them a particular identifier and writing rules that apply to that identifier.

One thing you'll find as you progress through the book is that CSS can serve as a replacement for many common tags. As I describe various tags, I'll explain how the same effects can be achieved using CSS instead. Generally, the flexibility of CSS makes it preferable to using regular HTML tags for just about anything. The coverage of CSS in this book culminates with Day 9, "Creating Layouts with CSS," which explains how to use CSS to manage the entire layout of the page, or even the entire layout of a site.

Including Styles in Tags

You've already seen how HTML pages are created using tags. I want to stop briefly and discuss attributes as well. An attribute is an additional bit of information that somehow affects the behavior of a tag. Attributes are included inside the opening tag in a pair. Here's an example:

<tag attribute="value">

Some attributes can be used with nearly any tag; others are highly specific. One attribute that can be used with nearly any tag is style. By including the style attribute in a tag, you can include one or more style declarations within a tag itself. Here's an example using the <h1> tag, which I introduced earlier:

<h1 style="font-family: Verdana, sans-serif">Heading</h1>

The style attribute of the <h1> tag contains a style declaration. All style declarations follow this same basic pattern, with the property on the left and the value associated with that property on the right. In the preceding example, the property is font-family, and the value is Verdana, sans-serif. This attribute modifies the standard <h1> tag by changing the font to Verdana, and if the user doesn't have that font installed on his system, whichever sans-serif font the browser selects. (Sans-serif fonts are those that do not include serifs, the small lines at the ends of characters.)

There are many, many properties that can be used in style declarations. As I've already said, putting a declaration into a style attribute is just one of several ways that you can apply styles to your document.

  • + Share This
  • 🔖 Save To Your Account