Hello there. I am Terry and I am a full-time undergraduate based in Singapore. I take photos, write a blog and design websites.

And no, I'm not a teddy bear.

5 Ways to Instantly Write Better CSS

Five Ways of Writing Better CSS

Five Ways of Writing Better CSS

Recently I read about an article that teaches web designers five ways to write better, neater and cleaner CSS by Trevor Davis, after following a link in a tweet.

In case you’re still wondering what CSS stands for, it actually means Cascading Style Sheets – the stylesheet part is easy to understand. CSS is created with a notion to separate content and styling, which explains why the <font>, <center> and etc. tags are deprecated and no longer accpeted in (X)HTML. The cascading part is a little tricky – it means that you can actually override certain properties in the stylesheet by being more specific in your selectors – for example, span.highlight can be overridden by div.content span.highlight if the <span class=”highlight”> is located within <div class=”content”>.

Back to the post itself, here are the five ways Trevis recommended, with my personal opinions/comments. Of course, for more details you will have to follow the link (or else I’ll be slapped with a plagiarism charge):

Reset

I never had a habit to use a reset stylesheet in the past, and the different default styling applied to elements (such as a certain margin and padding for the <body> tag, for example) drove me mad. It was until Jeff from Perishable Press wrote about them that I realized how important they are. For the moment being, my blog is using the 960 grid reset since I’m using the grid system as part of my layout. In the past, I’ve used Tripoli reset, and it was quite a good experience!

Alphabetize

I don’t think it serves much purpose, but the perfectionist side of me forced me to alphabetize my entire stylesheet as much as possible, except for the dimensions of the element (width and height), which I will place last.

Organization

I do organize my style sheet a little, just in case any curious person would like to took a peep at it. I divide my stylesheet into several sections. Although they don’t help much with navigation around the style sheet (I will just use the search function instead), they do keep things in order.

Consistency

Now the tricky part. I remembered reading a debate on what kind of way people prefer to organize their stylesheet – single line vs multiple lines. Let say we take one from my current style sheet, the single line way will look like this:

.entry-title a span.title-comments {background: transparent url(images/comment_bubble.png) 0 0 no-repeat; color: #cccccc; font-size: 90%; letter-spacing: -2px; margin-left: 4px; padding: 2px 2px 2px 16px;}

A multiple-line one will look like this instead:

.entry-title a span.title-comments {
   background: transparent url(images/comment_bubble.png) 0 0 no-repeat;
   color: #cccccc;
   font-size: 90%;
   letter-spacing: -2px;
   margin-left: 4px;
   padding: 2px 2px 2px 16px;
}

Of course, the first method saves you the line-breaks and your finger from all the scrolling madness, while the second method makes error-spotting easier – when you place the code under validation test, and when an error is found on a certain line you will immediately know which one went wrong.

Start in the right place

Just like what Trevis puts it, “Don’t you dare touch your stylesheet until you have written your markup!” It is imperative that you get your markup right first – which includes ditching the hated table layout, removing deprecated elements, checking your file with a validator before hand – before moving on to CSS.

Burn after reading » Now you're done reading. What's next?

Related

Related posts that might interest you:

Popular

Posts that are popular among visitors:

Share it

If you've enjoyed the post, or think that it might be useful to others, do share in on a social network site that you're using. Thank you!

  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • LinkedIn
  • Reddit
  • Design Float
  • Identi.ca
  • Live
  • Ping.fm
  • Print this article!
  • Twitter
  • Netvibes
  • HackerNews
  • Slashdot

6 responses to “5 Ways to Instantly Write Better CSS” » Leave a response

  1. Jeff StarrResponse

    These are excellent tips, teddY — thanks for sharing. I like to keep things neat and tidy, but have yet to discover the joys of declarative alphabetizing. Instead, I prefer to organize my declaration blocks according to the number of characters contained on each line, such that the result looks like a nicely formatted chunk of code with consistent right text edges. I have been ridiculed for doing it this way, but I don’t care because I think it looks clean, crisp, and complete. Anyway, just wanted to thank you for sharing these great reminders with us. And thanks also for mentioning my article on CSS Resets — much appreciated! :)

    Check out Jeff Starr’s latest blog post » Time for a New Design?

  2. NoktahHitamResponse

    Nicely written Teddy.

    I don’t think Alphabetize would work wonders. For me the break down is Header, Content and Footer. Key CSS like font style will always be at the top.

    Personally, my favorite is a single liner. Some call it the Pro’s way, but I personally think it depends on the coder. I don’t like scrolling a million miles south. Afterall, beauty lays in the eye of the beholder, that includes perfection as well ;)

    If Travis warns about touching CSS before Markup, let me warn something else.

    *Dont you dare touch those keyboard if you haven’t drawn anything on a paper!*

    Check out NoktahHitam’s latest blog post » Utusan Does It Again, Twist-Twist-Twist

  3. abdusfauziResponse

    a comprehensive post teddY.
    for me, i’m using multi lines for every class or IDs. So far, it looks good, but i figure out that, this is one of the reason im having severe injury on my right hand wrist and arm. hahaha. too much scrolling i think. hehe.

    Check out abdusfauzi’s latest blog post » A Very Quick Post : Dedicated to AisyahKama

Leave a Response » Share your thoughts or Return to top

Your name is required. Why?

  • So that I can address you personally (which I'm more than happy to do).
  • Everyone loves their name to be called, right? Nobody wants to be referred to as 'a certain someone' or Mr X (X-Men members aside).
  • If your post too much spam, your name will be imprinted on a personalised voodoo doll, completely free of charge. I'm just joking!

Your email is needed. Why?

  • Your email is, and will, never be given out to any third party under any conditions (except for people sneaking up behind me when I'm on WP admin).
  • So that I can contact you personally in case of certain issues that I wish to address personally.
  • If you post too much spam, I'll email the same spam you've posted 10 times back to you, completely free of charge.

You can always leave a personal URL behind

  • It can be anything - most people put their blog URL, their facebook page, their Twitter URL, their MySpace or etc.
  • I give love back by saying NO to no-follow. All of your webbie links are dofollows instead.

Twitter. Twitter. Twitter.

  • What is Twitter?: Twitter is a social networking and micro-blogging service that enables its users to send and read other users' updates known as tweets. Tweets are text-based posts of up to 140 bytes in length.
  • What is my Twitter ID?: To get a Twitter ID, you simply have to sign up for an account at Twitter.com. Your ID will be in the format of your profile's URL - http://twitter.com/[userid].

Comment notes

  • Gravatar: You can register for one at Gravatar.com
  • XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
  • Codes: Wrap them up with <code></code> tags.

Hello Easter Egg hunter! You've discovered the magic of the Konami Code!

Yea. You probably know what the Konami code is before getting to this page. So now what? Here is a randomised YouTube video on my favourites list... which includes Rickroll, if you're luck enough to get it.

You are currently watching Natasha Bedingfield - Unwritten. Loving it? You can even watch the video in its full screen glory :)

The Konami Code:
Konami Code sequence