Home > Articles > Web Development > HTML/CSS

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

A CSS Style Primer

You now have a basic knowledge of CSS stylesheets and how they are based on style rules that describe the appearance of information in web pages. The next few sections of this hour provide a quick overview of some of the most important style properties and allow you to get started using CSS in your own stylesheets.

CSS includes various style properties that are used to control fonts, colors, alignment, and margins, to name just a few. The style properties in CSS can be generally grouped into two major categories:

  • Layout properties, which consist of properties that affect the positioning of elements on a web page, such as margins, padding, and alignment.
  • Formatting properties, which consist of properties that affect the visual display of elements within a website, such as the font type, size, and color.

Basic Layout Properties

CSS layout properties determine how content is placed on a web page. One of the most important layout properties is the display property, which describes how an element is displayed with respect to other elements. The display property has four basic values:

  • block—The element is displayed on a new line, as in a new paragraph.
  • list-item—The element is displayed on a new line with a list-item mark (bullet) next to it.
  • inline—The element is displayed inline with the current paragraph.
  • none—The element is not displayed; it is hidden.

Understanding the display property is easier if you visualize each element on a web page occupying a rectangular area when displayed—the display property controls the manner in which this rectangular area is displayed. For example, the block value results in the element being placed on a new line by itself, whereas the inline value places the element next to the content just before it. The display property is one of the few style properties that can be applied in most style rules. Following is an example of how to set the display property:


You control the size of the rectangular area for an element with the width and height properties. As with many size-related CSS properties, width and height property values can be specified in several different units of measurement:

  • in—Inches
  • cm—Centimeters
  • mm—Millimeters
  • %—Percentage
  • px—Pixels
  • pt—Points

You can mix and match units however you choose within a stylesheet, but it’s generally a good idea to be consistent across a set of similar style properties. For example, you might want to stick with points for font properties and pixels for dimensions. Following is an example of setting the width of an element using pixel units:

width: 200px;

Basic Formatting Properties

CSS formatting properties to control the appearance of content on a web page, as opposed to controlling the physical positioning of the content. One of the most popular formatting properties is the border property, which establishes a visible boundary around an element with a box or partial box. Note that a border is always present in that space is always left for it, but the border does not appear in a way that you can see unless you give it properties that make it visible (like a color). The following border properties provide a means of describing the borders of an element:

  • border-width—The width of the border edge
  • border-color—The color of the border edge
  • border-style—The style of the border edge
  • border-left—The left side of the border
  • border-right—The right side of the border
  • border-top—The top of the border
  • border-bottom—The bottom of the border
  • border—All the border sides

The border-width property establishes the width of the border edge. It is often expressed in pixels, as the following code demonstrates:


Not surprisingly, the border-color and border-style properties set the border color and style. Following is an example of how these two properties are set:


The border-style property can be set to any of the following basic values (you learn about some more advanced border tricks later in this book):

  • solid—A single-line border
  • double—A double-line border
  • dashed—A dashed border
  • dotted—A dotted border
  • groove—A border with a groove appearance
  • ridge—A border with a ridge appearance
  • inset—A border with an inset appearance
  • outset—A border with an outset appearance
  • none—No border
  • hidden—Effectively the same as none

The default value of the border-style property is none, which is why elements don’t have a border unless you set the border property to a different style. Although solid is the most common border style, you will also see the other styles in use.

The border-left, border-right, border-top, and border-bottom properties enable you to set the border for each side of an element individually. If you want a border to appear the same on all four sides, you can use the single border property by itself, which expects the following styles separated by a space: border-width, border-style, and border-color. Following is an example of using the border property to set a border that consists of two (double) red lines that are a total of 10 pixels in width:

border:10px double red;

Whereas the color of an element’s border is set with the border-color property, the color of the inner region of an element is set using the color and background-color properties. The color property sets the color of text in an element (foreground), and the background-color property sets the color of the background behind the text. Following is an example of setting both color properties to predefined colors:


You can also assign custom colors to these properties by specifying the colors in hexadecimal (covered in more detail in Hour 7, “Working with Colors and Borders”) or as RGB (Red Green Blue) decimal values:


You can also control the alignment and indentation of web page content without too much trouble. This is accomplished with the text-align and text-indent properties, as the following code demonstrates:


When you have an element properly aligned and indented, you might be interested in setting its font. The following basic font properties set the various parameters associated with fonts (you’ll learn about some more advanced font usage in Hour 6, “Working with Fonts”):

  • font-family—The family of the font
  • font-size—The size of the font
  • font-style—The style of the font (normal or italic)
  • font-weight—The weight of the font (normal, lighter, bold, bolder, and so on)

The font-family property specifies a prioritized list of font family names. A prioritized list is used instead of a single value to provide alternatives in case a font isn’t available on a given system. The font-size property specifies the size of the font using a unit of measurement, usually points. Finally, the font-style property sets the style of the font, and the font-weight property sets the weight of the font. Following is an example of setting these font properties:

font-family: Arial, sans-serif;
font-size: 36pt;
font-style: italic;
font-weight: normal;

Now that you know a whole lot more about style properties and how they work, refer back at Listing 3.1 and see whether it makes a bit more sense. Here’s a recap of the style properties used in that style sheet, which you can use as a guide for understanding how it works:

  • font—Lets you set many font properties at once. You can specify a list of font names separated by commas; if the first is not available, the next is tried, and so on. You can also include the words bold and/or italic and a font size. Alternatively, you can set each of these font properties separately with font-family, font-size, font-weight, and font-style.
  • line-height—Also known in the publishing world as leading. This sets the height of each line of text, usually in points.
  • color—Sets the text color using the standard color names or hexadecimal color codes (see Hour 7 for more details).
  • text-decoration—Useful for turning off link underlining—simply set it to none. The values of underline, italic, and line-through are also supported. Hour 8, “Using External and Internal Links,” covers applying styles to links in more detail.
  • text-align—Aligns text to the left, right, or center, along with justifying the text with a value of justify.
  • padding—Adds padding to the left, right, top, and bottom of an element; this padding can be in measurement units or a percentage of the page width. Use padding-left and padding-right if you want to add padding to the left and right of the element independently. Use padding-top or padding-bottom to add padding to the top or bottom of the element, as appropriate. You learn more about these style properties in Hour 13, “Working with Margins, Padding, Alignment, and Floating,” and Hour 14, “Understanding the CSS Box Model and Positioning.”
  • + Share This
  • 🔖 Save To Your Account