Приглашаем посетить
Программирование (prog.find-info.ru)

Authoring Style Rules

Table of Contents
Previous Next

Authoring Style Rules

In this section, you’re going to create a structured style sheet built of a number of rules. You’ll take the markup example provided earlier, as well as the document tree, and begin adding rules in a logical order to most of the elements within the tree using element selectors.

Then, to accommodate links, special styles, and other features to enhance the content, you’ll write rules to style a range of page elements. To set up the workflow for this chapter, follow these steps:

  1.  WWW.  Open the Chapter3.html document and insert the following code into the head portion of the document:

    <link rel="stylesheet" type="text/css" media="screen"
    href="chapter3_style.css" />
  2. Open your text editor and create a document called chapter3_style.css.

  3. Save both files to a local folder.

    Keep the CSS document open because that’s where you’ll be adding the rules within this chapter.

Using Element Selectors

As you learned in Chapter 2, element selectors are those selectors that relate to the actual elements within the markup. There are a number of elements in the document tree. Let’s begin by walking through those and creating a style for each.

Styling the body Element

Because the first element that you’ll want to provide style for is the body element, you’ll start by creating rules for it. Typically, body element styles should contain information about the general display of the complete body of the design, such as margins, default font face and color, and any background color and images you might like.

Note 

 For more information about measurement values and their use, please see Appendix A.

Here, you’ll style the margins, padding, default body text font, font color, and background color. You’ll also learn how to employ grouping as you go.

To begin your first rule, write the element name:

body

and follow it with the margin rules:

body  {
     margin-top: 100px;
     margin-bottom: 20px;
     margin-right: 20px;
     margin-left: 100px;
}

This rule now sets all margins for the page at 20 pixels, providing a uniform margin around all the body content.

Note 

 Margin properties are not inherited.

While this set of rules for the body margins is absolutely correct, you can also use grouping as a means to shorthand your rule and streamline your workflow. Using grouping in this case could result in the following rule:

body  {
     margin: 100px 20px 20px 100px;
}
Note 

 If all margin values are equal, you can group them more efficiently by writing body { margin: 20px; } (see the "Grouping Margin Properties" sidebar in this section). If they are not equal (as in this example), or use more than one value system (such as a combination of pixels and ems), either use the longhand form or this method of grouping.

Figures 3.3 and 3.4 compare a portion of the document with the unstyled and styled margins.

Click To expand
Figure 3.3: A portion of the unstyled content within a browser window
Click To expand
Figure 3.4: Examining the styled margins

The next step is to create a rule for your default font face. Note that this font will often be overridden by other rules within your style sheet, but it’s still a good idea to put your body text font and related styles here. As you are probably aware from working with HTML, you can “stack” font family names. The first name in the stack will display if that font is available on your machine; if not, each one will be considered in turn. Using a default at the end, such as serif or sans-serif, means that if no prior font in the stack is located, the browser will display the closest font available.

body  {
     margin: 20px;
     font-family: "Trebuchet MS" Verdana, Arial, sans- 
     serif;
     font-size: 0.95em;
     line-height: 1.3em;
     font-style: normal;
}
Note 

 If you’re using a font name that contains any spaces, such as Trebuchet MS, you must put the name in quotations for it to be interpreted properly.

As with grouping margins, the family, size, line height, and style can similarly be grouped as follows:

font: normal 0.95em/1.3em "Trebuchet MS", Arial, Verdana,   
      sans-serif;

As with margins, font grouping order is critical. The font-weight and font-style properties must come before size measurement and family names. If you are using both font-size and line-height, the line-height value comes after the size, and their difference is denoted with a slash (/).

Note 

 For more details about using fonts in CSS, please see Chapter 4, “CSS Typography.”

Next, add a color for the text:

color: #333333;

and a color for the background:

background-color: #FFFFFF;

Listing 3.2 shows the now complete rule for the body style.

Listing 3.2: Styling the body Using an Element Selector
body  {
     margin: 20px;
     font: normal 0.95em/1.3em "Trebuchet MS", Arial,   
     Verdana, sans-serif;
     color: #333333;
     background-color: #FFFFFF;
}

You can now save your file and view the changes. Figure 3.5 shows a portion of the content style, which you can compare to the unstyled original.

Click To expand
Figure 3.5: Styling the body of with margins, fonts, line height, color, and background colot

Adding Style to Headers

In this instance, there are only two headers within the document tree. Typically, it’s wise to style at least levels 1–3. Here, you’ll work with the two levels in the document, because they are going to be distinctly different in their styling, and then add a third for later use.

For the h1, you’ll create a style that contains its own margin style, padding, and styled bottom border, as well as font styles. Remember that each element relates to a "box" within the browser space. The style for h1 in this example demonstrates how to use that box to style aspects of the element.

