6. Text and Headings with CSSOne of the beauties of style sheets is the way they help maintain consistency. In this lesson we are going to set up a number of rules to ensure that text has a consistent appearance across our site. Kinds of TextUsually there are two major kinds of text in a document, the main text, and headings. There are often also other pieces of information, such as the labels for images, the list of ingredients in a cookbook, and so on. In this lesson we will restrict ourselves to the main text. In a later lesson we are going to look at how style sheets help work with more specific kinds of information on web pages. When designing a publication (print or web based) the appearance of text is fundamentally important. I don't have much to say about that issue (though it is a very worthy, and overlooked one) here. The Body and HeadingsIn print based publications, all of the main text of a document is in the same font, while headings either share this same font, or all share a different font. An important principle is to minimize the fuss, that is the use of different fonts for their own sake. Our first step is to create a rule that assigns a particular font (size, etc.) to the main text of a page. Fortunately, we can avoid that tedious, and error prone procedure, because of an important feature of web pages, and style sheets, called inheritance. If you create a rule which selects a particular type of element, and applies properties to it, then any element that is contained inside a selected element inherits many of these properties. So, once again, we are going to create a rule that selects the body, though this time, we are going to apply text properties to it. Font and SizeAt this stage, the most important text properties are the font, and the size of the text. These are specified by the FontThe font-family property specifies one or more fonts for an element, using the family name of the font. Each font name is separated by a comma. The browser will use the first font in the list that is installed on its computer. Put together, we have the following rule
Exercise 1Now it's your turn. Create a similar rule which specifies the fonts you want apply to the text in the body of your page. If you prefer serif fonts (like Times, or Palatino) then the final generic font family should be SizeSize is specified using the For these tutorials, I'm going to stick to relative values, in this case using percentages. Specifying a font-size in percent terms specifies that the size should be that percentage of the font size of the parent element. To specify a size of say 150%, the property is
Add this to your rule for specifying the font family of your text, and you have a rule for specifying the standard text for your page. We now have two rules with Time to preview again, after saving your style sheet. Has the text done what you told it? If so, well done. If not, check that syntax. HeadingsBecause of inheritance, the rule we just created will apply the same style to headings, as well as paragraphs, and the other elements on a page. What we want now is to specify a different appearance for headings. First we want to create the selector for each heading. This means that we will have several rules, one for each heading level ( To create a group of selectors, you simply list the individual selectors separating each with a comma. pop quizThe selector group for all levels of heading Exercise 2To complete this lesson, we want to create a rule, using the above selector, and apply the same font to each heading level. Answer at the end of the lesson. Once more time to take a progress check. How's the page coming along? Next>In this lesson we've covered a lot really. We've seen
We've also seen how to add comments to a style sheet (did you notice?) Next we'll look at all the wonderful things you can do with links using style sheets. Answer to Exercise 2Your rules should look something like this
(C)1997-2001 Western Civilisation Pty. Ltd.
|