Web Design with HTML and CSS

(National Geographic (Little) Kids) #1

70


The role of CSS

Web Design with HTML and CSS Digital Classroom

Understanding class styles and s.


Tag selectors are frequently used, but they can only be applied to HTML elements. When
you want to style something that does not map directly to a tag, for example, change the
color of a single word within a paragraph, standard HTML tags are not a good option. In this
case, you can use a class selector, which is a CSS rule that you can apply to any number of
items on a page. Class selectors have fl exible naming options, but you should choose names
that describe what they do. For example, you may wish to name class selectors as caption,
imageborder, or redtext. In this exercise, you will create a class style that applies the color
purple to the word Smoothies in your paragraph.

1 Place your cursor on the line immediately below the closing curley bracket for the h1
rule, then type the following:
.purple {
color:purple;
}
Note the period at the beginning of the class selector. The text following the period is
the class name. You can use any name you wish, but the period is required at the start
to identify it as a class.The rule is the same as in the last exercise, only here the selector
is not the h1 element. The class name can be anything you want, but it must have the
period at the beginning to identify it as a class. Next you’ll apply this class to the word
Smoothie in order to style it purple. To do this, you will use an HTML tag <span>.
2 In the paragraph within the <body> tag, locate the word Smoothies, click once to the
left of it, and then type:
<span>

The <span> tag allows you to defi ne the portion of a paragraph you’d like to style.

3 Click to the right of the word Smoothies and add a closing span tag </span>.
Your code should look like this:
<p><span> Smoothies</span> are the ...

Save your fi le. If you were to preview the page in the browser you would see no change.
The <span> tag in HTML is an empty tag; it does nothing on its own and needs to be
paired with a style. The <span> tag defi nes the beginning and end of where the style
will be applied within the paragraph, but it does not apply the style on its own, and does
not defi ne the style.
Free download pdf