I've seen a few posts about how to write css for readability (e.g. http://coding.smashingmagazine.com/2008/05/02/improving-code-readability-with-css-styleguides/, http://woork.blogspot.com/2008/03/write-well-structured-css-file-without.html, http://woorkup.com/2009/10/18/5-rules-to-write-more-readable-css-files/) and a lot of them give good common-sense tips, but I'd like to challenge a couple things.
I like to order my CSS selectors based on the order they appear in the HTML. So if I've got this HTML:
<div id="container">
<div id="logo">...</div>
<nav id="main-nav">...</nav>
</div>
My css might look like:
#container {
position: relative;
top: -10px;
max-width: 1024px;
width: 90%;
margin: 0 auto;
color: #000;
.box-shadow(0px 0px 4px 1px rgba(127, 127, 127, 0.6));
}
#logo {
float: left;
padding: 0.5em 1em;
font-family: 'LogoFont';
font-size: 24px;
color: #c4bb00;
}
#main-nav {
/* etc */
}
In cases where the content is dynamic and the elements are "reliably" in order, then order based on expected user experience. I do this because debugging that content often requires that order anyway.
Most importantly
I prefer to order my css attributes based on this order:
- positioning
- dimensions (width, height)
- spacing
- colors
- effects
I do this because when I'm tweaking or debugging, I'm often not looking in alphabetical order like the blog posts above mention. I'm looking for layout fixes. Or color fixes. Ordering things like this to me makes logical sense as well as makes editing what I'm looking for easy to spot. See the #container css above.
This is just my way. Thought I'd share.




