The Cascade by Example

The Cascade in Cascading Style Sheets can determine how a web page displays. The purpose of the Cascade is to resolve conflicts when more than one rule applies to an element on an HTML page. Factors such as specificity, inheritance, and the order of the rules within a style sheet can all affect the Cascade.

The most basic concept around the Cascade is the location of the style rule. Styles can be written in an external stylesheet, an internal stylesheet, or inline. In addition, each browser has built in default styles for HTML elements that determine formatting and appearance.

The general rule is “the closest style rules.” Rules cascade from browser defaults, external rules, internal rules, to inline rules. Each step down the cascade from browser defaults to inline rules brings the rule nearer and nearer to the HTML element it targets.

Some illustrations

Here’s a paragraph with no styles attached. It displays according to the browser’s default styles.

a paragraph with browser default rules

Adding an external style sheet serves to illustrate the next step in the Cascade. I’ll add this rule to an external, linked stylesheet:

p {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
color: #00F;
}

Here’s the result. You can see that the rules for font-family and color in the external stylesheet take precedence over the browser default rules.

a paragraph styled by browser defaults plus external rules

The next step down through the Cascase into closer proximity to the affected HTML paragraph is to add an internal style rule to the document head.


p {
color: #93F;
}

This rule sets the paragraph to a purple color. Notice that the font-family set in the external style sheet is still Trebuchet, but the external rule regarding color has been overruled by the internal color rule.

a paragraph affected by both external and internal style rules

The next step down in the Cascade is one that is not normally taken. It needs illustration, even though it is not a best practice and its use is discouraged. That is the inline style, a style rule written as an attribute of a single HTML element.

Here’s what I added to the <p> tag in the HTML document.

<p style="color: #F00">

The result is yet another color change. The inline red color rule takes precedence over the internal rule and the external rule. The font-family rule set in the external stylesheet remains unaffected.

a paragraph with external, internal, and inline styles

These examples illustrate the principle of location of style rules in the Cascade. In practical terms, most web sites use only external stylesheets, and internal and inline styles don’t enter into the mix.

Additional Thoughts

I plan to add future posts explaining factors that can affect conflicts in the Cascade when the only styles are in one or more external stylesheets. As I mentioned earlier in this post, that can include concepts such as the specificity of selectors and the order of  the rules in the stylesheet. The order in which external stylesheets are linked to a page is also part of the Cascade.

Related posts:

Using Dreamweaver to add labels to forms

Web Teacher tip on how to use form labels in Dreamweaver.

There are still people out there who are convinced that Adobe Dreamweaver can’t create an accessible or standards-compliant web site. Well, it can. You do have to know what an accessible site is, and you do have to use the tools (in this case, Dreamweaver) properly. After that, you will be completely able to create accessible and standards-based work in Dreamweaver.

One aspect of accessibility is the proper use of the label element with form fields. Dreamweaver makes that very easy to do.

Starting with Dreamweaver CS4, all the accessibility preferences are on by default. If you are using Dreamweaver CS3 or older, you have to go to Preferences and turn them on. Select the Accessibility category on the left.

Make sure every field is checked, not only the one for Form objects. Use best practices at all times and you’ll be accessible.

On a document in Dreamweaver, insert a form element. You’ll see this dialog.

form dialog

Here you enter the ID and the label text. Select Attach label using ‘for’ attribute for the majority of form elements. Dreamweaver will give the ‘for’ attribute the same value as the value you enter for the ID. Form controls like Submit or Reset buttons need no label because the element itself is the label.

Next, decide whether to place the label text before or after the form element. For input elements such as text boxes, the label normally should go before the form element. For radio buttons and checkboxes, the label should go after the form element. Although the dialog has access key and tab index options, I suggest you leave them blank. These have proven over time to be more of a hindrance than a help.

Click OK and you have an accessible form element with a label.

Related article: Styling the label element with CSS

The hReview microformat I use

My adaptation of the hReview microformat. More . . .

My history with microformats on this blog has been long and mostly happy. I have great success with the hReview format, which is the one I use most frequently. I use it for book reviews, but it can be used for any type of product review.

I thought it might be nice to share the code. Not that I have anything different from what you might see at microformats.org, but I have adapted it a bit.

Here’s the code for the version I use now. It can be adapted to suit; just use the classes that you see for each element so it retains its microformat identity.

