Forum Moderators: not2easy

Message Too Old, No Replies

Efficient CSS Selectors

         

LinusIT

7:06 pm on Aug 3, 2010 (gmt 0)

10+ Year Member



I am using Yslow to analyse my pages and have got stuck on one of its suggestions. It says use efficient CSS selectors but my CSS knowledge is limited so I'm hoping someone can help.

Here is the code in question:

.box ul li{padding-bottom:5px;}

Tag key with 2 descendant selectors

a.button span{background:transparent url('../images/bg_button_span.png') no-repeat;display:block;line-height:14px;padding:5px 0 5px 18px;}

Tag key with descendant selector and Class overly qualified with tag

I am keen to learn about this and hopefully "fix" my code. I have read many pages about CSS selectors but cannot get my head around it 100%.

Fotiman

7:36 pm on Aug 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!

Class overly qualified with tag

That simply means that instead of doing this:

a.button

You could simply do this:

.button

In other words, you have "overly qualified" it with tag "a". In some cases, you may need to specify the tag name if you were applying the same class name to multiple element types, but most times you probably don't need the tag name.

Tag key with descendant selector


Means that you've specified multiple levels... that is, your tag key ("span" in your second example) includes a descendant selector ("a.button"). In other words, if you had specified only "span" as the selector, you would not get this message. The same is true in your first example... you have 2 descendant selectors (.box and ul).

It's important to note that these types of analysis don't always reflect what is actually the best practice. For example, if you added an id attribute to every element in your document, and then referred to those id's only in you CSS file, the analysis would say that you're using efficient CSS selectors. But in fact, this method is very inefficient from a markup point of view, as it bloats your markup. In terms of "efficiency", these applications are approaching it from a browser point of view, not necessarily from a development/maintenance point of view. Yes, it is less efficient for a browser to apply styles where multiple selectors are involved, but it's really a matter of whether there is any perceived impact.

There is another thread on this topic here:
[webmasterworld.com...]

And a description of what is meant by efficient selectors here:
[code.google.com...]

In your first example above, the "ul" is definitely not efficient if your markup does not contain any ordered lists that would also match. In other words, you should do this instead:

.box li {...}

unless you have <ol> elements within .box that you don't want this rule to match on.

Hope that helps. :)

LinusIT

8:55 pm on Aug 3, 2010 (gmt 0)

10+ Year Member



Thanks very much for your reply Fotiman, I understand the concept a little better now.

I have changed the code as you suggested and it's worked. I have had to add a few things to make it work as it used to but that's no problem.

There are other areas on my site that flag these warnings so I'll now take a look at them and see if I can suss it out.

Thanks again