10 Principles of the CSS Masters

When it comes to CSS, there are lots of resources and supposed “expert tips” on the web. These are from unproven, self-proclaimed “gurus” who have no street cred in the design world. While they may have valid points, how is one to know whether a CSS tip is a valid resource or just an untested hack?

Instead of relying on unknown sources for advice, let’s look deeply into designers who have excellent design backgrounds and have walked the walk. These CSS tips are gathered from some of the most respected designers on the planet. They have the portfolios to back their advice up, so you’ll know that each tidbit of advice is of the highest quality.

Below are 10 excellent principles that any web developer or designer can find useful, meaningful, or challenging. Consider this sage advice from journeymen (and women) who have walked the long, hard road of design excellence. These are the true masters of CSS. Drink deep from their knowledge and take their wisdom on your next designing adventure.


1. Keep CSS simple – Peter-Paul Koch

What bothers me most about the mindset of CSS hackers is that they are actively searching for complicated solutions. Seek and ye shall be found, if you want complexity it’ll take you by the throat. It’ll never let go of you, and it won’t help you, either.

Peter-Paul Koch is a godfather of web development. While he’s an old-school developer and the bulk of his web portfolio was between 1998-2002, he’s worked with the likes of Apple and other heavyweights. He’s written a book on javascript, but don’t think for a second he doesn’t have anything to say about CSS.

The danger of CSS hacks

Koch has addressed something that every designer and web developer should follow with zeal: Keep your CSS simple. Simplicity is a hard thing to achieve, especially in CSS design. There are a myriad of CSS hacks that one can find for making all browsers look the same, regardless of version or type. Yet there’s a fundamental flaw with using many CSS hacks. As web browsers evolve, it’s much harder to keep up with the changes.

Koch makes an interesting point about developing for the web. The Internet as as whole is a very unpredictable place, and trying to second-guess the way it will work in the future is a very bad strategy.

The Web is an uncertain place. You’ll never be sure that your Web sites will work in the exact way you want them to work, not even when you apply all modern insights from CSS, accessibility and usability. Instead of seeking false comfort in hacks that seem all the more comfortable because of their complexity, you should accept uncertainty as a basic principle.

Browsers don’t have perfect CSS support; especially for people who’ve just started learning CSS, that can be infuriating. Nonetheless CSS hacks are not the solution. Acceptance of the way the Web currently works is the best way to go because it’ll keep your sites simple.

Peter-Paul has hit on something that rings true for not only CSS, but for web development as a whole. Simplicity is key for efficiency in coding.


2. Keep CSS declarations in one line – Jonathan Snook

Jonathan Snook is an incredibly popular designer from Ottawa, Canada who’s made his name in web standards and design. He’s spoken at prestigious conferences like SXSW and has published quite a few technical resources on design through Sitepoint.

One of Jonathan’s tenants to coding CSS is to keep declarations in one line.

The second one may look prettier but it sure doesn’t help me find anything. When looking for something in a style sheet, the most important thing is the ruleset (that’s the part before the { and } ). I’m looking for an element, an id or a class. Having everything on one line makes scanning the document much quicker as you simply see more on a page. Once I’ve found the ruleset I was looking for, find the property I want is usually straightforward enough as there are rarely that many.

Jonathan goes on to give an example for single line declarations that looks like this:

Good

