Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Dreamweaver Developer Center /

Understanding specificity

by Adrian Senior

Adrian Senior
  • www.webade.co.uk
  • www.communitymx.com
  • www.ukcsstraining.co.uk

Created

5 June 2006

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
CSS design Dreamweaver 8 website

Requirements

Prerequisite knowledge

Basic knowledge of using Dreamweaver and CSS.

User level

Beginning

Required products

  • Dreamweaver (Download trial)

Sample files

  • specificity_sample_files.zip (2 KB)

Note: This article was written for Dreamweaver 8. However, the content in general is still valid for Dreamweaver CS5, even though screenshots may differ.

In this tutorial you will look at specificity. Specificity is a type of weighting that has a bearing on how your cascading style sheet (CSS) rules are displayed. All rules in your style sheet carry a specificity rating regardless of selector type, although the weighting that is given to each selector type varies and will ultimately affect the styling of your web documents.

Specificity is often the cause of problems for designers who don't yet have a firm understanding on how specificity works. If you have a particular CSS rule that looks as though it should be applying a set of styles to your web page in a certain way, but it isn't; then it is very likely that your "misbehaving rule" is being overridden by a more specific selector.

After you have completed this tutorial you will have gained the ability to use specificity to your advantage. You will also have discovered how to map CSS rules to specific elements within your web page. Specificity is a powerful tool.

The examples used in this tutorial are available through the download link below. You can reference them as you work along, or you can follow along and create the examples as you work through the tutorial.

The laws of specificity

You should be aware that the laws of specificity can change from version to version. I think at this point in time it would be fair to say that CSS 2.1 is generally the version that most designers aim for; so this tutorial will reference CSS 2.1.

Specificity is calculated using a formula that is based on the selector section of your CSS rule. The selector area is the section that comes before the opening brace, in the following example, the h1 section of the rule is the selector:

