Home > Articles

Cascading Style Sheets

This chapter is from the book

In this Chapter

  • A Brief Introduction to Styles
  • Designing with CSS
  • Style Properties
  • Working with CSS Styles
  • Editing CSS in Code View

The cascading style sheets (CSS) language is a way to describe the appearance of Web pages by assigning styles to specific HTML tags and portions of the page. These styles allow a designer greater range of presentational effects than can be achieved by using only HTML styles. Dreamweaver MX 2004 makes it easy to edit and apply style sheets to new or existing Web documents.

If you're already familiar with CSS, you can skip over the following introduction and go directly to "Designing with CSS" to use Dreamweaver MX 2004 to build Web pages using styles.

A Brief Introduction to Styles

The font face of a line of text, the colored border around a sidebar, the underline of a link—these are all examples of styles in action. A style is simply the way in which markup, such as HTML content, is displayed to the user.

All browsers have built-in style rules for displaying HTML; for example, they display the text marked with the <h1> tag as bold, extra-large text and the <p> tag as normal text with a blank line after it. These built-in style rules can be extended or changed by using CSS.

A style sheet is a collection of additional style rules that are added to the browser's rules, changing the way in which the browser displays the Web page. Most style sheets are separate files, usually with the file extension CSS. By saving styles in a CSS file, you can attach the style sheet to any number of HTML pages. This makes it easy to control the presentation of the entire site from a single file or several files in combination.

The word "cascading" refers to the key concept of the cascade. The cascade is the method used to combine style rules from a variety of sources—not only the browser's styles and those of the Web designer, but also the user's preferred styles. In general, the more specific the style, the higher weight it is given in the cascade. A rule that applies to some paragraphs is more specific than one that applies to all paragraphs, and a rule set by the user is more specific (to that user) than one set by the browser.

The CSS language was originally defined by the World Wide Web Consortium's CSS Level One recommendation (December 1996) and was updated in May 1998 by the CSS Level Two recommendation. The CSS Level Two recommendation can be downloaded from http://www.w3.org/TR/REC-CSS2 in a variety of formats, although it's rather dry reading.

CSS Styles Versus HTML

CSS properties can create the same types of presentation effects as HTML tags and attributes, such as the <font> tag or the various attributes on the <body> tag.

But CSS styles go beyond the capabilities of HTML alone, enabling you to create effects such as links that change color when the mouse is moved or borders around any HTML elements. Beyond simple text effects, CSS styles can also be used to lay out the whole page, entirely avoiding the use of HTML tables for layout.

This allows HTML to be used for its primary purpose of conveying the structure of the Web page content, and the style sheet defines the presentation. This separation is quite helpful to Web users who have disabilities, especially visual impairments, because the page content can be accessed directly, yet still allow designers to create visually impressive Web pages. Thus, CSS offers the best of both worlds for the creative designer as well as the user with special needs.

The Syntax of CSS

CSS are created as ordinary text files, as HTML files are, but unlike HTML they don't use a system of tags and attributes. The CSS language instead uses a syntax of its own. Listing 10.1 is an example of a rather ordinary style sheet that sets several rules. Don't worry if you can't understand it yet; you'll be learning to understand CSS rules soon, and Dreamweaver MX 2004 automates the process of writing style rules for you.

Listing 10.1 A Typical CSS Consists of Multiple CSS Rules

body {
  font-family: Arial, sans-serif;
}
h1, h2, h3, h4, h5, h6 {
  font-family: Verdana, sans-serif;
  color: teal;
}
p {
  padding-left: 3em;
}
.person {
  padding-left: 0em;
  font-size: large;
  font-weight: bold;
  color: maroon;
}
.job {
  font-weight: bold;
}

This style sheet consists of five style rules. Locate the pairs of braces (curly brackets) to differentiate each style; each set of braces represents one rule. The style rule begins with a selector, which indicates what parts of the Web page should be styled by this rule. Then, within the braces are one or more declarations, consisting of a CSS property name, a colon, the value of that property, and a semicolon.

These properties determine how the style differs from the default values. For example, the font-family value specifies a certain font face that should be used by the browser instead of its default font (which is usually Times New Roman). Each CSS property can be assigned a certain set of values; for example, font-family can be assigned names of fonts, whereas padding-left can accept measurements. Dreamweaver MX 2004 makes it easy to assign values by providing pull-down menus with the appropriate values and units for each property.

NOTE

When setting measurements and font sizes, you can use several types of units. The most common are pixels and percentages, which work just like pixel and percentage values in HTML. Ems are based on the size of the current font, as set by the style sheet or the user's browser preferences; if the font is 12-point Arial, a measurement of 2 ems equals 2 times 12 points, or 24 points. Inches and centimeters can also be used as units of measure.

Units that are based on a fixed physical size, such as pixels, points, inches, or centimeters, are absolute units. These are most useful if you know the exact dimensions of the output device with a fair degree of certainty. Percentages and ems are relative units, which—if used properly—adjust to the user's desired preferences, which is useful for users with disabilities. In general, relative units are more accessible, but absolute units are easier for designers to work with. If accessibility is a concern, use relative units even if it means slightly less control over the final output.

