CSS Rehab – 3 Step Program To Getting Clean

I’ve been making websites commercially for about five years now. In 2004, a year or two out of college — it was definitely a learning process for me. I originally graduated with a degree centered on print design, so programming in general was very new to me. During that time, table-based layout structure was still prevalent, so that’s how I started out.

CSS Illustration

I eventually added CSS to slowly wick away the pain of tables by using simple global CSS selectors (i.e. body, link, img and etc.). At that time, I would say that’s what most people did. It was still very difficult for pure CSS based layouts because the browser situation was still awkward.* Internet Explorer was at it’s prime and Firefox was not as popular and easy an alternative as it is now.

[*This is from my experience, as I remember specifically working on a client project in 2003-2004 how hard it was to follow tutorials as every one had so many browser caveats. I'm sure my inexperience contributed to the difficult as well.]

Needless to say, my HTML/CSS was a mess even after I started using CSS the way it is supposed to around 2006-2007. This is the time frame I’d say Firefox really started getting traction and soon after IE7 came out in 2007 making everybody’s life so much easier. The main problem was that even though I used CSS, the actual implementation was kind of jagged. As a designer/developer — it’s important to be clean, simple and elegant (as Jamie from Myth Busters likes to phrase technically challenging concepts implemented efficiently) as it helps to debug, transfer, share, update, teach and perform best practice techniques.

Even now, I can do better but here are 3 techniques I practice religiously that have really benefited me after adapting them into my routine.

STEP 1: RESET – Let’s Code on a Level Programming Field

Since Eric Meyer made resetting browsers popular with his influence/community in 07′— it started to build in popularity quite rapidly. As soon as I started using it, I immediately thought to myself how great it was as many problems started fading away very quickly. Sure, we all used mini resets like setting margins and padding to “0″ in the html or body selectors but completely eviscerating all browser defaults is a wonder you have to experience yourself.

If you take nothing away from this article — make sure you start using CSS resets. It’s been one of the biggest difference makers in reducing day-to-day CSS headaches (especially between browsers). Eric Meyer is generally accepted as a CSS authority and his style sheet is generally accepted as the best:

From: http://meyerweb.com/eric/tools/css/reset/index.html

[Link updated: 7.8.09]


html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
:focus {
outline: 0;
}
ins {
text-decoration: none;
}
del {
text-decoration: line-through;
}
table {
border-collapse: collapse;
border-spacing: 0;
}

The way I use this is by linking to it as a separate external style sheet as the first cascade.

STEP 2: FRAMEWORK – No Need to Re-Invent The Wheel

I initially did not flock to CSS Frameworks but have personally experienced the benefits from using them these last few years. CSS is pretty simple once you gain experience using it, so going through the trouble of using a “bloated” framework seems like BS to some developers. This is especially true for those Jedi web developers who have made dozens and dozens of websites through the years and have developed their own way of doing things.

There are plenty to choose from, the two I am most familiar with is Blueprint and a pseudo framework called 960 Grid System (GS) that predominantly focuses on standardizing grid layouts. Once I started using 960 GS, I never stopped. The main reason behind that is because it takes away arbitrary decisions that change from website to website.

CSS Frameworks: 960GS and Blueprint

For example — what size should the sidebar be from website to website? In the past, it could range from 251px to 327px. Yes, circumstances may require you to make the sidebar exactly 243px wide but if you design it with the grid in mind in the first place — you can avoid these awkward numbers. That can become difficult to manage as you work on several new websites during the course of a day and even more if you do work updating old ones. 960 GS logically gives you several options to design from and as you implement the framework — it’s consistent and makes sense no matter what website you work on.

The way that all the frameworks work is that they supply you with basically a template — but a bare template that’s flexible and versatile enough that web professionals will not reject it for lack of adaptability and can use it for any conceivable future project. Blueprint unlike 960 GS goes much further and offers all the selectors but IMO, it starts to get bloated. However, that’s the beauty of CSS frameworks — you can choose the one that makes sense to you. There probably is a Goldilocks framework out there waiting for you.

Here’s a list of some of the most popular for you to explore:
http://960.gs/
http://www.blueprintcss.org/
http://www.yaml.de/en/

w3avenue and SpeckyBoy have explored more of the frameworks more in-depth:
http://www.w3avenue.com/2009/04/29/definitive-list-of-css-frameworks-pick-your-style/
http://speckyboy.com/2008/03/28/top-12-css-frameworks-and-how-to-understand-them/

Jeff Croft waxes poetic about this subject over at A List Apart
http://www.alistapart.com/articles/frameworksfordesigners

I actually saw Eric Meyer speak about CSS frameworks at An Event Apart SF 08 and the conclusion he left us with is that each one has good and bad points but most stress standards compliant code and organization… so using them certainly can’t hurt.

STEP 3: ORGANIZING – It Works For Your Socks, Why Not Your Code?

[NOTE: Step 3 is hard. It's a bit nit-picky but hey — we're all trying to get better right?! I know I need to follow my own advice better at times!]

You’ve heard this before and like me — you’re too busy writing the code to fuss with it too much. That’s the reality but there are clear benefits as well. CSS = Cascading Style Sheets. Organization is inherently built into the functionality of the language! We should be striving to be clean and easy, and here’s a few quick tips that any of us can start doing right away without much fuss:

Selector Grouping

h1 { font-family: sans-serif }
h2 { font-family: sans-serif }
h3 { font-family: sans-serif }

is the same as:

h1, h2, h3 { font-family: sans-serif }

and a real world example of the space you’d be saving is:

h1, h2, h3 { font-family: sans-serif }
h1 {2.5 em}
h2 {2 em; color#000000;}
h3 {2 em; color#333333;}

Reducing some of the redundancy will really help make your CSS be more efficient and save you time as well!

Default Selectors First

Define the values of these at the top of the document and keep them there. I’ve seen some style sheets where they group similar selectors together with the assigned selectors such as putting form selectors with the rest of the structure and styling of the form. It makes some sense but it’s bad practice because it’s hard to locate later on and especially frowned upon if you work with a team.

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td

This actually happened to me… a co-worker a few years back told me I should stop sprinkling my selectors everywhere. He had to use the Find tool all the time to figure out what the hell I was doing. Don’t annoy your co-workers like me!

Alphabetizing Selector Values

Yep. It’s as simple as that and yet it’s so hard to do.

h1 {
background-color: #000000;
color: #ffffff;
font-family: sans-serif;
padding: 20px;
}

I read about some developers doing it but I always opted for grouping similar/popular values together. Such as width and height, padding and margin and etc. The reason why I’ve come over to this way of thinking is because I realized when I deliver code to a client’s IT team — it makes sense to them that it’s alphabetical since they don’t work with me and don’t know my quirks. In other words, my arbitrary grouping of selectors means nothing to other people. Alphabetical makes sense since it’s a simple and functional way to organize.

In Conclusion…

The reasons why you should strive for clean CSS is to code faster, be efficient and to not annoy your co-workers and clients.

  • 1. RESET!
  • 2. Use a Framework!
  • 3. Organize within your code!

Thanks Darwin for the photo!

Helpful? Comment!

25 Appreciated Comments to “CSS Rehab – 3 Step Program To Getting Clean”

  1. Dan says:

    Some great tips :D

    I’d disagree with alphabeticising rules though; this isn’t “the way to do it” it’s just a way that some people do it. I’d be confused as hell if I organised my CSS in this way. There is no benefit to doing this, no performance gains, and it doesn’t make it any easier for the browser or anything like that, like I said, it’s just how some people do it. A better tip would be leaving a lot of single line comments in the code (as these are automatically removed when the CSS is minified (using YUICompressor or similar).

    Personally I group rules that are related, e.g. position, top, left followed by display, width, height etc. With rules pertaining purely to presentation rather than structure last, e.g. background, color, etc

  2. TypeSett says:

    Thanks Dan! Great point!

    Honestly — I agree for personal use. For this website, I didn’t go that far… and I think that’s the distinction. Only I will see the code.

    However, for some of the clients we work for I’m trying to maintain an air of professionalism and why should I subject them to my neurotic CSS tendencies? When I was learning “float” back in 2003, it gave me a real hard time. For whatever reason, I always used to put it first on my definitions list. Other people always start with margin and padding I noticed and some like font…

    I don’t see the harm in alphabetizing if you plan to share code. Also, this is because I’m a four-eyed freak… i actually will repeat a definition by accident because they are visually separated. Alphabetizing does have minor benefits.

  3. [...] CSS Rehab – 3 Step Program To Getting Clean [...]

  4. Ezrad Lionel says:

    I don’t think anything should be standard after a reset.css. CSS frameworks are more like presets rather than frameworks. Unless it suits your style, you might as well start from scratch. I see so many CSS files 100k+ trying to keep the CSS classes structures, when a table would do. Really, I think this is the last time I click on an article with CSS in the title. You standards guys are creeping me out.

  5. TypeSett says:

    @Ezrad Lionel – It’s really up to you to decide where you want to take your code.

    The websites that I work on are not really large enough that I have to worry about bandwidth conservation. I worry primarily about client-side loading times. The trade-off of clean, sharable CSS is more important to me than having tiny files.

    Thanks for your comment though.

  6. Andy Ford says:

    The linked Eric Meyer reset is out of date. Make sure to use this version instead – http://meyerweb.com/eric/tools/css/reset/index.html

    As Dan mentioned above, I also prefer to organize CSS properties by their relationships rather than by alphabetizing them. However, I think it’s very much a personal preference and I’d rather edit a CSS file that had *some form* of property organization rather than none at all. For what it’s worth, here is a property ordering guide I put together – http://aloestudios.com/code/css-property-ordering-guide/

  7. TypeSett says:

    @Andy Ford – thanks man. i knew something was wrong because i googled it twice. i even went to the homepage but it looked like it was updated recently (and comments were open) so I went with it. Muchos gracias!

  8. Oreana says:

    I don’t know if I can get into alphabetizing values… I usually start with what comes to mind first. Then, I end up going back and making sure that similar things (like headings) have their values in the same order. Yup, could probably save some time by just having a default order.

  9. [...] This post was Twitted by joytreyes [...]

  10. guide says:

    i really loved your site and found it to be very friendly and helpfull.

  11. [...] Read more: CSS Rehab – 3 Step Program To Getting Clean « typesett – Your AMUSING Resource for Useful Design, T… [...]

  12. DrDiv says:

    Great article! Thanks for sharing your experiences.

  13. I must say that I was surprised to find this web page, but – - – Good Job.

  14. rules for texas holdem poker says:

    I must say that I was surprised to find this web page, but – - – Good Job.

  15. susan says:

    I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

    Susan

    http://texasholdpoker.info

  16. Greetings from the UCA (Unruled CSSers Anonymous…). Good tips.

  17. Del Roddo says:

    W3C validator finds 129 Errors, 23 warning(s)
    on your page. CLEAN IT UP or stop posing as an expert.

    Del Roddo

  18. admin says:

    @Del Roddo – Right now, I’m not so interested in being validated as this is a side project but when typesett 2.0 comes out, I’ll try to be less lazy.

    I owe you a beer! Thanks!

  19. admin says:

    @ Marcos Watanabe – Thank you sir, just some ideas for you to chew on.

  20. Carmen says:

    I loved reading this article. I’m in love with Clean CSS!!

  21. EpKonnie says:

    If you try to find locality where you can get resume writing service here is very perfect place for you about this good post, which arrange examples and gives an fortune to learn how make great CV resumes . But this site is more charismatic, and more constructive.

  22. cahbreis says:

    Great read. Thanks for all the info.

  23. Xamuel says:

    Some great tips… I was always a programmer and now that I’m moving toward the internet, I’m having to learn a lot about style and CSS. Never even thought of alphabetizing sector values.. shameful to say but I’m guilty of some pretty arbitrary css-sprinkling :P

  24. admin says:

    @Xamuel – Everybody does it at times, just progressively clean it up as you learn more and more.

  25. Hi, thanks for the tip! I wish I could just grasp CSS a bit better… and I can program MySQL, HTML, C++, PHP, etc. Any suggestions for people to help? Maybe you? Anyway, cool blog – I’m subscribed to your RSS feed now so I’ll be checking in regularly!