<div class="hreview" id="hreview-summary">
<span class="reviewer vcard">
<span class="fn">Reviewer: NAME HERE</span>  </span>
<span class="type" style="display:none">product</span>
<img alt="photo of 'PRODUCT NAME'" src="PRODUCT PHOTO URL" class="photo" />
<div class="item">
<a class="fn url" href="PRODUCT URL">PRODUCT NAME</a>
</div>
<div class="description">
<p><abbr title="5" class="rating">&#x2605;&#x2605;&#x2605;&#x2605;&#x2605;
</abbr></p>
<p>
WRITE REVIEW HERE
</p>
<p class="summary">Summary: SUMMARY</p>
</div>
</div>

I hand code in the number of stars I want to give using the character entity &#x2605; because I haven’t had good luck with the autogenerated stars. Nor do they look good in Safari unless I use the character entity.

Google is very quick to pick up hReview information, which is why I’m so fond of it.

Tip: Styling the label element with CSS

More about styling form elements, this time the label element . . . .

A few days ago I wrote  Tip: Styling a Fieldset with CSS. I decided to add to the small form I used and provide another tip on how to style the label element.

I made a few changes to the HTML example I’d used in the previous example. Here’s the new HTML page, with CSS. The changes result in this appearance for the form.

the styled labels in the form

In the HTML, I changed the labels to include the “for” attribute, so that I could move the closing label tags from where they before. The code is now like this for each label:

<label for="name">Your Full Name</label>
<input type="text" name="name" id="name" />

I also moved the submit button out of the fieldset, since it really didn’t belong in there in the first place.

I added the CSS to make the input fields appear as you see in the image (or on the example page). I made the labels display as block level elements, which were floated to the left. Then I assigned a width to the labels so that the input fields would all be a uniform distance away from the labels. I assigned a color and made the text bold. I used generated content to add a colon ( : ) after the label. (The colon may not appear in every browser.) Here’s the CSS.

label {
color: #B4886B;
font-weight: bold;
display: block;
width: 150px;
float: left;
}
label:after { content: ": " }

Those are the only changes needed to the form as it was described in the post about  Styling a Fieldset with CSS.

Tip: Styling a fieldset with CSS

A small form with a styled fieldset to give you some ideas about how to punch up the appearance of a form. More . . .

A form element that will help you organize and clarify a form is the fieldset element. A form can contain more than one fieldset. For example, there might be a fieldset containing the form elements for the user’s personal information: name, email, etc. Another fieldset might collect information about a question, a product, or a service. Depending on the purpose of the form, fieldsets can help break it up into meaningful chunks and visually distinct areas.

click image to see the HTML page

Click the image to see the actual HTML page. (The form is nonfunctioning, submit won’t get you anywhere.)

The form in the example contains one fieldset. It’s simple, but it’s enough to illustrate the point.

Here’s the code for the fieldset:

<fieldset>
<legend>Reserve Your Space</legend>
<p>
<label>Your Full Name
<input type="text" name="name" id="name" />
</label>
</p>
<p>
<label>Email
<input type="text" name="email" id="email" />
</label>
</p>
<p>
<label>Phone
<input type="text" name="phone" id="phone" />
</label>
</p>
<p><input type="submit" name="button" id="button" value="Submit" />
</p>
</fieldset>

Look at the various elements in the form. These elements can be styled: fieldset, legend, p, label, input. (The p elements aren’t necessary, the example could use br or div instead. And, in most forms, the submit button would not be part of a fieldset, particularly if the form had more than one fieldset, which is a very likely scenario.)

Any rule of CSS can be applied to any or all of the form elements present. You can write rules for fonts, colors, background, background-image, line spacing, padding, margin, border, or anything else you might want to present in a particular way.

These are the CSS rules I wrote. You could do this many other ways.

fieldset {
border: 1px solid #CCA383;
width: 50%;
background: #FFE8EF;
padding: 3px;

}
fieldset legend {
background: #CCA383;
padding: 6px;
font-weight: bold;
}

Some simple touches with CSS involving color, padding, and border make the legend stand out and visually emphasize the form on the page.

See also: Styling the Label Element with CSS, which contains the same fieldset with additional styling for the label elements. Also see Three examples of fieldsets styled with CSS, which gives some very different fieldset examples.

Adapting Dreamweaver CSS layouts to display:table rules

With the upcoming release of IE8, which will support CSS display:table and other table related display properties, I’m expecting changes in the way web pages are laid out. I just finished an intensive experience with Adobe Dreamweaver CS4 and the built-in CSS layouts that come prepackaged with the product. Naturally, my mind veered in the direction of those layouts. Okay, the truth. Actually, I’ve been having visions of Stephanie Sullivan (who created the Dreamweaver CS4 built-in CSS layouts) madly adapting all those page layouts to add display:table layout options.