h1 { color: #000000; }

A selector can contain one or more reference points, the greater the specificity rating of these reference points the more specific your rule is.

The specifity hierarchy

Specificity is calculated in a hierarchy system. In CSS 2.1, this hierarchy is split into four distinct categories for the purpose of determining the specificity of any given rule (listed in descending order, with Inline Styles being the most specific category):

a. Inline Styles

b. IDs

c. Classes and pseudo-classes

d. Elements and pseudo-elements

The specificity of an h1 selector that contained only the element name would be calculated as follows:

h1 = (a=0), (b=0), (c=0), (d=1)

In reality this would be written as a comma-separated list:

h1 = 0,0,0,1

If your selector referenced an id then the values would look like this:

#content h1 = 0,1,0,1

As the example above contains an id selector (#content), the second value becomes 1 to show that this selector contains 1 id reference point. The second example is more specific, because it contains an id reference point which carries more weight than a simple element name. It doesn't matter where this rule is placed in your style sheet; it will always win out against the simple h1 selector due to its greater specificity. The further to the left of the comma-separated list your reference points are, the more specific the reference point is.

Let's assume that in error you have written the same rule into your external style sheet twice. When placed next to each other, these two rules might look like the following:

h1 {color: blue;} h1 {color: yellow;}

In this example, the specificity of both rules is 0,0,0,1 neither is more specific than the other. If these were the only two rules affecting h1 in your style sheet, then the h1 element would be given the yellow color; as the lower rule in your style sheet is closer to the element to be styled, it is deemed to be more specific. If you switched the two rules so the color: blue; rule was on the bottom, then the h1 element would be shown in blue. When selectors have an equal specificity value, then the conflict will always be resolved in this manner.

The origin of style sheets

Styles sheets can originate from different places—you can link to them, you can embed them in the head of your document, and you can even have inline styles living right inside your XHTML document. Origin can, and does, affect specificity where conflicting rules exist. In the previous example, you saw how conflicting rules are resolved; the origin of your style sheet will also have a bearing on the final outcome of conflicting rules.

You might also have an embedded style sheet in the head of your web document similar to this one:

<style type="text/css"> h1 {color: red;} </style>

If such an embedded style sheet existed, the specificity of the h1 selector would be 0,0,0,1, which is exactly the same as the specificity for the two rules in your linked style sheet. However, the embedded style sheet is closer to the element to be styled and would therefore win out and the h1 element would be red in color.

So far, you have seen how conflicting rules are resolved. Next, you will look at some working examples of specificity where conflicts are not involved. After you have completed that section, you will learn how you can read the document tree from within the Dreamweaver Design view and learn how to write CSS rules with specificity in mind using the CSS panel.

Using specificity to your advantage

Table 1 shows four variations of an h1 selector. More specifically, it shows how you can affect the same h1 element by manipulating the specificity of your CSS rule; example number 4 is the most specific and example number 1 is the least specific. You can find the complete rules in the example1.css file (located in the CssFiles folder), which is part of the sample files that accompany this tutorial.

Table 1. Calculating specificity

Example CSS Selectors Inline Styles ID Classes and Pseudo-classes Element Names
1 h1 0 0 0 1
div h1 0 0 0 2
div h1.blue 0 0 1 2
#content h1 0 1 0 1

A rule becomes more specific as the values in the ID, Classes and Pseudo-classes, and Element Names columns increase (you will learn more on inline styles below). A single point is awarded to each column for the number of times a type of selector appears in the make-up of your CSS rule.

If you had a fifth rule with selector reference points of #wrapper #content h1, then its specificity values would be 0,2,0,1. The fact that you have two id selectors referenced in this rule makes it more specific than rule 4 which only has 1 id selector referenced.

Time to look at some practical examples. The following snippet shows the XHTML you will use in the following examples; you can also find this in the example1.html file which is part of the sample files that accompany this tutorial.

<div id="container"> <h1 class="blue">I'm an h1 title</h1> </div>

Example 1: h1

In the following, you are declaring h1 as the selector. At this stage, this will be the only rule in your style sheet. The specificity value of h1 is: 0,0,0,1. You can see these values in Table 1 by reading across the Example 1 row. At present this is the only reference to h1 in your style sheet, therefore this is the most specific rule and the values it contains will be applied to the h1 element in your XHTML document.

h1 { font-size: 110%; color: #000000; }

Example 2: div h1

You can now add rule number 2 to your style sheet. Rule number 2 contains two element names; this gives this rule a specificity value of 0,0,0,2. You can see from this that rule number 2 is more specific than rule number 1, while the first three columns still have 0 values; you now have an extra element name in the selector which is the reference to the div:

div h1 { font-size: 110%; color: #66FF00; }

Example 3: div h1.blue

Rule number 3 uses the blue class that is already referenced in the XHTML document. .blue is a selector in its own right; it is a class selector and carries greater specificity than the element names you have used so far. The specificity value of rule 3 is 0,0,1,2. This rule is more specific than either rule number 1 or rule number 2.

div h1.blue { font-size: 110%; color: #0033CC; }

Example 4: #content h1

Rule number 4 references only two points in its selector, the #content selector, which is an id selector (the # character declares it as such) and the h1 element name. At first glance you might think this rule is less specific than rule number 3, as it has fewer reference points. However, this assumption would be incorrect. The id selector carries a greater specificity than any of the reference points in rule 3, it is therefore deemed to be more specific. The specificity value for rule number 4 is 0,1,0,1.

#container h1 { font-size: 110%; color: #FF0000; }

Looking at these four example rules, you can see how specificity is gauged; the more specific the references are in your selector the greater the specificity for the rule.

Working with the sample files

Open the files example1.html and example1.css in Dreamweaver, if you haven't done so already. Before making any changes preview your page in your browser of choice and note that the h1 content is black in color (see Figure 1).

The h1 is black
Figure 1. The h1 is black

Next, uncomment the rules in the example1.css file by removing the opening /* and ending */ comment markers from around the rules that are commented out. After you have uncommented these rules, preview example1.html in your browser of choice again—the h1 content is now shown in red.

The h1 element is red
Figure 2. The h1 element is red

You will find rule number 4 is the last rule in example1.css. Move it so that it becomes the first rule in your style sheet and then preview example1.html in your browser of choice. You will see that the styles for rule number 4 are still applied to your document regardless of its position within your style sheet. This will be the case wherever rule number 4 appears in your style sheet; it will always win out against the other rules as it is the most specific.

Try commenting out only rule 4 in your example1.css file and see how the next most specific rule, div h1.blue, performs in the same way as rule number 4 had previously done.

Exercise

To see if you have a grasp on specificity, create a new rule in your style sheet referencing only the following XHTML structure (this code can be found in example1.html:

<div id="container"> <h1 class="blue">I'm an h1 title</h1> </div>

See if you can turn the text in the h1 element blue without altering the XHTML in example1.html. You will find an example of how you could write this rule at the end of this tutorial (no cheating, please).

Remember, if you correctly make your new CSS rule more specific than the existing rules in the style sheet, then its position within the style sheet will have no bearing on its ability to make the text blue.

Descendant selectors

Descendant selectors are the ideal way to create selectors that are specific. I'm sure you have picked up on that point while working through the four rules I discussed previously.

A type selector for p might look like this:

p {color: black;}

This rule would make all p elements in your XHTML document black. If you required that the p element in your sidebar needed to be red and the sidebar was constructed with an ID selector named #sidebar, then you could specifically target those p elements with a descendant selector:

#sidebar p {color: red;}

The space between the #sidebar selector and the p element is known as a descendant combinator. The combinator instructs, "Look for an element with an id of sidebar and turn any p elements red." Descendant selectors can, (and often do) contain more than two reference points. If your p element contained a span and you wanted to make the span text green, then you would simply build on the previous selector:

#sidebar p span {color: green;}

For more information about descendant selectors , go to the Descendant Selector section on the W3C site.

Inline styles and specificity

Inline styles are the most specific types of CSS rules in CSS 2.1. An inline style lives within your XHTML document. It is attached directly to the element to be styled and uses the following syntax:

<h1 style="color: #ffffff;">I am now white</h1>

The specificity values for any inline style would be 1,0,0,0. As you can see, you now have a value in the left column that, as you have already learned, carries a greater specificity than the columns to its right.

If you add the inline style to your h1 element in the sample file example1.html, then you will no longer be able to change the color of the h1 element from within your style sheet. An inline style carries the greatest specificity and will override your external or embedded CSS rules every time.

The document tree

The basic structure of your XHTML file can be broken down into a document tree. The document tree consists of the base structure of your document and the elements that you have built into it. Dreamweaver provides you with a fast and efficient way to read your document tree.

Reading the Document Tree

Open the sample files example2.html and the example2.css and view example2.html in the Dreamweaver Design view. It should look like Figure 3.

Sample file example2.html in the Dreamweaver Design view
Figure 3. Sample file example2.html in the Dreamweaver Design view

Figure 4 shows the document tree for example2.html.

The document tree for example2.html
Figure 4. The document tree for example2.html

In Figure 4, you can see that the body element has two children: topcontainer and bottomcontainer. You can see these divs in your XHTML code. In turn, these two elements have children of their own, in this instance both have ol children that are being styled by the following rule, which can be found in example2.css:

ol {color: #FF0066;}

To make the appearance of the two lists different, you can create a selector for one that is more specific than the previous rule. However, before you can do this, you need to know where the ol element you want to change exists within the document tree. You can do this by looking at example2.html in Design view.

To explore the document tree from Dreamweaver's design view and learn how to create selectors with greater specificity by using this information you can either watch the following movie or complete the following steps.

Watch the movie:

  • View the movie: The Document Tree and Specificity

Follow these steps:

  1. Open example2.html.
  2. Open example2.css.
  3. Place your cursor in the Chapter Two text in bottomcontainer.
  4. Note how the tag selector shows a hierarchy of elements up to the body.
The document tree view is highlighted in yellow
Figure 5. The document tree view is highlighted in yellow

You can see from this information that a selector of #bottomcontainer ol maps to the ol element within the bottomcontainer element and you know that this is more specific than a simple element name selector. By using this information and the ability of Dreamweaver to write automatically the correct selector, you can very easily give your selectors greater levels of specificity.

The hierarchy displayed in Figure 5 shows you where your selected element exists within the document tree:

  1. Select the ol element; it is highlighted in Design view.
  2. Press Shift+F11 to open the CSS panel.
  3. Click the New CSS Rule button. The New CSS Rule dialog box opens. The Advanced option button is selected. You can see in the Selector box Dreamweaver has built a descendant selector mapped to the ol element. Dreamweaver will always use the container to make your rule more specific. This helps to prevent conflicts within your style sheet.
  4. Click the Cancel button.
  5. Select the li element from the bottomcontainer div.
  6. Click the New CSS Rule button. The New CSS Rule dialog box opens. The Advanced option button is selected. You can see in the Selector box Dreamweaver has built a descendant selector mapped to the li element.
  7. Click the Cancel button.
  8. Switch example2.html to Code view.
  9. Highlight the code between the body tags.
  10. Select the Insert Div button from the Layout tab. The Insert Div Tag dialog window opens. Insert should have Wrap around selection selected
  11. Type wrapper into the ID text box.
  12. Click OK.
  13. Save your work.
  14. Switch back to Design view.
  15. Place your cursor within the Chapter Two text in the topcontainer div. Note that the wrapper div you added now shows in the Tag Selector (see Figure 6).
The wrapper div is displayed in the tag selector's document tree view
Figure 6. The wrapper div is displayed in the tag selector's document tree view
  1. Select the ol element within the topcontainer div.
  2. Press Shift+F11 to open the CSS panel.
  3. Click the New CSS Rule button. The New CSS Rule dialog box opens. The Advanced option button is selected. You can see in the Selector box Dreamweaver has built a descendant selector mapped to the ol element, but this time it includes the #wrapper selector, giving the selector greater specificity.
  4. Click OK. The CSS Rule Definition dialog box opens.
  5. Select Type from the Category list.
  6. Click the Color: color cube.
  7. Select black.
  8. Click OK.

Conclusion

Specificity is not as complex as it may seem, but it may a take a read or two to get the specifics. Once you have a grasp of specificity and the document tree, it will make your life so much easier—selectors become easier to write and conflicts easier to resolve.

Although this article by no means covers specificity in its entirety, it does give you a good foundation and the ability to go on and investigate further. You can find more information on specificity on the W3C site.

And, before I forget, here is the answer to the exercise in this tutorial:

#content h1.blue { font-size: 110%; color: #0033cc; }

Did you get that right? I'm willing to bet you did!

More Like This

  • Responsive design with jQuery marquee
  • Understanding HTML5 semantics – Part 2: Document structure and global attributes
  • Using Modernizr to detect HTML5 and CSS3 browser support
  • Customizable starter design for jQuery Mobile
  • Customizable starter design for HTML5 video
  • Dreamweaver CS5 Missing Manual excerpts: Behaviors, site management, and templates
  • Using and customizing jQuery Mobile themes
  • Customizable starter design for multiscreen development
  • Marking up your site for easier redesign in five steps
  • Getting Started with CSS excerpts: Styling tables, backgrounds, and borders

Tutorials and samples

Tutorials

  • Understanding HTML5 semantics: Changed and absent elements
  • Mobile app with PhoneGap: Submitting to the Apple App Store
  • PhoneGap and Dreamweaver: Releasing on iOS
  • Mobile app with PhoneGap: Submitting to Android Market

Samples

  • Responsive design with jQuery marquee
  • Customizable starter design for jQuery Mobile
  • Customizable starter design for HTML5 video
  • Customizable starter design for multiscreen development

Dreamweaver user forum

More
04/23/2012 Resolution/Compatibility/liquid layout
04/20/2012 using local/testing server with cs5 inserting images look fine in the split screen but do not show
04/18/2012 Ap Div help
04/23/2012 Updating

Dreamweaver Cookbook

More
11/07/2011 Simple social networking share buttons
09/20/2011 Registration form that will generate email for registrant to validate
08/21/2011 Spry Accordion - Vertical Text - Auto Start on Page Load - Mouse Over Pause
08/17/2011 Using cfdump anywhere you like

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement