Redundant CSS
I had a client call me a couple of weeks ago in a rush as they were trying to get their newly designed web site launched. Although I did design the site they parted ways with their on staff designer about three weeks before the targeted launch date. So I agreed to go help them get through the last few days before launch.
They gave me a list of what should be simple things to change via CSS like, font color and size, link behaviors, etc. After I spent a little time getting a handle on the site’s structure and briefly looked at the site’s CSS file I figured it would be simple to tackle their list in a timely matter.
However when I reopened the CSS list I knew I was in for a more then I bargained for. (I should state before I get to much further I just don’t like working with someone else’s code. I’m pretty picky about how I write things and the order I write my CSS in. That doesn’t mean I won’t touch someone else’s code, I just don’t like it. I always feel like I’m working in slow-mo because I have find stuff I didn’t write and don’t know where it is.) Any way, I after I opened up the CSS file I saw it was an amazing(not in a good way) 2065 lines of code. In case you don’t know that’s a lot, for example this site has a slim 188 lines in it’s CSS file and that with a number of empty lines I leave to help break it up into sections.
Back to the 2065 lines file. This really took me back because this wasn’t a site with tons of pages. There were maybe a 100 pages total with I would say 90 of them were based of one template, meaning all the pages were laid out the same and and the same major elements just the content on the page was changing. When I really dig into the file I found what was making the file so large with so many lines of code, there were CSS redundancies all over the place.
Note — I not showing all code because some of this is from a live site, so you’ll have to take me at my word about the HTML structure.
One Example was this style was repeated 7 times:
a:hover{ color: #53439; text-decoration: underline;}
Why seven times? I don’t know. Now just that one line was in there seven times. But that style was in there seven times in different lines like this:
#content div.ajax div.links ul li a:hover{ color: #53439; text-decoration: underline;}
#wraper div.info div.mid-col ul li div.holder ul li ul li a:hover{ color: #53439; text-decoration: underline;}
#wraper div.map div.industry-news div.industry-content ul li a:hover{ color: #53439; text-decoration: underline;}
Every one of those lines were totally unneeded because they were all calling the same style that could have set globally with just:
a:hover{ color: #53439; text-decoration: underline;}
I know everyone likes to so things differently, but personally I like to keep it simple so I’ll set a style like this in the top of my CSS file and let it effect all of the links on the site. However there are times where I want some links to act different then the global style. So I’ll write other style to effect only those links. My bigger point here is repeating the same styling seven times only makes things harder in the future where someone decides they no longer like the color of the links. So ask yourself this, would you rather change one line of code or seven?
And all of the styles in the file were in the form of something like this:
#wraper div.map div.industry-news div.industry-content ul li a:hover{ color: #53439; text-decoration: underline;}
Most of it totally unnecessary and was just helping bloat the file size, so let break it down and clean this up a bit.
- #wrapper — This tells the style it’s in the id=wrapper. First you can not have more then one type of ID per page, i.e. there can’t be two wrapper IDs on the same page. And in the structure of both the pages and site this was not needed.
- div.map — I’ve never got this. Basically it’s saying the map class is in a div tag, meaning in the CSS .map is the same as div.map. I use styles like h1.title or p.title because I sometimes need different styles for my title class. But here then was only one .map class and that one .map class only was used on one page. It is a way to make sure you are selecting just that one div, but most of the time I just don’t see the point.
- div.industry-news & div.industry-content — See above. With the added bonus that both of these were empty classes, meaning there were no styles and I’m really not sure way.
- ul li a:hover — Here they are targeting an anchor tag, thats’s inside a li tag, that’s inside a ul tag or a list item that’s a link. This done when we want to change the styling of a link away from a global link style. I do this quite a bit when it comes to menus and sub menus. And :hover means when the pointer is sitting on top of the link before it’s clicked.
So if we remove all of the unneeded text in this style what are we left with?
#wraper div.map div.industry-news div.industry-content ul li a:hover{ color: #53439; text-decoration: underline;}
or
.map ul li a:hover{ color: #53439; text-decoration: underline;}
Again just setting one global style of a:hover{ color: #53439; text-decoration: underline;} makes the whole line is unnecessary.
In all the CSS file weighted 44K or 44 Kilo-bytes, which I think is a lot for a CSS file. It caused the site to load much slower then it should because the browser would load all of the CSS styles before it the rest of the page.
Remember when you are coding adding anything unneccesary is only going to cause problems later. What should be simple changes turn into long ones because of the extra time it takes to change 7 styles instead of just one.
- Go State! Sorry I'm be hide, was watching on DVR.
- It's like Ron Prince never happened, back to the old jerseys.
- http://icio.us/qtgamq
- 10 Useful WordPress Plugin Recommendations Based on Personal Testing http://ping.fm/mDJ2W
- Ping is an interesting addition to ITunes...











