5 tips for better CSS organization

Organizing your CSS the right way can be a real time-saver, especially when you come back to work on a file that you didn’t touch for a while. These little tips are not an exhaustive list, but they should help you to get a better organization in your CSS files.

  • Create a distinct CSS file for your hacks
    In a perfect world we wouldn’t need hacks because all browsers would be standards compliant. Unfortunatly lots of people are still using Internet Explorer 6 out there, and depending on what you want to achieve you may be forced to use browser-specific hacks. If this happens, having those hacks in a separated CSS file (eg. ie6-hacks.css) will make your life easier once the browsers have evolved.
    Some people take this technique one step further and use multiple CSS files to get organized. I would only do it when their is specific CSS for a website’s section (eg. styles for a complex event calendar).
  • Organize the stylesheet in sections
    By creating sections, you’ll make it easier to scan through the stylesheet to find the properties that are situated in a particular region of your page.
    You can create these sections separations by using CSS comments, as in the following.
    /* HEADER */Personnaly, I also use capital letters to make these comments stand out.
  • Make the cascade more visual
    One more time, this will help you to go through the stylesheet’s cascade faster by tabbing the children elements to get a visual understanding of the hierarchy.
  • Alphabetize selectors within the sections
    Just as simple as it sound. Within the sections you’ve created in your file, organize your selectors going from A to Z. This will make the eye scanning of your file more logical and efficient.
  • Use shorthand properties
    This won’t be long to learn, but really helpful to unclutter your CSS file. Shorthand properties will allow you to set multiple properties in one declaration.
    For example:
    margin-top: 5px;
    margin-right: 10px;
    margin-bottom: 8px;
    margin-left: 3px;
    will become
    margin: 5px 10px 8px 3px;CSS shorthand can be used for the following properties: font, margin/padding, border, list-style, background.
9 Votes, Rating: 3.331 Star2 Stars3 Stars4 Stars5 Stars

Advertisements

This entry was posted on Thursday, December 4th, 2008 at 1:19 pm and is filed under Blog, CSS, How-to. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

12 responses so far!

  • 1 Jumping Dog Design ~ Dec 4, 2008 at 4:46 pm

    Possibly different stylesheets for different sections ie. layout, navigation, common etc…

  • 2 Julien ~ Dec 5, 2008 at 6:35 am

    you can go to http://www.codebeautifier.com to alphabetize selectors automatically (and many other things).

  • 3 Daily Links | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster? ~ Dec 5, 2008 at 5:05 pm

    [...] CSS Orgy » Blog Archive » 5 tips for better CSS organization Quick tips for better CSS Organization (tags: CSS) [...]

  • 4 Design links for the week | Design daily news ~ Dec 7, 2008 at 4:35 am

    [...] 5 tips for better CSS organization Advice on how to improve the organization of your CSS files. [...]

  • 5 Rachel Nabors ~ Dec 7, 2008 at 10:10 am

    Nice tips! Thank you for sharing.

    Although, I admit that I organize my CSS by color, typography, positioning etc.

  • 6 Artha ~ Dec 8, 2008 at 6:35 am

    Thank you for css tutorial, very usefull

  • 7 Dave ~ Dec 8, 2008 at 6:22 pm

    Your point on capitalizing comments is a good one, I haven’t thought of it yet, thanks!

    Please note that having multiple stylesheet files can also be a bad thing. Each visit requires several more http requests, slowing down the user experience. That said, using a separate stylesheet ie. for the homepage (as it tends to haver parts that are too different from other pages) can be a good thing.

    In my case I tend to sort the selectors by what they do. I usually start with selectors that modify the looks of the object, then margins/paddings, then backgrounds and the like, then font-related items such as color, size, etc. This kinda goes naturally for me.

    Thanks for the tips!

    -Dave

  • 8 my blog ~ Jul 2, 2009 at 4:49 am

    check this out…

    this is mine…

  • 9 My Domain ~ Feb 4, 2010 at 5:22 pm

    Joe…

    Check out my domain sometime….

  • 10 hermes kelly bag ~ Jul 8, 2010 at 10:38 pm

    check this out…

    this is mine…

  • 11 best food processor reviews ~ Oct 4, 2011 at 7:11 pm

    food processorreviews reviews…

    [...]CSS Orgy » Blog Archive » 5 tips for better CSS organization[...]…

  • 12 Debian ~ Nov 13, 2011 at 2:06 am

    Debian…

    [...]CSS Orgy » Blog Archive » 5 tips for better CSS organization[...]…

Leave a Comment