Dreamweaver I | Cascading Style Sheets


Cascading Style Sheets

Coding Optional. With just a few clicks, you can break free of some of the limitations of straight HTML.

CSS is a designer's dream; the secret of its beauty is the level of control you, the designer, get to wield over your site creations.

Don't like underlines on your links? Let CSS axe them. Need to indent the first line of each paragraph? It's done. Want to apply these changes to your entire site and still have time for lunch? De nada.

Style sheets speed the designer's workflow, enabling you to format multiple Web pages (hundreds, if you dare!) all at once.

In this lecture, we will explore Dreamweaver's CSS tools in depth so you can exert precise control over typography, color, and spacing.

In this lecture, you can expect to:

Discover how CSS is used to style Web sites.
Learn how internal and external style sheets are handled.
Learn how to create Tag, Class, and ID selectors.
Explore the various text and design style attributes.
Learn how to create list based navigation.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CSS, Cascading Style Sheets, styles, and CSS rules are synonymous.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Your style sheet should be saved with a .css extension.

 

 

 

 

 

 

 

CSS Storage and Hierarchy

 


Before you revel in the fun of designing and applying styles, you'll need to decide where your styles will live—in a separate CSS file that "talks to" your HTML files, or within the HTML page itself.

Internal Styles

Internal styles, as you might expect, are styles that are contained internally within the HTML document—specifically within the <head></head> tags, for you code nuts out there.

In the example above, an internal style called "caption" was placed within the <head></head> tags of the HTML file.

Internal styles apply only to the HTML file in which they reside. This is limiting, as you'll often need to apply the same formatting to more than one page.

External Style Sheets

You may have heard the buzz phrase "separate content from presentation." Well, this is exactly what an external style sheet does. The HTML file contains the content—all the text, image links, and so on. The CSS file contains the instructions on how to present that content—how text should be formatted, how images should be positioned, and everything else involving how your content should be styled. Hence the name style sheets!

External style sheets can be attached (often described as "linked"—not to be confused with hyperlinking) to any number of HTML pages, making the styles available throughout an entire site.

“Will That Be to Stay or to Go?”

When you create a new style, Dreamweaver needs to know where the style should be saved: internally (right there in the HTML head) or externally (wrapped up "to go" in the external style sheet).

Create a new style by clicking the menu icon in the upper right corner of your CSS Styles panel and choose New—or simply click the New CSS Rule icon at the bottom of the panel .

Choose (New Style Sheet File) to save the style to an external style sheet.

Choose where your rule will be defined: internally (This document only) or externally (New Style Sheet File).

A save dialog will prompt you to name the new external style sheet:

Save your new external style sheet in your local root folder.

Once you've created a style sheet file, it will be listed in the Rule Definition menu. Each time you create a new style, simply select your external style sheet from the menu.

To add styles to an existing style sheet, be sure to select it from the menu, otherwise you might end up creating internal styles by mistake.

Dreamweaver will automatically link the external style sheet to the open HTML page you are working on.

To apply the external style sheet to the other pages in your Web site, you'll have to attach it yourself. Choose Attach Style Sheet from the menu in the upper right corner of the CSS Styles panel, or click the Attach Style Sheet icon .

Browse to the .css file you'd like to attach, make sure the "Link" radio button is selected, and click OK.

Choose the "Link" button when attaching style sheets. The "Import" button incorporates code that is not compatible with all browsers.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Search engines pay more notice to text formatted in heading tags.

 

 

 

 

Using proper markup will improve your search engine ranking.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Format most of your content with tag selectors. Then add class selectors as necessary.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ID selectors begin with an #. For example, #copyright.

 

 

 

Each ID selector can only be applied once per page.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Use the LoVe HAte mnemonic (L-link, V-visited, H-hover, A-active) for remembering the order in which you must link state styles.

 

 

 

 

 

 

 

 

 

 

 

 

Pseudo-class selectors apply to elements in a particular state—for example, a link that has been visited.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CSS Selectors

A CSS rule consists of two parts: the selector and the declaration. The selector tells the browser what to format and the declaration tells the browser how to format it.

This CSS rule instructs the browser to "select all paragraphs and set the font size to 12px.

Tag Selectors

Browsers come with default formatting for HTML elements like <h1> headers and <p> paragraphs. An un-stylized text block would look like this:

The default setting for headers is big and bold.

Using tag selectors, you can redefine the appearance of headers and paragraphs, changing the font size, weight and color:

A h1 tag selector was used to format the heading. A p tag selector was used to format the paragraph.

Each element in an HTML page is inserted inside a tag, which means that tag selectors can be used to format literally everything. A few handy tags are listed below.

 
 

