Don’t use ID selectors in CSS

Lately I have been testing out performance among css styles and I found that some of my very smart friends, started asking,”why aren’t you testing using ID’s for the unique sections of the page?”.

This wasn’t a hard answer:

  1. The element is not re-usable on that page.
  2. This is the begining of a downward sprial into specificity
  3. Usually, ID’s refer to something very specific, and abstracting would be tough
  4. Any performance gains picked up by using id, is negated by adding any other selector to the left fo that id

Lets delve into each of these issues at more length

The element is not re-usable on that page:

Id’s are programmer’s equivalent to singletons.  There can only be one instance on the page.  This means there is no way to re-use it.  It’s usually a one to one relationship, and according to my speed testing, one line of css that’s only usable once is not a good value.  There IS a cost to css bloat.

This is the beginning of a downward spiral into specificty:

There are two main ways of overriding in css.

  1. The cascade: (anything further down the css, can overwrite the previous css rules)
  2. Specificity: the idea of creating weight by using weighted selectors.

The reason I say that specificity is a downward spiral is because the only way to overwrite a weighted rule is to add more weight..  plain and simple.  Now I am not just saying this because I heard it somewhere.  I have made and paid for this mistake.


.ModuleOfficeList .property-checkbox input
{display:block;margin-bottom:8px;_border:0px !important;}

.ModuleOfficeList .property-checkbox,
  .ModuleOfficeList .new-icon,
  .ModuleOfficeList .open-icon
{display:block}
...
.uid-officelistings .property-checkbox
{display:none !important; }

Above is real code from one of my own work from 2005.  As you can see, after awhile, I had to resort to some weighty selectors and the !important rule. This is very bad. Once we get to this point.. it takes us more time to hunt down the parent id’s we are going to use in order to overrule the current specificity. This is not team-friendly and definately not maintainable. Eventually you will specify yourself into a hole, and refactoring out specificity is nothing short of a nightmare.

On the other hand:

I have heard a few reasons why using ID’s as selectors is a good thing. I’ll speak on them briefly:

Using ID selectors is faster
Yes, using ID selectors is faster than using class selectors for the simple reason that there can only be one id within a page. I even prove that is true, however the performance benefit is very small, and as soon as you add any other selector, the performance benefit is lost.


#profile-module {...}
#profile-module li {...}
.profile-module li {...}

The second selector is no faster than the third. That’s because CSS is read from right to left. so the
li
gets scanned first, which negates the speed of having an ID. Steve Souders explains how selector speed works.

ID’s are meant for singletons
So this is the best reason I have heard so far, for using id selectors. There are times when someone intentionally wants something to be used only once on a page. ID selectors would be useful for that purpose since they would signify once per page. My only argument here is my personal preference of having all my code be re-usable; especially since I don’t see CSS in a “programming language” way.

So in conclusion, I suggest not using id selectors, however, there are others that believe using id selectors do have purpose. In the end, the choice is yours to make.

Posted in css, webdev | Leave a comment

Using the window.location property in Javascript

I often forget how this works, so here is the different properties of window.location.
Thanks to DevGuru for the data.

To see how these work, pop open your firebug and add them to your console box and run them.

The properties:

location.hash

The hash property is a string beginning with a hash (#), that specifies an anchor name in an HTTP URL.

location.host

The host property is a string comprising of the hostname and port strings.

location.hostname

The hostname property specifies the server name, subdomain and domain name (or IP address) of a URL.

location.href

The href property is a string specifying the entire URL, and of which all other Link properties are substrings.

location.pathname

The pathname property is a string portion of a URL specifying how a particular resource can be accessed.

location.port

The port property is a string specifying the communications port that the server uses.

location.protocol

The protocol property is the string at the beginning of a URL, up to and including the first colon (:), which specifies the method of access to the URL.

location.search

The search property is a string beginning with a question mark that specifies any query information in an HTTP URL.

Posted in javascript / jQuery, webdev | Leave a comment

Loading your javascript files asynchronously

So it’s widely understood now, that performance has a direct effect on the traffic of our site.   This puts performance at a much higher priority than before.. especially if your site traffic is what pays the bills.  Steve Souders began working on website performance, initially thinking that backend performance was the key, but he quickly found out that most of the performance could be gained on the front end, with far less work than optimizing sql queries and refactoring backend code.  Since he started publishing his work, we have seen a progression of understaning what helps our websites perform better.  Here is a list of things that we can look to accomplish quickly and see vast performance improvements.

  • minification of files
  • zipping css / js files
  • pulling all css files into one
  • pulling all js files into one
  • creating image sprites that combined many images into one big sprite
  • avoid using IE specific css expressions
  • using a cdn to serve files
  • loading js at bottom of page
  • serving files from a different domain
  • serving files asynchronously after page has loaded

Want to know more about the above performance enhancements?  Go to the Yahoo Developer article explaining them in detail.   Also keep in mind that your mileage may vary.  We found this out at my current job, since we took are most popular pages, and removed all the less important parts of the page, and added them via ajax, after the page loaded.   This made the pages load screaming fast!  However, our server peaked out very quickly because it was doing about 5 times the amount of requests.  Each ajax request, even when cached, requires resources from your server.  Needless to say, we don’t have the money to drop into several more servers, so lazy loading via ajax will have to wait. Keep things like that in mind when adding performance techniques to your site.

Okay Okay, get on with it!

So you have an idea of whats going on with website performance, and the last part of the list was about serving files asynchronously after page load.   Here is a little script that simply loads files after the page has loaded

And to load these files, you only need to call the function with the script as the argument

Really, that’s about it. Fairly simple script as you can see, yet very powerful, this was basically borrowed from Google Analytics implementation of their asynchronous loading analytics.
One thing to note: I am loading the files in the opposite order than usual. The jquery.js file should be loaded first in the dom.. but since we are using insertBefore.. the first must be last and the last must be first.

Posted in javascript / jQuery, webdev, yahoo | 1 Comment