Versión en español



 


Tutorial: Cascading Style Sheets (CSS)



ID selectors

ID selectors work in a very similar way to the classes with a few exceptions. In the first place, in the definition of an ID selector the element's name is followed by a "#" symbol and an identifier ("id" value).

Code beginp#help {
font-family: arial,helvetica;
font-size: 11px;
font-weight: bold;
}

*#hframe {
border-style: solid;
border-color: blue;
border-width: 2px;
}

#hcolor {
background-color: #9EC7EB;
color: white;
}
Code end
 

Except for the "#" symbol and the name (that must be an identifier), the rest works exactly as classes do.

Using the "id" attribute

The way to apply CSS properties to HTML elements is through the "id" attribute. As you may know the "id" attribute is designed to be an identifier for the element (thus it must be unique), which means that the properties defined using this method will only be applicable to a single element per document.

In this case we could say that the properties are grouped and refer to a specific element instead of the classes that are designed for the elements to refer to them. In this example, a paragraph have an "id" value that matches one of the previous definition.

Code begin<p id="hframe">Paragraph</p>
Code end
 

Inheritance and cascading

The inheritance and cascading are the two characteristics that shape the CSS language and makes it powerful. These two concepts will give authors full control over the appliance of properties to the document's elements.

Cascading

The hierarchy of the CSS structure is one of it's most important characteristics, from where it gets the "cascading" denomination. The idea of cascading comes from the effect caused by the order in which the properties are applied to a single element from several style definitions. This may help authors to mix properties from different classes and style declarations and apply them to elements in different stages.

To illustrate this, lets take a look at this example document:

Code begin<html>
<head>
<title>Test</title>
<style type="text/css">
p {
font-family: arial,helvetica;
font-size: 10px;
color: black;
}

.important {
font-size: 12px;
font-weight: bold;
}
</style>
</head>
<body>
<p class="important" style="font-weight: normal;">Some text</p>
</body>
</html>
Code end
 

You can analyze the appliance of the properties in two ways: from the most specific to the most global or vice versa. The most specific style declaration in this example is the one made with the "style" attribute and the less specific is that made for the "p" element in the style block. No matter the way you want to see it, the declarations with higher specificity level overrides the rest.

In the example above, the paragraph will have an "arial,helvetica" font face and a "black" color from the properties defined for the "p" element, but will not have a "10px" font size because it will be overridden by the property defined in the "important" class that's applied to the element. Subsecuently, the element will receive from the "important" class a "12px" font size but the "bold" font weight will be overridden by the property defined in the "style" attribute.

The specificity order

The list below shows the specificity order used to apply CSS properties to HTML elements, from the most specific to the most global.

  1. "style" attribute (e.g., <p style="...">).
  2. ID selector (e.g., p#help {...}).
  3. Class specific definition (e.g., p.second {...}).
  4. Class global definition (e.g., *.second {...}).
  5. Element definition (e.g., p {...}).
  6. Global definition (e.g., * {...}).

Inheritance

The same analysis can be used for properties inherited from other elements. In the next example, the properties defined for the paragraph will override those defined for the body (HTML body tag). The body's properties that are not overridden, are inherited by the paragraph (because it's contained by the body).

Code begin<html>
<head>
<title>Test</title>
<style type="text/css">
p {
font-family: arial,helvetica;
font-size: 10px;
color: black;
}

.important {
font-size: 12px;
font-weight: bold;
}
</style>
</head>
<body class="important">
<p>Some text</p>
</body>
</html>
Code end
 

This document will look exactly as the one before, with the exception of the font size that won't be inherited by the paragraph from the body because the style declaration for the "p" element will override it, and the "bold" font weight that will be inherited from the body's properties.




Bypass footer options  |   Send to a friend Send to a friend  |  Post to del.icio.us Post to del.icio.us

Digg this page Digg this!  |  File on Furl File on Furl  |  Add to Yahoo! MyWeb Add to Yahoo! MyWeb

Bypass W3C declarations | 

Valid XHTML 1.0 Strict  |  Valid CSS Why should you trust us? Click the images on the left to see how seriously we write our pages, then make your choice.

Level Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0. This website gives its best effort to achieve the Level Triple-A Conformance, W3C-WAI Web Content Accessibility Guidelines 1.0. If you find any detail or error that we didn't see, don't hesitate and let us know.

The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. -- Tim Berners-Lee, W3C Director and inventor of the World Wide Web.

 Link to us  |  Contact us  |  Beyond HTML  |  Tools and resources  |  Sitemap  |  Webmaster