Link, Class, and Span

To use CSS styles with HTML files, several tags and attributes are used that aren't commonly seen outside of their roles in CSS.

The <link> tag is found only in the <head> section; it specifies a style sheet that is attached to the HTML file. Without using <link>, an external style sheet can't be associated with the Web page; <link> tells the browser to load those styles and apply them.

The class attribute is used to define custom styles in HTML; by setting a class attribute on any HTML tag, you make it part of a group of tags that can be selected by a style rule.

The <span> tag is used to designate a smaller part of an HTML element as being part of a class; it functions like a <strong> or <em> inline element, but with no specific styles attached to it.

Examples of these tags and attributes are shown in the HTML page in Listing 10.2. You won't need to memorize the syntax for these tags and attributes, because Dreamweaver MX 2004 creates them for you. However, you should be familiar with their function, in case you wonder why Dreamweaver MX 2004 is inserting all these strange things into your Web page!

Listing 10.2 This HTML File Uses the class Attribute to Set Styles

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Idyll Mountain Internet Employees</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="idyllmtn.css" rel="stylesheet" type="text/css">
</head>
<body>
<h2>Let's meet the Idyll Mountain team:</h2>
<p class="person">Liz Bartlett</p>
<p>As <span class="job">Chief Operations Officer</span>, Liz runs the 
 business aspects of the company, and is also the primary designer 
 for the majority of Idyll Mountain client sites.</p>
<p class="person">Kynn Bartlett</p>
<p>Although rarely seen around the office, 
 <span class="job">Chief Technologist</span> 
 Kynn Bartlett does system administration as well as trailblazing new 
 technologies and leading our accessibility efforts.</p>
<p class="person">Maria Moreno</p>
<p>Our <span class="job">Graphic Designer</span> Maria is responsible 
 for the visual design of most of our Web sites, and also for entries 
 in the <a href="http://www.dogshow.com/">Virtual Dog Show</a>.</p>
<p class="person">David Waller and Laura Bishop</p>
<p><span class="job">Site Maintenance Specialists</span> David and Laura 
 maintain and develop client Web sites according to the 
 <acronym title="World Wide Web Consortium" lang="en">W3C</acronym>'s 
 <acronym title="HyperText Markup Language" lang="en">HTML</acronym> 
 and <acronym title="Cascading Style Sheets" lang="en">CSS</acronym> 
 specifications.</p>
</body>
</html>

You should be able to spot the class="person" and class="job" attributes in this HTML file; these refer to the style rules from Listing 10.1. Apart from these classes, the HTML file doesn't contain any presentational markup; <font> tags and their ilk are conspicuously absent. If you load the Web page into a Web browser, however, you'll see styles applied to the HTML thanks to the <link> tag referencing the style sheet. The end result of combining the HTML with the CSS is shown in Figure 10.1.

Figure 10.1Figure 10.1 Netscape 6 displays the Web page with CSS styles.

Support for CSS Styles

Although CSS has been around since 1996, it's only recently that there has been widespread support for styles in the browsers. Early attempts at CSS implementation, notably Internet Explorer 3 and Netscape 4, were well-meaning but unfortunately contained serious bugs. Most Web developers wisely avoided using CSS until browsers would correctly interpret their designs.

The short listing of browser versions in Table 10.1 gives a rough idea as to the adoption of CSS by the browsers. This is just a representative sample of the most common browsers and doesn't include many of the other browsers out there, such as OmniWeb, Konqueror, or iCab.

Table 10.1 Browser Support for CSS

Browser and Version

CSS Implementation

Internet Explorer 3

Poor

Internet Explorer 4

Fair

Internet Explorer 5 (Windows)

Good

Internet Explorer 5 (Macintosh)

Great

Internet Explorer 6

Good-Great

Firefox

Great

Lynx

None

Netscape 3

None

Netscape 4

Fair

Netscape 6

Great

Netscape 7

Great

Mozilla 1

Great

Opera 3

Fair

Opera 4

Good

Opera 5

Good-Great

Opera 6

Great

Safari

Great

WebTV

Fair


The good news is that CSS support continues to improve, and the newest versions are quite good at displaying most CSS styles. The bad news is that older browsers are still out there, and that means that you need to be sure to test your CSS-based designs in as many older versions as you can find.

NOTE

Several newer browsers have a special compatibility mode for HTML and CSS, where they adhere more closely to the published standards. Mozilla, Netscape 6 and 7, and Internet Explorer 6 turn on this mode when they encounter a valid DOCTYPE for HTML Strict and for a few other DOCTYPE declarations; other pages are done in a "quirky" mode for backward compatibility with older browsers. For more information, see the following URLs:

http://www.mozilla.org/docs/web-developer/quirks/

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnie60/html/cssenhancements.asp

A recommended basic test suite includes Netscape 4, Netscape 6 (or Netscape 7, or Mozilla), Internet Explorer, Opera, and Lynx. A good CSS design may not look the same on every browser—especially Lynx—but should be functional on all browsers and platforms. You can download these browsers from the following locations if they're not already installed on your computer:

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020