<body> Use the body tag selector to add background colors or patterns to the page body.

<h1> <h2> <h3> <h4> <h5> <h6> Use h1-h6 tag selectors to format headings and subheadings. There are six levels of headings, with h1 as the most important and h6 as the least.

Select the text, then choose a heading level from the Format menu in the Property inspector.

<p> Use the p tag selector to format paragraph text.

<strong> Use the strong tag selector to embolden important words or phrases in the text. Clicking the B button in the Property inspector will wrap text in <strong> tags.

Clicking the B button in the Property inspector will wrap text in <strong> tags.

<em> Use the em selector to format text that you would normally set in italics, like a quote, a technical term, or an idiomatic phrase from another language.

Clicking the I button in the Property inspector will wrap text in <em> tags.

<img> Use the img tag selector to add borders, margins, and padding around images.

 
 

Creating a tag selector begins in our handy New CSS Rule dialog. Select Tag from the Selector Type menu, then choose a tag from the menu to the right of the Selector Name field.

Choose the Tag selector type to format elements set within html tags like paragraphs <p> and headings <h1>.

Class Selectors

Class selectors come in handy when you need to be more specific than a standard tag selector. For example, let's say you have two lists:

Lists are contained within ul tags:

Therefore, both of these lists could be formatted using the <ul> (unordered list) tag selector:

Easy enough. However, how would you make the fruit list red, and the vegetable list green?

You would need to add a class selector to each list:

And write separate class selectors:

As you'd probably expect, since you're now pretty familiar with the New CSS Rule dialog, creating a class selector begins right there. Select the Class selector type, type in a name, and you're on your way. Note that all class selector names begin with a dot (Dreamweaver will put it in for you if you forget).

ID Selectors

ID selectors are used to format unique page elements—for example, a company logo.

ID selectors and class selectors are almost interchangable. You could format the "fruit" and "vegetable" lists using ID selectors rather than class selectors, and you'd get exactly the same results.

However, there is an important point of difference: each ID selector can only be applied to one element per page. You could format any number of lists with a .fruit class selector, but you can only format one list with an #fruit ID selector.

To create an ID Selector, use the New CSS Rule dialog. Choose the ID selector type. Then enter the name of your selector, beginning with a # (hash or pound sign).

Descendant Selectors

Descendant selectors are used to target elements within other elements.

This descendant selector would format any paragraph within the content div:

And this descendant selector would format any paragraph within the sidebar div:

If you select a page element before you create a new CSS rule, Dreamweaver will write the descendant selector for you. Choose Compound (based on your selection).

Descendant selectors are used to select elements that "descend" from another element higher up in the document family tree.

Group Selectors

You'll often apply the same formatting to multiple elements. For efficiency's sake, group the selectors together.

Each selector must be separated by a comma. For example, this group selector removes margins and padding from all unordered lists, ordered lists, and definition lists:

Here's another group selector which adds left and right padding to headings and paragraphs:

Group selectors can include a wide range of selector types. Here's a grouping of two descendant selectors. Note that .content ul is for the unordered lists inside the content div, and .content ol is for the ordered lists inside the content div:

Pseudo-Class Selectors

Now on to the fun topic of link states. You might be familiar with these already, but let's review:

1   1
 

a:link: This is the typical state of a link when it has never been visited—it's just sitting quietly on your page waiting for some action.

a:visited: This state appears when the viewer has visited the link previously, but is not currently doing anything with the link like clicking on it or mousing over it.

a:hover: A designer favorite! This state appears when the user's mouse is hovering over the link—looking much like a JavaScript image rollover.

a:active: This is a brief state, only appearing for the duration of time that the user's mouse is actively clicking on the link.

a:focus: This state will create a rollover effect when using a keyboard rather than a mouse to tab through the navigation options.

 
1   1

You must define your link selectors in the following order:

 
 
  1. a:link
  2. a:visited
  3. a:hover
  4. a:active
 
 

A mnemonic for remembering this sequence is LoVe HAte: L-link V-visited H-hover A-active.

Listing your link styles in the correct sequence will ensure that your a:visited link styles override your a:link styles, and that your a:hover styles override both the a:visited and a:link styles.

When two rules target the same element (in this case, both a:link and a:visited format links) the last rule to be defined wins out.

The a:hover, a:active, and a:focus selectors are often grouped together, since they receive the same formatting during the rollover state. The complete set of link styles looks like this:

Hierarchy of Styles

All this talk of selector types brings us to the important topic of hierarchy—CSS hierarchy, that is.