{font-size:18px; border:1px solid blue; color:#000; background-color:#FFF;}

Bad

h2 {
font-size:18px;
border:1px solid blue;
color:#000;
background-color:#FFF;
}

Not only does this approach help with quickly scanning your CSS, it also helps in keeping your CSS file smaller by removing unneeded spaces and characters.


3. Use CSS shorthand – Roger Johansson

Most people know about and use some shorthand, but many don’t make full use of these space saving properties.

Roger Johansson knows a thing or two about designing for the web. The Swedish web designer has been working on the Internet since 1994, and has a popular web design blog. When it comes to simple and elegant solutions, Roger is one of the most knowledgeable in his field.

Johansson has a very in-depth article on the importance of CSS shorthand, and gives quite a few examples of how to use it while coding CSS. Here’s an example:

Using shorthand for these properties can save a lot of space. For example, to specify different margins for all sides of a box, you could use this:

margin-top:1em;
margin-right:0;
margin-bottom:2em;
margin-left:0.5em;

But this is much more efficient:

margin:1em 0 2em 0.5em;

The same syntax is used for the padding property.

While CSS shorthand reduces the size of the stylesheet, it also helps organize and keep the code simple. Beautiful CSS is simple CSS.


4. Allow block elements to fill space naturally – Jonathan Snook

Mr. Snook has another piece of crucial advice that every web developer should live by: allow block elements to fill space organically. If there’s one recurring theme in CSS development, it’s to not force the code to do things it isn’t meant for. This means avoiding CSS hacks and finding the simplest solution possible.

My rule of thumb is, if I set a width, I don’t set margin or padding. Likewise, if I’m setting a margin or padding, I don’t set a width. Dealing with the box model can be such a pain, especially if you’re dealing with percentages. Therefore, I set the width on the containers and then set margin and padding on the elements within them. Everything usually turns out swimmingly.

Jonathan’s rule of thumb is great for ensuring that your layouts won’t break and that the simplest approach is used when creating layouts with block elements.


5. Set a float to clear a float – Trevor Davis

Floating is probably one of the most important things to understand with CSS, but knowing how to clear floats is necessary too.

Trevor Davis may not be as big of a name as Zeldman or Snook in the design world, he surely deserves some mention just based on his excellent portfolio of web layouts. His blog is an excellent resource for any web developer wanting to brush up on his design chops.

Clearing floats

In Trevor’s flagship article The 6 Most Important CSS Techniques You Need To Know, he’s added a nugget that can save many headaches when using columns in your layouts.

I have created a simple page with two floating columns next to each other. You will notice in the example that the grey background does not contain the floating columns. So, the easiest thing to do is to set the containing element to float. But now, you will see that the container background doesn’t contain the content area.

Since the container has margin: 0 auto, we do not want to float it because it will move it to whichever side we float it. So another way to clear the float, is to insert a clearing element. In this case, I just use an empty div set to clear: both. Now, there are other ways to clear a float without markup, but I have noticed some inconsistencies with that technique, so I just sacrifice an empty div.


6. Use negative margins – Dan Cederholm

Sometimes it’s easier to deal with the exception to the rule, rather than add declarations for all other elements around it.

Dan Cederholm’s company SimpleBits is a powerhouse of a design company. Dan’s worked with the likes of:

  • Google
  • Blogger
  • MTV
  • Fast Company
  • Inc.com

… and many other high-profile web companies. Fortunately, Dan passes on some of the knowledge he’s learned working with these massive names on his blog at SimpleBits. Here’s a rule of thumb for you web designers and developers: If Dan Cederholm says anything, you listen. Think of him as a digital sherpa, guiding you to the crest of your design mountain.

Negative margins

While it may seem counterintuitive to put a negative in front of any declaration (like margin-left: -5px), it’s actually quite a good idea. Mr. Cedarholm explains that using negative margins on elements are sometimes easier than having to change every other aspect of the design to make it align they way you want.

There are situations when using negative margins on an element can be the easiest way to “nudge” it out from the rest, treating the exception to the rule in order to simplifiy code.

You can see his example of proper negative margin usage here.


7. Use CSS to center layouts – Dan Cederholm

“How do I center a fixed-width layout using CSS?” For those that know, it’s simple. For those that don’t, finding the two necessary rules to complete the job can be frustrating.

It’s no surprise that Dan is going to make this list twice. Centered layouts are on the surface a very simple idea, but for some reason they don’t always work as easily as advertised. Centering layouts with CSS can be a frustrating endeavor for a beginner if they’ve never tried it before.

Dan’s got a tried-and-true method that he uses frequently to achieve centered-layout nirvana.

#container {
margin: 0 auto;
width: xxxpx;
text-align: left;
}

Many modern designs rely on centered layouts, so using this method will at some point come in handy for web developers and designers.


8. Use the right DOCTYPE – Jeffrey Zeldman

You’ve written valid XHTML and CSS. You’ve used the W3C standard Document Object Model (DOM) to manipulate dynamic page elements. Yet, in browsers designed to support these very standards, your site is failing. A faulty DOCTYPE is likely to blame.

Jeffrey Zeldman is one of the co-founders of the excellent resource site A List Apart, co-founded and ran The Web Standards Project, runs the Happy Cog design studio, and even wrote the book on designing for web standards. In short, Zeldman is in the upper-echelon of web designers.

DOCTYPE misunderstanding

The DOCTYPE of a web page is one of the most overlooked aspects of design. Using the right DOCTYPE is crucial, and Zeldman explains why.

Using an incomplete or outdated DOCTYPE—or no DOCTYPE at all—throws these same browsers into “Quirks” mode, where the browser assumes you’ve written old-fashioned, invalid markup and code per the depressing industry norms of the late 1990s.