To style the header level 1, follow these steps:

  1. Begin with the selector and brackets for the declarations:

    h1  {
    
    }
  2. Add the margin styles. Here, I’m using negative values to pull the respective top, bottom, and left margins in closer than the default margin. That creates a tight effect but still leaves the right margin at 15 pixels:

    h1  {
         margin: -10px -10px 15px -10px;
    }
  3. Set the padding:

    h1  {
         margin: -10px -10px 15px -10px;
         padding: 0px 10px 5px 10px;
    }
  4. Now, add the font styles:

    h1  {
         margin: -10px -10px 15px -10px;
         padding: 0px 10px 5px 10px;
         font: 900 2em/1.1em Verdana, Arial, sans-serif;
    }
  5. Add the text color:

    h1  {
         margin: -10px -10px 15px -10px;
         padding: 0px 10px 5px 10px;
         font: 900 2em/1.1em Verdana, Arial, sans-serif;
         color: #aaaaaa;
    }
  6. Add the background color for the header block:

    h1  {
         margin: -10px -10px 15px -10px;
         padding: 0px 10px 5px 10px;
         font: 900 2em/1.1em Verdana, Arial, sans-serif;
         color: #aaaaaa;
         background-color: #eeeeee;
    }
    
  7. And finally, add the border style for the bottom border, which will be one pixel thick and of a “dashed” style:

    h1  {
         margin: -10px -10px 15px -10px;
         padding: 0px 10px 5px 10px;
         font: 900 2em/1.1em Verdana, Arial, sans-serif;
         color: #aaaaaa;
         background-color: #eeeeee;
         border-bottom: #bbbbbb 1px dashed;
    }
  8. Save your style sheet and preview the main document, chapter3.html, in your browser. Figure 3.6 shows the styling of the h1 header.

    Click To expand
    Figure 3.6: Using the element box to assign interesting style to a header

Listing 3.3 shows the entire sheet thus far.

Listing 3.3: The Style Sheet with body and header Level 1 Styles
body  {
     margin: 20px;
     font: normal 0.95em/1.3em "Trebuchet MS", Arial,
     Verdana, sans-serif;
     color: #333333;
     background-color: #FFFFFF;
}

h1  {
     margin: -10px -10px 15px -10px;
     padding: 0px 10px 5px 10px;
     font: 900 2em/1.1em Verdana, Arial, sans-serif;
     color: #aaaaaa;
     background-color: #eeeeee;
     border-bottom: #bbbbbb 1px dashed;
}

To style the h2 header, follow these steps:

  1. Underneath the rules for h1, use the element selector for h2:

    h2  {
    
    }
  2. Now, add the margins you’d like:

    h2 {
         margin: 0px 0px 5px 0px;
    }
  3. And add the padding:

    h2  {
         margin: 0px 0px 5px 0px;
         padding: 0px 0px 0px 0px;
    }
  4. Set the font properties:

    h2  {
         margin: 0px 0px 5px 0px;
         padding: 0px 0px 0px 0px;
         font: bold 1.2em Verdana, Arial, sans-serif;
    }
  5. Add the text color:

    h2  {
         margin: 0px 0px 5px 0px;
         padding: 0px 0px 0px 0px;
         font: bold 1.2em Verdana, Arial, sans-serif;
         color: #666699;
    }

Once you’re finished, save the changes. Figure 3.7 shows a comparison between the two header styles with the paragraph text temporarily removed.

Click To expand
Figure 3.7: Comparing the styles for h1 and h2 headers

At this point, you’d create additional styles for headers level 3, 4, 5, and 6 should you need them. Each rule should be written consecutively so as to continue following the document tree.

Listing 3.4 shows the style sheet as it should look now.

Listing 3.4:  WWW   The Style Sheet with body and header Levels Required for Your Content
body  {
     margin: 20px;
     font: normal 0.95em/1.3em "Trebuchet MS", Arial,
     Verdana, sans-serif;
     color: #333333;
     background-color: #FFFFFF;
}