The best way to remember the hierarchy of CSS is that the closest style wins. External styles are the "farthest" from the item you're modifying—all the way over in some other document. Internal styles are much closer—just at the top of your HTML code. Therefore, an internal style will override an external style.

In addition, a class selector will override a tag selector, because the code used to apply a class selector is wrapped around the item you're styling.

ID selectors carry more authority than classes. If a class selector and an ID selector are in conflict, the ID selector will win out.

CSS will accommodate styles that don't conflict with one another, combining them rather than knocking out the less dominant. That's where the intriguing term "cascading" comes into play (I know you were wondering).

Such issues are paramount when troubleshooting. If you apply a style and don't see a change in your document, there's a good chance that another style is overriding the style you're trying to apply.

The Code Navigator is a helpful tool to identify conflicting styles. Select the page element you need to troubleshoot, then go to View > Code Navigator to study the styles in effect.

Keep Disable indicator checked. Otherwise, the Code Navigator icon will pop up whenever you hover over any page element, which becomes annoying. You can access the Code Navigator by Alt-click (Windows) or Command-option-click (Mac) instead.

Style Settings: Overview

Now that you know how to create internal and external styles using each of the major selectors, we can focus on style settings—the attributes that make your styles so stylish!

Defining Style Attributes

After clicking OK in our trusty New CSS Rule dialog, Dreamweaver supplies us with the following settings:

The CSS Rule Definition dialog is used to edit style attributes.

The various style attributes are organized into categories on the left. Your style may contain attributes from all of the categories, just a couple, or in many cases, just the Type category.

Style Settings: Type Attributes
Type Styles

The Type category allows you to control typography settings such as font size, weight, and color.

Most of the options here are self-explanatory, while others (sigh!) are a little more complicated. Let's look at each one.

 
 

Font-family offers serif, sans-serif, monospace, and cursive font stacks.

Font-size can be set in a number of different units (points, pixels, ems, and so on) which we will talk about in the next section.

Font-weight refers to the boldness of a font. Did you know that you have more options than just plain normal and bold? Click the menu next to the Font-weight field to choose a variation, or enter a numerical weight (ranging from 100 to 900—normal is somewhere in the middle while the standard bold weight is around 700).

Font-style includes "normal" and "italic" and a surprise newcomer: "oblique." Oblique is handled differently from browser to browser. In some browsers, it will act as italic, slanting the text, while in other browsers it will revert to normal. So italic is still your best bet for slanted text.

Font-variant includes an option for "small caps" (where lowercase letters are set as small capitals).

Line-height specifies the height of each line of text, also known as line spacing. To improve the readability of long sections of text, specify a line height slightly larger than the font size.

Text-transform displays text in uppercase, lowercase, or capitalized (where the first letter of each word is capitalized, also known as "Headline Case").

Text-decoration allows you to add underlining, overlining, line-through (also known as strikethrough), or a blinking effect. You may choose more than one of these options. For the sake of all that is right and good, please avoid blinking.

Color sets the font color. Click the color chip or enter a specific hexadecimal color code.

 
 

You do not need to apply all of these type settings—the fields you leave blank will simply default to normal (such as no decoration, normal case, standard line height, and so on).

In the above CSS example, the header style includes font, size, weight, and color. The main paragraph style includes font, color, and line spacing. A strikethrough was added to the original price, and red, italicized text denoted the sale price.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Fixed Type Sizing

Fixed units like pixels, points, inches, and centimeters set type to a specific size. Points are generally used in print design, but on the Web, the pixel is the standard. Pixels display at exactly the same size on Windows and Macs, whereas points vary slightly.

For consistent results, use pixel units.

Relative Type Sizing

Relative units like ems, exs, or percentages will scale according to default browser font sizes.

Em or % units are the most widely used relative type units.

The % option is just what you'd expect it to be—100% appears at the default font size, and percentages above or below that will appear larger or smaller accordingly.

An em is the width of the letter "m"—the widest letter in most fonts. 1 em is equal to 100%. Variations are in decimal format (for example, 0.9 em would be smaller and 1.25 would be larger).

An ex (you guessed it) is the width of the letter "x." Exs are not often used—most designers go with ems instead.

You'll also find options for "x-small/small/medium" font sizes. However, these settings vary widely between Windows and Macs. A "small" font size on a PC will display "xx-small" on a Mac. Use em or % units for more consistent results.

These font size settings are not recommended. Use em or % units instead.

Type Blocks

The Block category of the CSS Rule Definition dialog offers several methods of controlling the spacing and presentation of text blocks.

The settings in the Block category affect the spacing and alignment of a text area.