Zeldman stresses the importance of a) actually using a DOCTYPE, and points out that you have to add an url in the declaration like so:

If you’re finding unexplained problems with your layouts, odds are the DOCTYPE could be the problem.


9. Center Items with CSS – Wolfgang Bartelme

Centering items is a frequent task when designing websites. But for people that are new to CSS it’s mostly kind of enigma how to center for example a whole website in browsers other than IE.

Wolfgang Bartelme is a web designer with Bartelme design, a web design firm. Bartelme has one of the most elegantly-designed blogs, and continually creates excellent icon and design work. He’s done design work for the blogging platform Squarespace, as well as the popular software event MacHeist.

Wolfgang has created a tutorial that helps with the complicated task of centering elements with CSS. Centered elements are insanely useful, but are sometimes hard to achieve given the design. Bartelme’s tutorial ensures centered alignment by choosing the right DOCTYPE and making adding his CSS voodoo. The code nothing fancy and gets the job done, and falls directly in line with striving for simplicity in CSS.


10. Utilize text-transform commands – Trenton Moss

Trenton Moss knows web usability. He has his own web usability company that trains people in usability training and web writing. He also writes for sites like Sitepoint. Trenton gives excellent tips based on his experience as a web usability expert.

It’s a simple fact that designs change over time, especially in the way text is displayed on websites. The best thing a web designer can do is plan for the future to make sure that instead of having to manually change the way text is displayed, it’s best to use CSS to change the appearance of the text. Trenton Moss shows us how to achieve this through the use of a simple, underused CSS command called text-transfom.

One of the lesser known, but really useful CSS commands is the text-transform command. Three of the more common values for this rule are: text-transform: uppercase, text-transform: lowercase and text-transform: capitalize. The first rule turns all characters into capital letters, the second turns them all into small letters, and the third makes the first letter of each word a capital letter.

By using CSS to display the appearance of text on the site, it allows for change in the future and keeps things consistent over time.

This command is incredibly useful to help ensure consistency in style across an entire Website, particularly if it has a number of content editors. Say for example your style guide dictates that words in headings must always begin with capital letters. To ensure that this is always the case, use text-transform: capitalize. Even if site editors forget about the capitalisation, their mistake won’t show up on the Website.

While text-transform is a small thing to add to add to a css layout, it can make a world of difference in the future when changes need to be made.

  • Subscribe to the NETTUTS RSS Feed for more daily web development tutorials and articles.

Glen Stansberry is a web developer and blogger who’s struggled more times than he’d wish to admit with CSS. You can read more tips on web development at his blog Web Jackalope.