h1  {
     margin: -10px -10px 15px -10px;
     padding: 0px 10px 5px 10px;
     font: 900 2em/1.1em Verdana, Arial, sans-serif;
     color: #aaaaaa;
     background-color: #eeeeee;
     border-bottom: #bbbbbb 1px dashed;

h2  {
     margin: 0px 0px 5px 0px;
     padding: 0px 0px 0px 0px;
     font: bold 1.2em Verdana, Arial, sans-serif;
     color: #666699;
}

Figure 3.8 shows a portion of the page that contains all of the styles you’ve written so far.

Click To expand
Figure 3.8: A portion of the document as it looks with the current style sheet in place

Creating the Paragraph Style

Creating styles for the paragraph is as straightforward as creating them for the body and headers. In this case, you’re going to add a margin and font styles, as follows:

p  {
     margin: 0px 0px 5px 0px;
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana,  
     Arial, sans-serif;
}

Styling the Images

Images are unique in that they can be positioned on a page in a variety of ways. In this chapter, the concern is mostly to ensure that there is no border around the images. To do this, add the following rule:

img  {
     border: none;
}

Styling the Link

To style the link, first, you’ll create a style for the main link, and then you’ll move on to the next section, where you will create link rules using pseudo class selectors.

Add the following CSS rule to your style sheet:

a:link  {
     color: #0099CC; 
     background-color: transparent;
     text-decoration: none;
}

This styles any standard link as blue in color, with a transparent background and no text decoration.

Listing 3.5 shows the progress of the style sheet.

Listing 3.5:  WWW  Style with Rules for the Image and Primary Link Selectors
body  {
     margin: 20px;
     font: normal 0.95em/1.3em "Trebuchet MS", Arial, 
     Verdana, sans-serif;
     color: #333333;
     background-color: #FFFFFF;
}

h1  {
     margin: -10px -10px 15px -10px;
     padding: 0px 10px 5px 10px;
     font: 900 2em/1.1em Verdana, Arial, sans-serif;
     color: #aaaaaa;
     background-color: #eeeeee;
     border-bottom: #bbbbbb 1px dashed;
}

h2  {
     margin: 0px 0px 5px 0px;
     padding: 0px 0px 0px 0px;
     font: bold 1.2em Verdana, Arial, sans-serif;
     color: #666699;
     background-color: transparent;
}

p  {
     margin: 0px 0px 5px 0px;
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana,
     Arial, sans-serif;
}
img  {
     border: none;
}

a:link  {
     color: #0099CC; 
     background-color: transparent;
     text-decoration: none;
}

Finally, you’ll create styles for the unordered list and list items, as follows:

ul  {
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana, 
     Arial, sans-serif; 
}

li  {
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana, 
     Arial, sans-serif; 
}
Note 

 Setting styles only for the list item and not the ul (or ol) will sometimes cause the list items not to be rendered in the style you’ve created. Therefore, it’s wise to set styles for both the list type and list item, as shown in Listing 3.5.

Creating Link Rules with Pseudo Class Selectors

No doubt you’ve seen links styled using hover effects created with CSS. In this section, you’ll add styles for visited and active links, as well as creating a hover effect. Link styles, other than the element selector, require you to use pseudo class selectors.

Enter the visited link rules after the anchor element within your style sheet. Then, follow these steps:

  1. Begin with the visited link pseudo class selector:

    a:visited  {
    
    }
  2. Add the first rule, which in this case is the link color:

    a:visited  {
         color: #0077aa;
    }
    
  3. Add the second rule, which styles the background as transparent:

    a:visited  {
         color: #0077aa; 
         background-color: transparent;
    }
  4. Finish up with the text-decoration property, in this case set to none. This ensures that no underlines will appear beneath the link:

    a:visited  {
         color: #0077aa;
         background-color: transparent;
         text-decoration: none;
    }
  5. Save your changes before continuing to add the styles for the active link. Begin with the active link pseudo selector:

    a:active  {
    
    }
  6. Add the color:

    a:active  {
         color: #0099cc;
    }
  7. Add the background color, which in this case is also set to transparent:

    a:active  {
         color: #0099cc;
         background-color: transparent;
    }
  8. Finish up by adding the text decoration property and style:

    a:active  {
         color: #09c;
         background-color: transparent;
         text-decoration: none;
    }

Your style sheet now contains all the common link styles: normal, visited, and active. The next step is to create a hover style, so that as a site visitor’s mouse passes over the link, the link will change. The style created here causes a gray box to appear as the mouse passes the link. The box is created because all the other link examples have their background set to transparent rather than defining a color. In the case of this hover rule, the background color is defined:

a:hover  {
     color: #0077cc;
     background-color: #eeeeee;
     text-decoration: none;
}

Figure 3.9 shows a plain link, and then the hover style.

Click To expand
Figure 3.9: Using pseudo classes to apply a hover style to a link

Listing 3.6 shows the progress of the style sheet. Note the location and order of the pseudo elements. This is important for inheritance and proper functioning of the link styles.

Listing 3.6:  WWW  Style Sheet with Element Selectors and Pseudo Class Selectors in Use
body  {
     margin: 20px;
     font: normal 0.95em/1.3em "Trebuchet MS", Arial, 
     Verdana, sans-serif;
     color: #333333;
     background-color: #FFFFFF;
}

h1  {
     margin: -10px -10px 15px -10px;
     padding: 0px 10px 5px 10px;
     font: 900 2em/1.1em Verdana, Arial, sans-serif;
     color: #aaaaaa;
     background-color: #eeeeee;
     border-bottom: #bbbbbb 1px dashed;
}
h2  {
     margin: 0px 0px 5px 0px;
     padding: 0px 0px 0px 0px;
     font: bold 1.2em Verdana, Arial, sans-serif;
     color: #666699;
     background-color: transparent;
}

p  {
     margin: 0px 0px 5px 0px;
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana, 
     Arial, sans-serif;
}

img  {
     border: none;
}

a:link  {
     color: #0099CC; 
     background-color: transparent;
     text-decoration: none;
}
a:visited  {
     color: #0077aa;
     background-color: transparent;
     text-decoration: none;
}

a:active  {
     color: #0099cc;
     background-color: transparent;
     text-decoration: none;
}

a:hover  {
     color: #0077cc;
     background-color: #eeeeee;
     text-decoration: none;
}
ul  {
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana,  
     Arial, sans-serif; 
}

li  {
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana, 
     Arial, sans-serif; 
}

Adding Class Selectors

Now that you’ve got all of the primary elements styled, you can begin creating class selectors to more broadly manage certain features within the document.

In this case, you’ll add one class called small and another called smallColor to enable you to style text within the document as smaller, or smaller and colored differently than other styles within the document:

.small  {
     font: 0.75em "Trebuchet MS", Verdana, Arial, sans-
     serif;

}

.smallColor  {
     font: 0.75em "Trebuchet MS", Verdana, Arial, sans-
     serif;
     color: blue;
}

 Tip  When authoring class names, it’s a good idea to make them descriptive but not necessarily specific. For example, I could have named the smallColor class smallBlue. Later on, however, if I wanted to change the actual color of the class to purple, there’d be a significant discrepancy in any document, site-wide, where class="smallBlue" appears. Sticking to descriptive rather than specific names is a much more efficient authoring practice.

Remember, you can add a class to any element using the class attribute. For example, if you wanted an entire paragraph within the document to be italicized, you’d style that paragraph as follows:

<p class="small">If it weren’t for the Burger King, the Holiday Inn, and 
the massive Glaxo/Smith Klein complex, I really might have confused it all for New Jersey.</p>

If you’d like to have only one word or series of words within a paragraph affected, you can use the span element, as follows:

<p>If it weren’t for the Burger King, the Holiday Inn, and the massive
Glaxo/Smith Klein complex, I really <span class="small">might</span> have confused it <span class="smallColor">all for New Jersey.</p>

Figure 3.10 shows these two samples.

Click To expand
Figure 3.10: Using classes to apply style

Listing 3.7 shows the completed style sheet for this chapter, and Figure 3.11 shows the fully styled document that results.

Listing 3.7: The Completed Style Sheet
body  {
     margin: 20px;
     font: normal 0.95em/1.3em "Trebuchet MS", Arial,
     Verdana, sans-serif;
     color: #333333;
     background-color: #FFFFFF;
}

h1  {
     margin: -10px -10px 15px -10px;
     padding: 0px 10px 5px 10px;
     font: 900 2em/1.1em Verdana, Arial, sans-serif;
     color: #aaaaaa;
     background-color: #eeeeee;
     border-bottom: #bbbbbb 1px dashed;
}
h2  {
     margin: 0px 0px 5px 0px;
     padding: 0px 0px 0px 0px;
     font: bold 1.2em Verdana, Arial, sans-serif;
     color: #666699;
     background-color: transparent;
}

p  {
     margin: 0px 0px 5px 0px;
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana, 
     Arial, sans-serif;
}

img  {
     border: none;
}

a:link  {
     color: #0099CC; 
     background-color: transparent;
     text-decoration: none;
}
a:visited  {
     color: #0077aa;
     background-color: transparent;
     text-decoration: none;
}

a:active  {
     color: #0099cc;
     background-color: transparent;
     text-decoration: none;
}

a:hover  {
     color: #0077cc;
     background-color: #eeeeee;
     text-decoration: none;
}
ul  {
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana, 
     Arial, sans-serif; 
}

li  {
     font: normal 0.95em/1.3em "Trebuchet MS", Verdana, 
     Arial, sans-serif; 
}

.bold  { 
     font-weight: bold;
}

.italic  { 
     font-style: italic;
}

The result is a well-ordered style sheet that takes its structure directly from the documents for which it’s being created.

Click To expand
Figure 3.11: The final style sheet as applied to the document
Table of Contents
Previous Next