Most of the block attributes work just like their names suggest.

 
 

Word-spacing and Letter-spacing adjust the space between words and letters for better legibility (or worse, if you're feeling anarchistic).

Vertical align will align content to the top, middle, or bottom. The vertical-align menu also includes options for subscript and super script—most often used to place digits above or below the baseline, as in mathematical or scientific uses.

Text-align sets a paragraph to left-align, center, or right-align.

Text-indent creates an indent before the first few words of a paragraph.

White-space can be set to "pre" to allow multiple space marks (normally, if you type two consecutive space marks Dreamweaver will assume it's a typo and compress the spaces into one) or "nowrap" to prevent line wrapping without a <br> tag. This property is not often used.

Display determines how an element will display. If you choose display none, the element won't display at all. This comes in handy for "hide and show" behaviors.

 
 

Revisiting our vegetables example, the header has been given 5 pixels of letter spacing. The paragraph text has been set to align right.

 

 

 

 

 

 

 

 

 

 

 

Apply background images to the h1 tag selector to create interesting page headers.

 

 

 

 

 

 

 

 

 

 

 

Style Settings: Design Attributes

The remaining categories in the CSS Rule Definition dialog deal with page design rather than typography.

Backgrounds

The first two settings in the Background category work as expected. Enter a hexadecimal color code to define a background-color. Use the Browse button to choose a background-image.

The background-repeat setting determines if (and how) a background image will repeat. The "repeat" option tiles the image horizontally and vertically, "repeat-x" tiles horizontally only, "repeat-y" tiles vertically only, and "no repeat" does not tile at all so your image will only appear once.

Background-attachment determines the position of the background image as the user scrolls the page. The "scroll" attachment option (which is the default) sets the background to scroll along with the page content, while "fixed" leaves the imagery in position as the page scrolls.

Background-position (X) and background-position (Y) allow you to better situate your background image (left, center, right, top, middle, or bottom).

In each of the above examples, green was selected as the background color and the grass illustration was chosen as the background image. The first version shows the "no repeat" setting. The second uses the "repeat-x" setting. The third uses "repeat-y" and a "horizontal center" position. The possibilities are endless!

Borders

The Border category gives you a selection of border styles, widths, and colors:

Check "same for all" if you want the border to surround all four sides of your element.

This table has a 4 pixel green dotted border.

Uncheck "same for all" to create a border across just one edge.

This h1 tag selector has a bottom border only.

Boxes

The Box category is used to control the spacing around page elements.

There are two options: padding and margin. Padding adds space inside an element.

The "Order the Guide to Fruit and Veggies" text is too close to the dashed border—it needs padding.


Here, 5 pixels of padding has been added.

Margin adds space outside the element.

Here, 20 pixels of left and right margin has been added outside the dotted border.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Make links block elements to create a button-like effect with a larger clickable area.

 

 

 

 

 

 

 

 

Block elements must have a defined width to display properly in IE.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

List Based Navigation

The larger your Web site grows, the more links you'll need. Graphic buttons make your pages image heavy and slow to load.

A more practical approach is to use plain HTML links, styled with CSS. It is good for you (easy to build and easy to edit) and good for your user (quick to load and more accessible to screen readers).

Go to the course downloads area to download the listamatic file. Unzip the listamatic folder and move it into your dw1exercises local root folder.

These sample list styles have been modified from the list gallery at css.maxdesign.com.

An Unordered List

Expand the the listamatic folder in your Files panel and double-click to open the v1-unordered.html file. In Design View, you'll see an unordered, bulleted list.

Switch to Code View to study the HTML list structure. The unordered list has a class of .nav. Each link is inserted in a separate list item.

Using CSS, we can give the list a complete makeover.

A Simple List

Open the v2-simple.html file. You'll notice some changes in Design View— the list bullets are gone, and the font color is red.

Switch to Code View. The HTML list formatting is exactly the same, but the page is linked to an external style sheet:

Use the Related Files toolbar to toggle to v2-simple.css.

Study the CSS selectors used to format this list:

ul.nav is a combination of a tag selector (ul) and a class selector (.nav). Therefore, ul.nav will format any unordered list with a class of .nav.

The list-style "none" property removes the bullets. Setting the padding and margins to "0" removes any default browser spacing around the list.


ul.nav li is a descendant selector. It controls the list items inside the .nav unordered list. The padding property defines the spacing between the list items.


This rule groups two selectors: ul.nav a and ul.nav a:visited—so the visited links will look the same as the non-visited links.

The font family and font color are defined here. If you would like to choose a different font or font color, edit these settings.


This rule groups three selectors: a:hover, a:active, and a:focus, controlling the appearance of the link during the rollover state.

The rollover font color is set to a lighter shade of red. Test it in Live View. To change the rollover color, edit this style.

A Rollover List

Open the v3-rollover.html file. This is starting to look more like an official nav bar. Click the Live View button to test the rollover effect.

This rollover nav bar is styled with pure CSS. The horizontal stripes that extend to the right will fit properly once the list is inserted in a sidebar div.

If you switch to Code View, you'll see that the list markup is exactly the same as the other lists. However, the CSS styling is quite different:

The list items have a 1 pixel solid gray bottom border.

If you would like to change the border style, click the pencil icon to edit the settings in the CSS Rule Definition dialog.

The top border is applied to the unordered list itself.

The list also has a bottom margin, to allow some space below the list.

The display "block" property makes the entire button area clickable.

The text-decoration is set to "none" to remove the link underlines.

This rule sets the background color and the font color for both mouse and keyboard navigators.

You can change these colors to match your page design.

A Box Style List

Open the v4-box.html file and click the Live View button to test the rollover effect.

Most of the CSS properties work the same way as the rollover list, but you'll notice a few differences:

The left and right margins are set to "auto" to center the list.

The right, left, and top borders are applied to the unordered list.

The bottom borders are applied to the list items.

The background color is set to white. Choose a different color if you like.

The text align "center" property centers the text on each button.

Just like the rollover list, the display "block" property is added to make the entire button clickable.

A 3D List

This list flaunts a 3D effect. Open the v5-3d.html file. Test the rollover effect in Live View.

The "current" link style is used to indicate the current page, a friendly "You are here."

Notice how the first link is flagged with a red marker? A class of "current" has been applied to the link anchor in Code View.

Apply the "current" class to the relevant link on each page. For example, on the home page, the home link would get the "current" class. On the about page, you would delete the "current" class from the home link and apply it to the about link, and so on.

The appearance of the current link is controlled by this selector in the external style sheet:

Edit the ul.nav a.current selector in the CSS Rule Definition dialog to see the border settings. Each border is assigned a different shade of blue, creating a beveled effect.

Horizontal Lists
A Simple Horizontal List

Open the h1-simple.html file. The links display in a single row.

The secret to the horizontal list style is display "inline."

The display "inline" property forces the list items into a single line.

The right padding creates some space between the links.

A Rollover Horizontal List

Open the h2-rollover.html file. Try out the rollover effect in Live View.

The list items are set to display "inline." In addition, the link anchors are set to float "left." This turns the links into block elements, making the entire button area clickable.

The unordered list is set to float "left" and width "100%". These properties force the list to span the full width of the browser, creating the nav bar style.

A Tabbed Horizontal List

Open the h3-tab.html file. How are the tab borders defined?

The black bottom border is applied to the unordered list itself, so it spans the full width of the list.

The top, left, and right borders are applied to the link anchors.

"Link one" is styled as an opened tab. The 1px solid white bottom border applied to the .current link anchor overlaps the 1px solid black bottom border applied to the unordered list, whiting it out.

If you want to change the tab background color to something other than white, remember to change the bottom border color as well. The bottom border needs to match the background color in order for the illusion to work.

A Simple Tabbed Horizontal List

Open the h4-simple-tab.html file: a minimalist take on the tabbed list.

In this list variation, the .current link has a 4px solid back bottom border.

A Button Style Horizontal List

Open the h5-centered-buttons.html file. Test the rollover style in Live View.

There are three things I like about this list:

1. The links are centered in the nav bar, creating a balanced, symmetrical layout. The text-align "center" property applied to the ul.nav selector does the trick.

2. The current page is indicated by the yellow ul.nav a.current background color.

3. On rollover, the links are highlighted by a black border, applied to the ul.nav a:hover, ul.nav a:active, ul.nav a:focus selector.

A Pipe List

A common building block in any site, pipe lists are an important skill to have in your Web designer's toolkit. Open the h6-pipedream.html file.

Each list item is separated with a pipe ("|"), created by a 1px solid black left border:

Feel free to experiment with these sample lists. Change the background colors, border styles, and padding values and see what happens.

If you need to undo your style changes, toggle to the .css file, then Edit > Undo.

Now you're ready to put CSS to work in the exercise.

   
 
Learn the benefits of using CSS for positioning.
Learn how CSS sites are planned and developed.
Learn step by step how to modify a Dreamweaver CSS starter page.
Explore the concept of responsive design.
 

Discussion
Share your thoughts and opinions on Dreamweaver in the Discussion area.

Exercise
Format a camp Web site using an external style sheet.