Tags: advice
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.detacheddesigns.com Jeffrey Way

    In reference to the “clear a floated element” tip by Trevor Davis – I would disagree. Setting overflow:hidden on the container element takes care of it. I’ve never had any problems. Or you can use the extended “containerElement:after” method to clear it.

    • sim

      Setting overflow:hidden on the container element does not work consistently. I prefer to use overflow:hidden/auto as my first solution, when it does not behave the way I wanted, an extra div with clear always do the job.

  • http://www.brugbart.com BlueBoden

    These Techniques are well known among most web designers, working with css based layouts.

    The problem with float is that it is tricky to create multi-column layouts, this is where CSS position is much easier to work with, and of cause it shouldn’t require as much div-it-is as the float based layouts.

  • http://another-perfect-world.org/ Vasili

    I love numbers 4, 6 and 7 the most. Number four and I go back a long time. :)

  • http://designshard.om Max | Design Shard

    Some good points , i guess every one has there own ways of dealing with css, som better than others.

  • http://www.detacheddesigns.com Jeffrey Way

    Here is a brief snippet from a SitePoint article that details the method I’m speaking of (overflow: auto, overflow:hidden) to clear a child element that is floated.

    “…until last week when SitePoint Forum’s own CSS Guru, Paul O’Brien, nonchalantly pointed out that adding a ‘overflow:auto’ to the outer DIV did the trick. [clearing the floated element]

    ‘Rubbish’ I thought to myself.

    Half an hour of testing later, I was amazed to find Paul was 100% correct – as this example shows. It seems that reminding the outer DIV that it’s overflow is set to ‘auto’, forces it to think “oh yeah.. I’m wrapping that thing, aren’t I?”.

    This fact is so boringly simple that it’s hard to know if thousands of developers are well aware of it, but never thought to mention it. I know I showed the example page to four CSS-savvy SitePoint colleagues and all shock their heads, blinked slowly and said “Whaa…?” (or something similar).”

  • Jay

    Thank you Jeff.

  • http://www.puredezigner.com PureDezigner

    Very help post, also I’m digging the artwork you used to convey the message in each of the principles. Time to update my code structure on my website.

  • Dan

    I’ve been setting up my CSS like #2 says for years, but never thought anyone else did. I’ve always wondered why people do it differently- this is a breath of fresh air.

  • oisean

    Underwhelming stuff.

  • Simon

    Jeff, that is unreal. Never knew it existed (it’s sooo simple), but goddamn I’m glad I’ll never have to type (or something similar) ever again.

  • http://www.insicdesigns.info insic

    great tips. i like the article

    • http://garntech.com Ravi Kumar

      Yeah I am agree with u. Hey can you send me your email id. I want to talk with you.

  • Simon

    Hmm can’t edit my comment, but oisean, I think there is a point where css/html tutes/articles become rather mundane. You’re not really going to find any new/exciting techniques other than the odd little pearl of wisdom (like Jeffs comment).

  • oisean

    Simon, sure. I suppose this post suffers in comparison with yesterday’s PHP tut.

  • begs

    “Keep CSS declarations in one line – Jonathan Snook”
    I don’t agree here. It’s awful to edit one lined CSS declarations.
    Readability and of course editability for me as develpoper is much more of weight than the size of the css file.

    Tab and multiline your CSS declarations. Everyone can then edit the file easyly, and also find the right declarations fast. (And if not, they can use STRG+F / APPLE+F)

    And the fact, that a CSS File is much smaller, if there were no whitspaces in it, is true, but if you use images on your page with 100Kb (like in this article) no one cares about 1-2 Kb smaller CSS Files.

    • http://www.mode-nine.com James Boyce

      I completely agree.

      The difference in size is trivial at best, plus any individual CSS file SHOULD be layed out and labelled to a degree that you don’t need to sift through it to find what your looking for, not to mention even the smallest sites should utilise a number of basic CSS files eg: typogrtaphy, forms, layout etc…

      In my opinion single line declarations are a big nono.

      • Marshall Salinger

        I completely agree. This goes against typographic rules in general. CSS is here to help design documents so they are readable. One line css code is not easy to scan. Would anyone want to read a php class with functions that are reduced to one line? I didn’t think so…

    • http://joelpittet.com Joel Pittet

      I completely agree, too.

      In my experience, line breaks on properties reduces accidental duplication. People tend to miss that they already put that border-left in.

      If you really want to save that line breaks removed, parse out all the line breaks with a serverside script and cache it. There are tones of solutions out there for this approach.

  • Simon

    oisean, indeed ;)

  • oisean

    Agree with begs.

  • Blaž

    Nice article for beginer like me :). Time to change some code on a page that I am working on :). Oh and thanks Jeff, your method is to simple to be true.

  • http://vailo.wordpress.com Niklas

    Once again you guys open my eyes to new blogs and new websites! The article talks about interesting ideas. Even though I don’t agree with all entries new ideas and thoughts is always nice to read about. Thanks for another great, inspiring, article!

  • http://www.freshclickmedia.com Shane

    Interesting list – thanks for posting.

  • http://enhance.qd-creative.co.uk James

    @BlueBoden – Creating a multi column layout does not have to be hard with CSS. Have a look at some of the examples here: http://www.pmob.co.uk/

    I liked and agreed with most of the points made with the tutorial. I have to agree with Jeffrey’s comment on the whole clearing floats issue.

    I can see where Mr Snook is coming from on his, “keep declerations on the same line” rule but seriously, the only benefit I see in doing this is saving a bit of space. I cannot quite understand how the CSS file is more maintable if you do this. Surely a CSS file will be easier to manage with the traditional CSS decleration pattern:

    element.selector {
    property: value;
    property: value;
    property: value;
    }

    I can see how scanning a document might be faster with Snook’s approach but an entire ruleset can get quite lengthy and ends up being ugly in the text editor if it wraps (and harder to look at and add to or take away from) – and if it doesn’t wrap – like in Aptana :( – then you’re left with horiztonal scrolling which I hate!

    I like what Snook says about letting block elements fill space naturally… Too many people feel that they have to define a width for everything!

    Anyway nice list, thanks Glen! :)

  • http://www.ben-griffiths.com Ben Griffiths

    This was a great post – although I agree about the one line method, I hate that style ;)

  • http://www.borfast.com Raul Pedro Santos

    I also have to agree with begs.

    Having a CSS rule in just one line is confusing as hell. It may seem simpler but studies have proven a long time ago that, when looking for something, it’s easier to scan vertically along the first letters of the text, than it is to scan horizontally. And it’s quite simple to understand why, too. If you scan vertically, you just need to read the first couple of letters to know right away if that’s what you’re looking for, while when scanning horizontally you have to go through all the text because you can’t see as easily where a declaration ends and the next one starts. Scanning vertically allows you to look *immediately* at the beginning of each declaration.

    If file size is crucial, then you can always use a compressor on it, to remove extra white space and unnecessary newlines. But only when deploying the site; I would never work on a CSS file like the article says to.

  • Jayphen

    Nice article, though I don’t agree with #2 or #5.

    #2
    This simply looks a mess, in my opinion. Properly commented, sectioned & tabbed CSS beats having everything on one line any day, especially if you have more than one person editing a CSS file. If your problem is trying find a selector, a simple ctrl-F search will find it instantly (assuming you know the name of the ID or class, but why wouldn’t you?). If you have a lot of properties and values on the same line, it is difficult to find exactly what you’re looking for among them, and you can’t as easily ctrl-F and search for what you’re looking for, as it will undoubtedly appear many times throughout the entire CSS document.

    #5
    As mentioned before, overflow:hidden on the containing element is a much more elegant solution to this problem.

  • http://www.darrenmcpherson.co.uk Gafroninja

    I have to say, I partially disagree with Jonathan Snook on point #2.

    I think its wrong to say not using single line declarations is “bad”. I believe its more preference since there is almost no gain in file size. Some people find it easier if the declarations are over multiple lines some find it easier if it’s over single lines.

    I use both. Any element that has less than 3 declarations, i use single line. If any more I use multiple lines.

    Other than that, these are all good.

    Just my view not a hardcore fact or standard.

  • http://www.insicdesigns.info insic

    Keep CSS declarations in one line? i dont think so. for me, it makes your stylesheet horrible to read. im not againts with the masters, its my own idea only. hehehe

  • http://www.ryanroberts.co.uk Ryan

    > “Keep CSS declarations in one line – Jonathan Snook”

    What nonsense, this is nothing more than personal opinion so shouldn’t be listed as some sort of definitive rule. Single line declarations can become very hard to read, indenting and setting decelerations on individual lines is good for the same reason it’s good to correctly format PHP, JS, Python, HTML etc… but as I say it’s personal choice and the one line approach is certainly not *the* best.

  • http://www.ryanroberts.co.uk Ryan

    Also I’ve not used empty clearing divs for years, there’s no need to add useless markup since the solution can be accomplished for more efficiently through CSS. The CSS approach is also far more flexible if you decide to change the layout.

  • oisean

    So we all agree that single-line declarations are a good idea, then?

  • http://iainedminster.com Iain Edminster

    I’m glad to see other people disagreeing with “Keeping CSS declarations in one line”. This “rule” is more like personal choice. If you are working on a website with other people, doing things like this would be counter-productive. I’ve found the best way to write CSS is to separate key declarations into separate files contained in a single folder and then gather them all, compress it and cache it. Same thing can be said with Javascript files.

  • FlyboyArt

    While I admit it’s harder to edit CSS if it’s on one line, it truly saves some space and time in downloading. If you ignore all these 1k here, 1k there things then you’re website will become bloated. To be truly efficient, run your CSS file through a compressor (like we do for JS files) before you send it along to your website. Edit it in any fashion you want (one line or many) but at least upload it in the most efficient format for downloaded by browsers. They don’t care how it looks, just that it’s formatted properly.

    My two cents… ;=}

  • http://anthonyshort.com.au Anthony Short

    I should point out that with #4, you really shouldn’t have box model issues unless you’re accounting for IE5.5. IE6 renders the box model fine if its in strict mode. Add widths, margins and padding to whatever you want, you won’t break it in IE6+.

  • http://shubox.net shubox

    #2 Snook’s idea of keeping CSS in one line? I’ve heard this before and disagreed with it then, and disagree with it now. I have never noticed any file size difference or performance difference when using inline vs multi-line method. And in terms of ease of editing, the multi-line method is startling easier to look at and identify each part. Inline might work for Snook, that’s fine – but is certainly NOT bad practice to use multi-line layout for css.

  • http://www.danharper.me Dan Harper

    With most of the other comments, I completely disagree with keeping declarations on one line. Although, it’s really just personal preference and everyone develops their own way of laying out a document.

  • http://www.solutiondesigns.net Philo

    Nice Article!

  • Joris

    I agree on most of the things although I’d never use negative margins for a standard, they don’t always come out correctly and I only use them as a ast resort.

    As for the centered content I used to love it but after reading some usability studies etc I changed my mind, same goes for centered navigation, like underneath the header for example.

  • Ben W

    No, no no. I really can’t handle CSS declarations on a single line – someone I work with does that and I find it takes me MUCH longer to edit their CSS files. Also their CSS is always a complete mess with many duplicated ids/classes/attributes.

    I find it best to indent like this:

    #someid {
    background: #FFF;
    border: 1px soldi #CCC;
    font-size: 1em;
    }

    Then it’s very easy to scan down the left side and find what you need. Attributes should be in alphabetical order too.

    Other than that – good advice – nothing new though (to me).

    BTW in #8 you say “Zeldman stresses the importance of a) actually using a DOCTYPE, and points out that you have to add an url in the declaration like so:”

    Like what?

    Looks like you omitted the example.

  • Ben W

    Damn – looks like my indenting got “parsed out”. Bah!

    OK – in my CSS declaration, each attribute and the closing brace should be indented to line up with the opening brace (plus an extra space or two).

  • http://www.jonathansace.com Web Designer

    Thanks for this!

  • http://danielfelton.com Dan

    I prefer indenting than putting everything one one, RE one line css.
    I don’t like having to scroll to the right to read my rules.
    If you indent properly you get the same effect of being able to scan read the selectors.
    And of course, you can always add comments to remind you what everything is.

  • Alper

    Thanks for Article!!There are some good points that every css master’s should take care

  • http://www.1pixelbrush.com Dan

    Good comments going on here

  • http://themeshaper.com/ Ian Stewart

    For what it’s worth, I didn’t know about overflow:hidden for clearing floats until I read Transcending CSS.

    Also, one line CSS = crazy talk. :)

  • http://www.detacheddesigns.com Jeffrey Way

    I suppose I should weigh in on the CSS one line discussion. I, personally, prefer the vertical method. However, it is true that your file sizes will be larger this way. To compensate, I always run my stylesheet through a compressor just before deploying a project.

  • Wayne

    Single line declarations? Bah. Keep it readable, dude.

  • http://areyoudesign.com S.K

    One fun alternative to float layouts is the inline-block property. A couple of advantages I can think of right now include being able to use the vertical-align property and no float bugs. Just remember to unclude display:inline in your IE stylesheet.

  • http://davidwparker.com David Parker

    Like many other said, #2 is bunk. Keep your declarations on multiple lines and tab them. Also, if you’re worried about space, then run your CSS file through a script to remove all whitespace before you deploy. Best of both worlds.

  • http://ndezigns.com Nate

    Good article.

  • http://www.kevinquillen.com Kevin Quillen

    I think #2 is completely and utterly wrong. Single line formatting only makes things that much HARDER to find the rules you are looking for. Tab indentation makes reading more easier. What if your PHP/CF/ASP applications were all on a single line?

    In a business environment, that rule must be expanded upon to a level of consistency wherein coworkers follow the same style of formatting… example, reference with absolutes like div#container, instead of just #container. With that, the CSS sheet should follow the markup of the document, i.e. footer not being delcared at the top, header in the middle, etc.

    This rule really only works within your own work. When a CSS sheet grows to 1000 lines, its very difficult to filter visually for what you are trying to find.

  • http://www.twitter.com/aaroni268 Aaron Irizarry

    Thanks for the article… good brush up, and good to compare views on techniques.

    thanks!!!

  • http://www.satedproductions.com Michael Thompson

    Real CSS masters have a text editor (eg., Vim) capable of folds; making the declarations appear on one line until unfolded to reveal multi-line style definitions.

    I’d like to up the ante on this debate by quoting Roger Johansson from his blog post, CSS Tips and Tricks, Part 2:

    “When I combine selectors, I normally put them on separate lines. I find it easier to find things in the CSS file that way. Next, I leave a space between the end of the last selector and the opening brace. Each declaration goes on a separate line, with no indentation and no spaces around the colons separating properties from values. … Finally, the closing brace goes on a line of its own.”

    The style that CSS is written in is purely personal, and a rule imposing one person’s preference should never have been included in this article. Otherwise, good article.