Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Basic Properties: Font Styles

CSS support in browsers began with simple text style properties, so it's fitting that we begin our investigation into CSS properties with these foundational properties.

color

The color property specifies the text color for selected elements.

The property takes a single color value. Color values, which are also used by a number of other CSS properties, come in a number of forms.

The simplest color values are color keywords, of which there are two kinds: seventeen basic color values like red and blue, and a larger number of system colors, including ButtonFace, ButtonText, and other values. In addition, all browsers have long supported a large palette of 140 color keywords from the X11 color chart, and though these are not part of CSS1 or CSS2, they are part of the draft versions of CSS3.

The seventeen common colors are aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, red, silver, teal, white, and yellow. A full list of the X11 colors can be found at en.wikipedia.org/wiki/X11_color_names.

System colors don't set text to a specific color—instead, they let developers match the style of their own elements (like buttons) to the current system colors as defined in each user's operation system preferences, thus harmonizing their interfaces with the system's.

Color in CSS can also be specified in a number of other ways, the most common of which is hexadecimal RGB colors. These have the form #RRGGBB, where RR is a red hexadecimal color value, GG is a green hexadecimal color value, and BB is a blue hexadecimal color value. You won't generally need to manually determine the color value, as most design tools do this part for you, but now you know what the code means. When all three colors have their two digits or letters repeated (for example, #FF2277), the value can be truncated using the form #RGB (in this case, #F27). Note that this truncation can occur not when the second digit is a zero, but when the digit or letter for each color value is repeated. One very common CSS error is leaving off the # at the beginning of the hex value; don't do this. The third and fourth way to specify color values is via decimal and percentage RGB colors. Both of these methods have the basic form rgb(color value), with the color value inside the parentheses. In decimal RGB, a number between 0 and 255 is used for each color, with these values separated by a comma. In percentage RGB, a percentage value from 0% to 100% is used. So, orange, which is #FFA500 in hexadecimal, would be rgb(255, 165, 0) in decimal RGB and rgb(100%, 65%, 0%) in percentage RGB.

Let's cut to the chase and give our body a color of orange:

body {
  color: orange;
}

We've created our first statement. Easy, right?

font-family

Next, we'll give our headings a font. In CSS, we use the font-family property for this. This property takes one or more font names, separated by a comma. Why specify more than one? In CSS, we can generally only use the fonts already installed on our users' systems. There are exceptions to this rule, but web typography is sufficiently complex to deserve its own chapter (Chapter 15), so we'll stick with basics for now. The fonts on users' computers will depend on their operating systems, what fonts they have installed themselves, what software (such as Microsoft Office) they have installed, and several other factors. If we could only specify a single font, we'd really be stuck with the tiny subset of fonts that all platforms have in common. Luckily, we can specify more than one, so we can take advantage of a wider range of fonts. It's usually best to use a set of fonts that are similar to each other—by which I mean not simply fonts that look alike, but that have similar metrics—so that the same characters will occupy a similar amount of space in the various fonts we select.

A common pair of fonts with very similar metrics is Helvetica and Arial. One or the other of these two fonts are found on most browsing platforms, so specifying both together makes a lot of sense. We'd do this with the font-family property like this:

font-family: Helvetica, Arial
               

It's also a good idea to use a fallback generic font family, of which CSS provides five:

  • serif
  • sans-serif
  • monospace
  • cursive
  • fantasy

Here's how this all works together. When a browser sees the font-family property, it takes the list of font names (which are separated by commas) and looks to see whether the user's system has a font that matches the first name. If so, it uses this font. If not, it goes to the next font name, and sees whether it has a match for this one. If so, it uses that; otherwise, it continues along the list. You can name as many alternate fonts as you like. If none of the names match a font on the user's system, the browser takes the generic family name and uses either its default font for that family or the user's preferred font for that family (most browsers let you set a preferred font for generic families).

When a font has one or more spaces in its name—like Times New Roman—the name of the font must be wrapped in single or double quotation marks. For example:

