Friday, January 26th, 2007

Debug CSS with the Firefox DOM Inspector

Filed under: — Daniel Lemire @ 10:18

While it allows us to make HTML pages really beautiful without cluttering the HTML code with presentation artefacts, Cascading Style Sheet (CSS) is a rather difficult declarative language. Selectors are not trivial and rules overwrite each other without warning. Consider this example


a, b >a * {color:red;}
b a {color:blue;}

What happens when an element “a” is encountered? Which color is it? If you think this never happens, consider that a Web page can call several CSS files (using, for example, the @import statement).

Until recently, I thought that the Firefox DOM Inspector (under the Tools menu) was merely a useful way to browse the DOM tree of a document. It took me a while to discover why people were so excited about it. As far as I know, Firefox is the only browser with this feature and it is a must.

One thing it can do is to allow you to explore and understand how your CSS rules get applied. To see how it works, select a node in the left pane (choose an element with non-trivial CSS properties, like table, td or p or span), then in the right pane you should see its name, its URI, its type… Click on the small icon at the upper left corner of the right pane. Go to CSS Style Rules. Then, always in the right pane, you should see the CSS rules that have fired for this node and in the order (recall the CSS rules can overwrite each others). If you click on the rule, you see its consequences in the lower right pane. Next, go to Computed Styles (click on the small icon at the upper left corner again)… and see all of the CSS properties your node has (after all rules have been applied). By the way, you can also, by the same process, know exactly where on the page the node is and what its geometry is (go to Box Model).

1 Comment »

  1. Daniel, you definitely want to check out Firebug:

    http://getfirebug.com/

    It’s excellent, lets you inspect CSS, DOM, Javascript, and just about anything else you can imagine. It’s by far the best development tool I’ve seen for Web development.

    Comment by Parand — 26/1/2007 @ 12:08

RSS feed for comments on this post.

Leave a comment

Warning: When entering a long comment, please ensure that you make copy of your text prior to submitting it. If the server should fail or if you hit a bug, you might lose your work. I am not responsible for your lost effort.

To spammers: I carefully review every single post and make sure that spam gets deleted. You are wasting your time if you are manually entering spam using this form. Read my terms of use to see what I consider to be abusive.

Example: I + II + IX= XII. Yes, you have to enter a roman numeral. (Answer must be in upper case.)

« Blog's main page

25 queries. 0.296 seconds. Valid XHTML

Powered by WordPress

Subscribe to this blog in a reader or by Email.