A CSS Style Primer


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

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

  • Layout properties

  • Formatting properties

Layout properties consist of properties that impact the positioning of elements on a web page. For example, layout properties allow you to control the width, height, margin, padding, and alignment of content, and even go so far as to allow you to place content at exact positions on a page. This is something impossible to carry out in HTML code alone!

Layout Properties

CSS layout properties are used to 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. There are four possible values for the display property:

  • 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.

It's easier to understand the display property if you visualize each element on a web page occupying a rectangular area when displayedthe 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:

 display:block; 


By the Way

The display property relies on a concept known as relative positioning, which means that elements are positioned relative to the location of other elements on a page. CSS also supports absolute positioning, which allows you to place an element at an exact location on a page independent of other elements. You'll learn more about both of these types of positioning in Hour 14, "Using Style Sheets for Page Layout."


You control the size of the rectangular area for an element with the width and height properties. Like 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.

  • px Pixels.

  • pt Points.

You can mix and match units however you choose within a style sheet, 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 or pixels for dimensions. Following is an example of setting the width of an element using pixel units:

 width:200px; 


Formatting Properties

CSS formatting properties are used 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 is used to establish a visible boundary around an element with a box or partial box. 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 is used to establish the width of the border edge. It is often expressed in pixels, as the following code demonstrates:

 border-width:5px; 


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

 border-color:blue; border-style:dotted; 


The border-style property can be set to any of the following values:

  • 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.

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. The most common border styles are the solid and double styles.

By the Way

The exception to the default border-style of none is when an image is placed within an <a> tag so that it serves as a linked image. In that case, a solid border is automatically set by default. That's why you often see linked images with the style border-style:none, which turns off the automatic border.


The border-left, border-right, border-top, and border-bottom properties allow 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:

 color:black; background-color:orange; 


You can also assign custom colors to these properties by specifying the colors in hexadecimal or as RGB (Red Green Blue) decimal values, just as you do in HTML:

 background-color:#999999; color:rgb(0,0,255); 


By the Way

Hexadecimal colors are covered in more detail in Hour 9, "Custom Backgrounds and Colors."


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:

 text-align:center; text-indent:12px; 


After you have an element properly aligned and indented, you might be interested in setting its font. The following font properties are used to set the various parameters associated 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 (light, medium, bold, 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: medium; 


Now that you know a whole lot more about style properties and how they work, take a look back at Listing 12.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. Each of these font properties can be specified separately with font-family:, font-size:, font-weight:bold, and font-style:italic if you prefer.

  • 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 9 for more details).

  • text-decoration Useful for turning link underlining offsimply set it to none. The values of underline, italic, and line-through are also supported. The application of styles to links is covered in more detail in the next hour, "Digging Deeper into Style Sheet Formatting."

  • text-align Aligns text to the left, right, or center, along with justifying the text with a value of justify.

  • text-indent Indents beyond the left margin by a specified amount. You can say how far to indent in units (px, in, cm, mm, pt, pc), or you can specify a percentage of the page width (such as 20%).

  • margin Sets the left and right margins to the same value, which can be in measurement units or a percentage of the page width. Use margin-left and margin-right if you want to set the left and right margins independently, and margin-top to set the top margin. You'll learn more about these style properties in the Hour 14.

  • background Places a solid color or image behind text, with either a color or url(address), where address points to a background image tile. Note that this can be assigned not only to the <body> tag, but also to any tag or span of text to highlight an area on a page. The background-color and background-image style properties can be used to achieve the same effect.




SAMS Teach Yourself HTML and CSS in 24 Hours
Sams Teach Yourself HTML and CSS in 24 Hours (7th Edition)
ISBN: 0672328410
EAN: 2147483647
Year: 2005
Pages: 345

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net