font-family: Times, "Times New Roman", serif
               

Common mistakes with the font property include using quotation marks with font names that don't have spaces, using quotation marks with generic font family names (particularly sans-serif), and not using quotation marks with font names that do have spaces.

font-size

You can set the size of text with the font-size property. font-size is the first property we've seen that takes more kinds of values: length values and percentage values.

Length Values

A length value is a numeric value associated with a unit of measurement. In CSS, with very rare exceptions, when a property takes a numeric value, that value includes a unit. In HTML, unitless numeric values are assumed to be px values, but in CSS, a length value without a unit is almost always considered an error; there are one or two exceptions, which we'll discuss shortly. This is one of the most common errors CSS developers make, and it's so common in part because some browsers wrongly treat numeric values with no units as pixel values. There are several different units, the most common being pixels (px) and ems (em). Percentage values, though they're not strictly length values, are very similar to length values; the trick is that percentage values don't always refer to the same thing from property to property, as we'll see shortly.

Relative and Absolute Values

There are two types of length value—absolute values, which are most commonly length values with a px unit, and relative values, which are most commonly values with an em unit. Percentage values are effectively relative values as well.

Absolute length values are (usually) fixed, regardless of factors like the user's text zoom, window size, and so on. Relative values allow layouts, text size, and other CSS-applied styles to adapt to the user's preferences and setup, so they're generally preferred. We'll see plenty of examples of the benefits of relative units throughout this book, but some very common examples include:

  • Specifying font-size in em units or percentages. This allows for text to grow and shrink as users increase and decrease their font size (this is less of a problem today, but older browsers typically fixed the size of text so that it couldn't be zoomed by the user if the unit for font-size was pixels).
  • Specifying the width of an element in ems. This means that the element remains more or less the same number of characters wide regardless of how big or small the text is zoomed. This can be especially helpful for legibility, where particularly long or short lines of text can very much impact the ease of reading.

It's possible to set font-size in pixels, but it's much more preferable to set it in either percentages or ems. We could set our headings to have different font sizes using statements like these:

h1 {font-size: 2em}
h2 {font-size: 1.6em}
h3 {font-size: 1.4em}
h4 {font-size: 1.2em}

This means that the text of h1 elements will be twice the size that it would otherwise be, h2 elements 1.6 times the size it would otherwise be, and so on. We could similarly use equivalent percentages to do this:

h1 {font-size: 200%}
h2 {font-size: 160%}
h3 {font-size: 140%}
h4 {font-size: 120%}

Much less common is the use of keywords to specify font sizes. It is possible to use keywords like xx-small, small, and large, but it's usually best to avoid these, as there's no consensus among browsers—or in the CSS standard—as to exactly what these seven sizes should be in relation to one another.

font-weight

The font-weight property, which controls how bold or heavy a font should be, can be specified in two ways: we can use the keywords bold and normal, or we can use one of nine weights from 100 to 900. It's rare that fonts have a wide range of weights, and so values of 100 to 400 map typically to the value of normal, and 500 and up are considered bold.

font-style

One of the challenges of becoming proficient in CSS is learning and remembering all the different possible property names, which don't always align with what we commonly call these properties outside of CSS. A perfect example is how we specify whether an element's text should be italicized. In CSS, we use the font-style property, with three possible keyword values—italic (not italics), normal, and oblique. We can essentially consider italic and oblique to be the same thing.

text-decoration

With the text-decoration property, we can add or change text strikethrough, underline, the far less commonly used overline, and the mercifully unused and largely unsupported blink decorations. The text-decoration takes one or more space-separated (not comma-separated) keyword values:

  • underline
  • overline
  • line-through
  • blink
  • none (which removes, for example, the default underline most browsers apply to anchors)

Putting these common properties together, we might style a heading of level 1 like this:

h1{
  font-family: "Times New Roman", Times, serif;
  font-weight: bold;
  font-style: italic;
  text-decoration: underline;
  font-size: 2em;
}
  • + Share This
  • 🔖 Save To Your Account