demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

Understanding UA StyleSheets and the CSS3 initial Value

Imagine that you build a page using only , with no CSS attached or applied. When you render the page in a browser, how does the program know to display links as blue and underlined, and paragraphs as black? Why is the margin for h1 elements 14pt in IE by default, and 0.67em in Firefox?

The answer to these questions is User Agent stylesheets. UA stylesheets are CSS files built into the browser. Each browser has its own UA file, which it uses to determine how to render every website, unless the browser is explicitly told otherwise.

The way we tell the browser to use the site’s CSS, rather than the UA styles, is by linking or embedding a stylesheet to a page, or by using inline styles. The browser then makes a simple decision:

  • If the new styles conflict with the browser’s UA stylesheet rules, the site’s style declaration will be used when rendering the element.
  • Anything the site style rules don’t specify will be assumed by the UA stylesheet.

This leads developers to create mammoth reset stylesheets that attempt to set every CSS property to a shared default value; applied to a web page, this effectively wipes out any effect of a UA stylesheet. The designer/developer can then build custom CSS for the site, somewhat assured that browsers will not unexpectedly assume a value for some property they have left unspecified. (You can see the UA CSS for IE, Opera, Firefox and Webkit at IEcss.com.)

CSS Resets are not without controversy; as I have discussed, I personally prefer a less heavy-handed approach.

One of the things CSS3 has changed is making the increasingly-similar UAs our friend, rather than our enemy. For example, take the following snippet of HTML with some inline CSS:

  1. <div style=”color: red;”>
  2. <p>This is some text <span>in color.</span></p>
  3. </div>

The color of the paragraph text, inherited from the parent div, will be red. What if we wanted to set the text inside the span to black? There are two choices. The traditional approach is this:

  1. <div style=”color: red;”>
  2. <p>This is some text <span style=”color: #000;”>in color.</span></p>
  3. </div>

introduces a second option:

  1. <div style=”color: red;”>
  2. <p>This is some text <span style=”color: initial”>in color.</span></p>
  3. </div>

initial is a value that resets a property back to the default UA style. This can be particularly useful when the style you wish to negate may be, long, complex, or difficult to remember:

  1. div { border: 3px dotted green;  }
  2. div.special { border: initial; }
  3. /* resets the border on divs with a class of “special”
  4. to that specified in the UA, i.e. no border; } */

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.