p {color: #F00;}
CSS also allows us to define our own custom selectors. These are known as ID and class selectors. IDs and classes are applied to (X)HTML elements as simple attributes that provide much tighter control over our design.
<p id="pred">This paragraph has red text.</p>
<p id="default">This paragraph has dark gray text.</p>
The corresponding CSS uses the hash (
#) character to denote that the rule is a unique ID. The hash is combined with the ID name to start the rule, followed by the property declarations:
/* Define highlighted text */
#pred {color:#F00;}
/* Define default text */
#default {color:#333;}
Combining IDs with Selectors
/* Basic heading style */
h2 {
color:#333;
font-size:16px;
}
That is fine for most uses of <h2>, but let’s say the main <h2> on your page (e.g., the title of an article) needs to be emphasized with a different color. This calls for a new rule where the selector is defined in the form:
element#name
/* Adjust the color of h2 when used as a title */
h2#title {
color:#F00;
}
Here the new rule will override the default <h2> color (color: #333;) with red (color: #F00;) whenever an <h2> is identified with in the (X)HTML. The new rule does not redefine font-size, so that will be carried over and unchanged. Simply add the unique identifier to the page:
<h2 id="title">Title Of My Article</h2>
Remember that in the above,
title is a unique identifier, so it cannot be used again within that template. Any other instances of <h2> on the page will be rendered with the default styling.
<p class="highlight">This paragraph has red text.</p>
<p class="default">This paragraph has dark gray text.</p>
<p class="default">This paragraph also has dark gray text.</p>
Note that as the identifiers are classes, they can be used more than once, hence, in the example two paragraphs have been identified as default, so will be styled the same way. That would not be acceptable if using IDs. The corresponding CSS uses a period (
.) character to denote the rule is a reusable class. The period is combined with the class name to start the rule, followed by the property declarations:
/* Define highlight class */
.highlight {color:#F00;}
/* Define default class */
.default {color:#333;}
<ul id="drinks">
<li class="alcohol">Beer</li>
<li class="alcohol">Spirits</li>
<li class="mixer">Cola</li>
<li class="mixer">Lemonade</li>
<li class="hot">Tea</li>
<li class="hot">Coffee</li>
</ul>
Note first that the unordered list (<ul>) is given a unique ID. Thus,
id="drinks" will not be used again on the page at any time, and that allows that particular list to be styled uniquely. Note also that Beer and Spirits are within list elements defined with
class="alcohol", Cola and Lemonade are within list elements defined with
class="mixer", and finally Tea and Coffee are defined in list elements with
class="hot". This handling allows each drinks group to be treated individually.
/* Drinks list styling */
ul#drinks {color:#F00;}
Next, the classes for each drink type are defined with unique shades of gray for font color:
/* Define alcohol color */
.alcohol {color:#333;}
/* Define mixer color */
.mixer {color:#999;}
/* Define hot drinks color */
.hot {color:#CCC;}
The result sees the list of items move through shades of gray (defined by the classes). Any further drinks added to the list can be assigned to a particular drinks group. For instance, if we added Wine, we'd add
/* Default styling for paragraphs */
p {color:#F00;font-size:12px;}
/* Use this style to turn anything light gray */
.bleached {color:#CCC;}
All paragraphs will still be red by default, but this can still be overridden when necessary by identifying an element with the custom
bleached class, as in this (X)HTML:
<p>This paragraph has red text.</p>
<p class=”bleached”>This paragraph has light gray text.</p>
The second paragraph will now be light gray, as the color declaration in bleached overrides the red. Note that the paragraph is still rendered 12 pixels high, as bleached does not redefine font-size. Add a font-size declaration in bleached, and that value will override the original size for all paragraphs identified with that class.
/* Use this style to turn anything light gray */
.bleached {color:#CCC;}
/* Override the color of bleached when it identifies a paragraph */
p.bleached {color:#000;}
This method would be used when the standard declaration for the bleached class needs to be overruled. For example, any element given a class of bleached will remain light gray (color:#CCC;), but any instances of paragraph elements with a class of bleached will be rendered black (color: #000;). This method is useful when numerous instances of a class are littering your (X)HTML, and it would be too difficult to remove them all manually. Instead, simply target that class when it identifies the element you need to change using the element.classname form.
<li id="comment-27299" class="comment">
It has a class applied to it that you may want for styling all comments on the page, but it also has a unique ID value (dynamically generated by WordPress, nicely enough). This ID value is useful for direct linking. Now I can style a particular comment on a particular page easily.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)