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 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 PropertiesCSS 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:
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;
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:
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 PropertiesCSS 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:
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:
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.
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);
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:
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:
|