Well, I couldn’t wait for the next release of Dreamweaver to see what Steph might do (or might not do—this is only my fantasy—no idea if Steph is participating) to add to the layouts she’s already made for Adobe. I had to play around with it myself.

I started off easy. I picked a layout with two columns, no header and footer, and a fixed width. In the Dreamweaver File > New menu, this layout is called 2 column fixed, left sidebar. I got just a bit fancy by adding a div in the main content area that held two boxes or cells that use display:table-cell in the CSS. There are colors and widths and other CSS rules from the Dreamweaver layout that I didn’t touch. I only removed the rules for layout and added new ones. Here’s the CSS for the layout:

#sidebar1 {
display: table-cell;
width: 200px;
background: #EBEBEB;
padding: 15px 10px 15px 20px;
}
#mainContent {
display: table-cell;
padding-left: 8px;
}
.cell {
display:table-cell;
width:400px;
height:100px;
border:1px solid red;
}

Here’s how it looks. If you click the image, you’ll see the actual HTML page, which contains the CSS and comments that explain what I changed and did. Don’t expect the layout to look right with IE unless you have version 8. Use Firefox or Opera or Safari. Of course, you can view the source with IE6/7 to see the HTML and CSS.

layout using display:table properties

The changes were super easy. Took no more than 5 or 10 minutes to do.

I tried something harder. This time I used the Dreamweaver File > New layout for CSS called thrColLiqHrd, which means a three column liquid layout with a header and a footer. You are probably aware that this type of layout is normally done with floats (which must be cleared). Sometimes this type layout also needs faux columns to get equal heights on the three columns.

In the HTML, I changed the source order for the three columns to sidebar1, mainContent, sidebar2. I added a wrapper called contentwrap around the three columns. This enabled me to display the three columns using display:table-row. Similarly, the header and footer were designated as display:table-row. The last change was to set each column to display: table-cell. As with the first adaptation, I left colors and widths and other rules from the built-in Dreamweaver layout the same. I only changed rules affecting the layout. Here’s the CSS for the layout.

#container {
display: table;
width: 80%;
background: #FFFFFF;
margin: 0 auto;
border: 1px solid #000000;
text-align: left;
}
#header {
display:table-row;
background: #DDDDDD;
padding: 0 10px;
}
#contentwrap {
display:table;
}
#sidebar1 {
display: table-cell;
width: 22%;
background: #EBEBEB;
padding: 15px 0;
}
#sidebar2 {
display: table-cell;
width: 23%;
background: #EBEBEB;
padding: 15px 0;
}
#mainContent {
display: table-cell;
}
#footer {
display: table-row;
padding: 0 10px;
background:#DDDDDD;
}

And here’s the result. If you click the image, you’ll see the actual HTML page, which contains the CSS and comments that explain what I changed and did.

three column layout with display:table rules

This layout took longer to adapt, maybe 20 to 30 minutes. Plus, I made a few return trips to the book Everything You Know About CSS is Wrong! to make sure I knew what I was doing.

Even when IE8 releases, using layouts like these without conditional comments linking to IE7 CSS rules won’t truly be practical until all the older installations of IE have disappeared. For a while it will be like the bad old days when everyone used @import to create a stylesheet just for Netscape. Except, now we’ll use conditional comments to accomodate older versions of IE. We’re still dealing with the pain of creating extra stylesheets for browsers that don’t perform up to standards. Same old, same old. Sigh.

Yet. Yet. People will start playing with this more fully when IE8 releases. Here’s my advice to Dreamweaver users who are eager to start experimenting with layouts using display:table properties. You don’t need to wait for the next release from Adobe to create some pages using CSS table layouts. Try adapting the layouts you have. It works.

Related posts: CSS Tables, Review: Everything You Know About CSS is Wrong!, Mastering CSS with Dreamweaver CS3

Adobe Dreamweaver CS4 Classroom in a Book

get this book at AmazonAdobe Dreamweaver CS4 Classroom in a Book is now available.

This book is officially the product of the Adobe Creative Team, which includes quite a number of people including several editors and other people at both Adobe and Peachpit Press. For this edition of the book, I wrote some new lessons and revised some of the previous lessons. I was under the impression that my name would not be mentioned in the book. So I was pleasantly surprised to see my name in small print on the copyright page as writer.

If you are a Dreamweaver beginner, or want to learn about the latest features in this version of Dreamweaver, consider using this book as your guide. It’s my longest ever Web Teacher Tip.