1.5. Parts of a CSS Rule

Every style, whether it's embedded in a separate style sheet or not, consists of one or more rules. Figure 1-3 shows a CSS rule with all the parts labeled.

Each rule has exactly two parts:

  • A selector that defines the HTML element(s) to which the rule applies

  • A collection of one or more properties, [2] which describes the appearance of all elements in the document that match the selector

    [2] Many books and articles about CSS call them "attributes," or use the two terms interchangeably. In this book, I used the W3C endorsed terminology of "properties", and reserve the name "attributes" for attributes of HTML tags.

Figure 1-3. Parts of a CSS Rule

Each property consists of a pair of values separated by a colon. The first item of the pair defines the specific property that's being modified. The second item describes the value that the property takes on. Each property-value pair must be followed by a semicolon, with one exception: The semicolon following the last property is optional and may be omitted. In this book, however, we will always add this optional semicolon. I encourage you to adopt this habit as well, as it's much easier to train yourself to always add that semicolon than it is to remember when it is required and when it isn't. It also makes it easier to add properties to an existing style rule.

Here are a few examples of increasingly complex CSS rules, with the parts identified so that you can fix this syntax clearly in your mind. Essentially, this is the only real syntax issue you must learn in order to master CSS, so it's important!

h1 {
  color: red;
}

The selector, h1, indicates that this rule applies to all h1 headings in the document. The name of the property that's being modified is color, which applies to the font color. The value we want the color property to take on is red. Chapter 7 and Chapter 9 explore fonts and coloring in CSS in great detail.

p {
  font-size: 14px;
  color: green;
}

The selector, p, indicates the style rule should be applied to all paragraphs in the document. There are two property name-value pairs in the rule. The first, font-size , sets the size of the font in all paragraphs in the document to 14 pixels. A pixel is one dot on your screen, and is the most common measurement used in CSS. See Chapter 3, for an explanation of this and other measurement issues in CSS. The second property is color and is set to green. The result of this rule is that all paragraphs in the document will appear in a green, 14-pixel-high font.

p {
  font-family: 'New York', Times, serif;
}

Again, this rule deals with paragraphs, as is evidenced by the p selector. This time, the selector affects the font family that is used to display text. The new wrinkles in this example are that it includes a list of values for the font-family property, and one of those values is enclosed in quotation marks.

The font-family property is one of a handful of CSS properties to which you can assign a list of possible values, rather than a single, fixed value. When you use a list, commas must separate its individual members. In this case, the font-family property list tells the browser to use New York as the font if the user's machine has it installed. If not, it directs the browser to use Times. And if neither of these fonts is available on the user's system, then the browser is told to default to the font used for serif type. Again, this subject is covered in more depth in Chapter 7 and Chapter 9.

Whenever the name of a property value in a CSS rule includes spaces (as is the case with the font named "New York"), you must put that value into quotation marks. Many designers use single quotation marks for a number of reasons, not the least of which is that they're easier to type, but you can use either single or double quotation marks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.117.109