Stylesheets deal mostly with fonts and other text properties, and this applies to most media, so that's where I'll start.
Text can be controlled through the following groups of properties:
The other properties are:
The font properties describe how your basic text is supposed to look. The properties that fall under this group are:
Property: | font-family |
---|---|
Default value: | Depends on browser. |
Inherited: | Yes |
Do you want your font to be in Courier New, Times New Roman, Arial, Verdana, or just a generic font? That's what font-family deals with.
There are several generic types of fonts. They are:
sans-serifmeans
without serifs). They're usually easier to read on a screen.
You'll see examples of these when I talk about specific fonts families.
You can also identify the actual font you want. Below is a page with examples of the various types of fonts, with the names of specific fonts taking precedence. Those fonts are (in order):
Their appearance is shown below:
So, I specify them like this:
The CSS resizing the text has been omitted.
There is a slight catch: This font will only show up for the user if it is on the computer the user is viewing. Thus, it is a good idea to have a comma-separated list of alternatives; if one fails, the next alternative can be used.
The rule for #li_5
tells the browser the following:
Euclid Fraktur.
Euclid Fraktur, use
Old English Text MT, a more common font.
Old English Text MT, use the brower's default fantasy font.
If the browser does not have a default serif font, you're using a primitive browser indeed:
And now you know what things look like in the text-only browser Lynx.
Property: | font-style |
---|---|
Default value: | normal |
Inherited: | Yes |
This specifies whether or not a font is in italics. There are three values for it:
Property: | font-variant |
---|---|
Default value: | normal |
Inherited: | Yes |
Font-variant is rarely used, but this allows you to display lower-case letters as small capitals. Its values are:
Property: | font-weight |
---|---|
Default value: | normal |
Inherited: | Yes |
Font-weight allows you to highlight words by making them bold. It has three sets of values:
lighterand
bolder. Their effects depend on the weight of the font inherited from the parent element.
normaland
bold.
Property: | font-size |
---|---|
Default value: | medium |
Inherited: | Yes |
Easy enough to explain; font-size controls how big the text is. I explained this way back in Inline Elements, but we'll go over it again.
There are several ways to describe font-size:
There are seven absolute font size keywords, whose names are self-explanatory:
Below is a page displaying these sizes.
There are two relative font size keywords, whose names are also self-explanatory:
Do be warned: these can add up, as shown in the next example.
The code of the above example is frighteningly simple:
Heaven help you if you use the same code, but trade the p
elements for h1
...
As stated earlier, you can use the units of length explained in CSS Value Types. However, it is wise to be careful using relative such as em
and ex
, since they can multiply even faster than the larger
value.
The last way to set a font's size is to set it as a percentage of the inherited font's size. This is a number followed by a percent sign (%
)
Property: | font |
---|---|
Inherited: | Yes |
The font
property can be used as a shorthand means of dictating the font. It can set the following properties:
*More on this one later. Suffice to say for now, it controls how far apart a line of text is from the next.
The font
property can also be set using one of the system fonts, but I'm not going to get into those since they are defined by the individual user's operating system and the browser has no control over those fonts.
The rules of this get a little hairy, so bear with me.
font-size
and font-family
, in that order.
font:12pt serif;
line-height
property is optional. If you define it, it must follow font-size
, and the two values must be separated with a slash. (The font-family
property must always be at the end.)
font:12pt/18pt serif;
font-weight
, font-variant
, and font-style
properties are all optional, and their values may be in any order. But, they must appear before font-size
.
font:12pt/18pt serif;
font:bold 12pt serif;
font:bold small-caps 12pt serif;
font:italics small-caps 12pt serif;
font:small-caps bold italics 12pt serif;
The text properties don't control the text's appearance, but adjust the text as a whole. The text properties are:
Property: | text-indent |
---|---|
Default value: | 0 |
Inherited: | Yes |
The text-indent
property allows you to indent the first line of a chunk of text, such as the text in these paragraphs. Its value can be one of two things:
If you want the first line to be hanging, then it's permissible to use a negative number, which will cause the first line to stick out instead. To illustrate this, I have below a page with the following stylesheet:
The first paragraph is to be indented 5em. The second paragraph gets no styling. The third paragraph gets a left-side margin of 5em but has an indent
of negative 5em—in other words, a hanging first line or exdent.
Property: | text-align |
---|---|
Default value: | The content is left-aligned if the dir attribute is ltr or right aligned if the dir attribute is rtl . The actual value for this does not have a name. |
Inherited: | Yes |
The text-align
property allows you to align text to the left margin, right margin, make both sides of the block of text even, or center the text. The values are (respectively):
To illustrate these, I have below a page with the following stylesheet, demonstrating the above values in order:
Don't worry about reading the text; it's just the Lorem Ipsum
text repeated 4 times.
You can see that the top paragraph is straight on the left side, the second paragraph is straight on the right side, the third is straight on both sides (except for the last line), and the fourth is centered.
Property: | text-decoration |
---|---|
Default value: | none |
Inherited: | Technically no, but all text in the element is decorated according to the declaration unless otherwise specified. |
The text-decoration
property allows you to decorate your text with lines over, through, and/or under the text. You can also make text blink, but you should avoid using blinking text on your webpage—unless your webpage is about whether or not Schrödinger's cat is dead. Nonetheless, here are the properties:
The word blink
:
That's essentially what text-decoration:blink;
does. The stylesheet looks like this:
An interesting usage of this is you can apply more than one property to this. For example, if you had the declaration text-decoration: overline line-through underline blink;
, the affects of all four would apply—you'd get three lines and your text would blink.
The declaration text-decoration:none;
isn't all that common, but it is used when a website designer doesn't want hyperlinks underlined.
Property: | text-transform |
---|---|
Default value: | none |
Inherited: | Yes |
This last deals with capitilization. You can set it so that your letters are all capitals, all lowercase, only the first characters are capitalized, or to stick to what was actually typed. The values are (in order):
Below is a page that displays each. Each list item contains the exact same text.
And the stylesheet:
As you can see, the first changes everything to upper case, the second to lower case, the third changes the first letter of each word to upper case and leaves everything else alone, and the fourth keeps the text as it was typed.
Property: | color |
---|---|
Default value: | Depends on browser. |
Inherited: | Yes |
The color
property always refers to font colour. The codes for colour are explained in CSS Value Types.
The stylesheet:
Property: | line-height |
---|---|
Default value: | normal |
Inherited: | Yes |
Line-height controls how much space each line of text takes up. It can be determined in several different ways:
normal.
When this is set as a simple number, this spaces the lines in relation to their size. The calculation is simple: Font size multiplied by the line-height value. For example, if you wanted to have lines double-spaced, you would write line-height:2;
. This then doubles the line-height.
This allows you to explicitly state the line-height regardless of font size—unless you use units such as em
or ex
, which depend on the font size themselves.
This allows you to set the line-height as a percentage of the font-size.
The following line-height rules all have the same result:
Please notice that for the last paragraph, I explicitly set the font-size and the line-height. While I could have omitted the font-size
declaration with no effect, I wanted to emphasize that the line-height
declaration in that case was completely independant of font size, and that it is double-spaced only because the line height happens to be exactly twice that of the font size.
Properties |
|
---|---|
Default value: | normal (for both) |
Inherited: | Yes (for both) |
The letter-spacing property allows you to control how far apart the individual letters are, while word-spacing controls how far apart the words are.
If you look at the last rule, you'll see that this can actually be set to a negative measurement to make text scrunched.
There are other properties that deal with the webpage text as well. If you want to know what they are, the World Wide Web Consortium has excellent information on them.