Home > Articles

This chapter is from the book

Cascading Style Sheets

Cascading style sheets, or CSS, can make your life as a Web designer much simpler. CSS allows far greater control over the objects in your document and can adjust attributes that aren't accessible from standard HTML.

Because of their complexity, editing a CSS manually isn't much fun. Understanding the syntax isn't difficult, but remembering all the attributes that you can set is. For example, here is a simple style sheet that alters the <td> and <th> tags:

<style type="text/css">
<!--
td { font-family: Arial, Helvetica, sans-serif; 
   font-size: 10pt; color: #0000CC}
th { font-family: Arial, Helvetica, sans-serif; 
   font-size: 12pt; font-weight: bold; color: #660000}
-->
</style>

This style sheet sets the font rendering preference for <td> elements to Arial, Helvetica, sans serif and also assigns a default size and color. Likewise, the <th> tag is also redefined to the same font face, but a larger font size and a different color. Any page that contains this style sheet or is linked to a style sheet containing these attributes will inherit the properties of these tags.

NOTE

When you see a list of fonts used in style sheets or within a font tag, it indicates the preferred font rendering order. If the first font in the list is available, it is used, if not, the browser moves to the second, and so on.

The Dreamweaver MX interface makes defining tag attributes very simple—just a few points and clicks and you're set.

Working with Style Sheets

The CSS Styles panel, found under the Window menu or in the Design panel group, is the one-stop shop for creating, modifying, and linking to style sheets. Go ahead and open the CSS Styles panel now. It is shown in Figure 3.14.

Figure 3.14 CSS Styles can be applied across a single page or an entire Web site.

The CSS panel starts out empty. If you have an existing style sheet, you can immediately attach it to your document by clicking the first icon in the lower-right of the style sheet panel (rectangle with chain line). You'll be prompted to choose the style sheet within your site directory.

To create a new style, click the New icon in the lower-right corner (the pad of paper with "+" symbol), or choose New Style from the panel's pop-up menu in the upper-right corner. You should now be at the New CSS Style dialog box, shown in Figure 3.15.

Figure 3.15 Choose the type of style you want to define and click OK.

Three types of styles you can create are as follows:

Custom Style—The custom style can be applied to HTML objects, but must be explicitly specified when you want to use it. Custom styles are named starting with a period (.).
Redefined HTML Tag—By redefining a tag, you set the default style for that tag when it is used in the document. In order to override this default, you'll need to apply a custom style to the document.
Use CSS Selector—A CSS Selector is a combination of several tags.

The most powerful of these tools is the ability to redefine an HTML tag—suddenly your document can inherit a look without having to change any of the HTML.

You can create your styles in two places—an external file, or in the document that you are working in. If you want to add the style to a new style sheet, choose New Style Sheet File from the pop-up menu in the Define in section of the style creation screen. If you've already attached a style sheet to your document, you can select that sheet instead of creating a new one.

If you prefer to keep the styles defined directly within a document, make sure that This Document Only is selected.

Creating a Style

To create your first style, try redefining a tag like <b>. Click the Redefine HTML Tag button and choose <b> as the tag name. Click OK to continue:

  1. Eight categories of attributes can be set for each style. The most common one is Type, which controls font attributes. If the type category is not selected, choose it now. The CSS Style definition dialog box is shown in Figure 3.16.

Figure 3.16 You can control every aspect of your style's font definition here.

  1. Set a sans serif font face, forced to a 72 pixel size, with a bold weight. If you don't want to follow this example exactly, you can choose any of the attributes you prefer to use, including color, style, and line height. In fact, the number of attributes is quite overwhelming. Because of the volume of options, I'm including a short reference immediately following this simple tutorial.

  2. When you've finished deciding what your bold tag is going to look like, click the OK button.

  3. Before trying the style, go through the same process again; except this time define a custom style for yourself.

  4. The process of defining a custom style is exactly the same, except for choosing Make Custom Style (class) instead of Redefine Existing Tag. Name the new custom style .heading, and set it as you did the previous font. I choose a size of 128 pixels and an oblique style.

  5. Close the style definition window when finished.

After your two styles are created, you should be able to look at the HTML source code for your document and see the style definitions in the <head> section:

<style type="text/css">
<!--
b { font-family: Arial, Helvetica, sans-serif; 
   font-size: 72px; line-height: normal; font-weight: bold}
.heading { font-family: Arial, Helvetica, sans-serif; 
   font-size: 128px; font-style: oblique; font-weight: bold }
-->
</style>

Open the style panel again and take a look at the listed CSS styles that are available. Notice that only superbold is listed. The reason for this is that the only listed styles are custom styles. Styles based on redefined HTML tags are applied automatically and don't need to be listed.

Applying a Style

To apply a CSS-based style that redefines an existing HTML tag, you don't need to do anything except use that tag in your document. Try using the HTML tag you redefined in the previous example. If you followed the example and defined the <b> tag, just enter some text in the design view and choose Style menu under Text.

Notice that all of a sudden the bold tag doesn't behave the way it used to. Anytime you use it in the document where it has been redefined, it will appear using the attributes that you set.

To set text to a custom style, you can use either the CSS Styles panel or the CSS Styles menu under Text. Simply select what you want to apply a style to, and then click the style in the panel or choose it from the appropriate menu. If the auto-apply check box in the lower-right corner of the panel is not checked, you'll need to click the Apply button in the panel before the style will be applied. The None style will remove any applied styles from the current selection.

For precise control over where the style is applied, you can use the pop-up menu at the top of the CSS Style panel to choose any of the containing tags of the current selection.

TIP

If you've set up styles that aren't appearing correctly in your document design view, it might be that Dreamweaver MX doesn't support rendering that particular style attribute. Check the CSS definition screens for an * by your missing attribute. If you see an *, you'll need to preview your document in a browser to see that style attribute rendered correctly.

Editing and Linking to External Style Sheets

Defining styles within a document is perfectly fine, but doesn't make the best use of style sheets. Instead of redefining styles each time they need to be used, you can create a master style sheet that is linked to by every page that needs it. This is a more efficient means of keeping your site consistent. The most direct way to link to a style sheet is using the Attach Style Sheet icon in the lower-right corner of the CSS panel, as mentioned earlier. This, however, doesn't give you the greatest amount of control over the style sheets—you can use the Edit Style Sheet panel menu to manage linked styles in your document.

To create an external style sheet, use the panel menu, or choose CSS Styles, Edit Style Sheet from the Text menu. The Style Sheet editor seen in Figure 3.17 should appear.

Figure 3.17 You can edit and link to style sheets from this window.

From this view, you can create new styles or duplicate, edit, and remove selected styles. The item that we're mostly interested in right now is the Link button. Go ahead and click it now. You'll be prompted for a file or URL to link to and given the option to link or import the targeted style sheet. The Link External Style Sheet dialog box is shown in Figure 3.18.

Figure 3.18 Link to an existing style sheet or provide a new name to create a new file.

If you have a style sheet created, enter its location in the URL. If not, supply a new filename in the URL field and click OK. This creates a new empty style sheet.

NOTE

The Style Sheet import function only works on Internet Explorer. You should always link to external style sheets so that you maintain the greatest cross-platform compatibility.

The Edit Style Sheet dialog box will reappear listed with the linked-to style sheet. To start working with the new style sheet file, select it in the list and click Edit. An empty copy of the edit window will appear. This window references the linked style sheet. Here you can add all the styles you prefer. They'll be saved to the external file, but will be accessible in your current document. On subsequent Web pages, you can simply link to this newly established style sheet and instantly have access to all your styles.

TIP

Dreamweaver MX does not currently offer the ability to copy styles between sheets. If you want to merge two existing style sheets, open them in a text editor and copy the contents from one into the other.

A reference to the supported Dreamweaver MX CSS elements is included in Appendix C.

A Final Word on Styles

When you start using CSS, don't get carried away. CSS is a great idea and a fantastic tool, but it does not always work the way you would think. There are inconsistencies between Internet Explorer and Netscape, and between the same browsers on different platforms.

In my experiences with CSS, I've found that it is very useful for maintaining consistent text attributes across pages and keeping sites as cross-platform as possible. Don't expect things to look exactly as they do in the Dreamweaver MX design view, however. Be sure to preview in your browser frequently and have more than one browser platform to use in your tests.

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