9 CSS Ethics Every Designer Should Have
No need to get any more complicated structure than you need to. Writing a CSS Stylesheet That is Easy to Maintain is really easy, just by following these 9 rules.
How deeply you organize your CSS can greatly hinder any necessary tweaks that arise in the future. So, I proposed the question to my team to take a close look at some of the most interesting CSS coding structure and listed them below where you can probably use in every project you are developing.
1) Indent descendant and related rules:
This allows you to easily recognize page structure within your CSS and how sections related to each other. [Erratic Wisdom]
#main {
width: 530px;
padding: 10px;
float: left;
}
#main #nav{
background: #fff;
width:100%
}
#main #left-col {
background: #efefef;
margin: 8px 0;
}
2)Grouping and commenting your CSS rules
Setup certain sections in your CSS files that always exists: page structure, links, header, footer, lists, etc. Those sections are always CSS commented to name each section appropriately.
/* Header Styles Go Here **************/
...CSS Code Goes Here…
/* End Header Styles *************/
Header
Structure
Navigation
Forms
Links
Headers
Content
Lists
Common Classes
And a sample separator that is most easily noticeable
/* -----------------------------------*/
/* >>>>>>>>>>>>> Menu<<<<<<<<<<<<<<<<-*/
/* -----------------------------------*/
3) Keep style type on single line
Combine properties onto a single line by using shorthand properties means that your CSS will be easier to understand and edit.
Instead of this:
h2{ color: #dfdfdf;
font-size: 80%;
margin: 5px;
padding: 10px;
}
Do this:
h5{color: #dfdfdf; font-size: 80%; margin: 5px; padding: 10px;}
4)Break your CSS into sheets
Separate your CSS stylesheets for different sections, use one stylesheet for layout, another for typography and another for colors .Mixing layout / typography properties will make you find that you are needlessly repeating yourself.
#main { @import "/css/layout.css";
@import "/css/typography.css";
@import "/css/design.css";
@import "/css/design-home.css";
@import "/css/extra.css";
5)Reset your elements
Many designers clear the styling of their sheets with a global reset which has an impact on some elements like form buttons and fieldsets that are completely destroyed with the global reset.Instead, you should pick-and-choose the elements you want to reset.
So instead of doing this
*{ margin: 0; padding: 0; }
Do This
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 {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-weight: inherit;
font-style: inherit;
font-size: 100%;
font-family: inherit;
vertical-align: baseline;
}
6)Place color scheme in one place for refrence.
Before you start your CSS file, comment your common colors and add it to the top of your style sheet.This will save you ton of time and will insure that your site has one color scheme.
/* Colors: Dark Brown #473B38 Light Blue #A8EFEE Pink FF4095 */
7)Use a meaning naming system.
Having a naming system for classes and id's saves you a lot of time when updating your document or debugging, you can use parent/child structure. The parent would be the container. So if our DIV is named “header”, and two divs nested called “menu” and “logo”. The naming structure in your css would be:
#header #header_menu #header_logo
8)Alphabetical Properties
It makes specific properties much easier to find.
body {
background:#fdfdfd;
color:#333; font-size:1em;
line-height:1.4;
margin:0;
padding:0; }
9)Keep a library of helpful CSS classes.
Useful for debugging, but should be avoided in the release version (separate markup and presentation). Since you can use multiple class names, make use of them debugging your markup.[Richard K. Miller]
.width100 { width: 100%; }
.width75 { width: 75%; }
.floatLeft { float: left; }
.alignLeft { text-align: left; }
.alignRight { text-align: right; }
Keep it Simple
No need to get any more complicated structure than you need to. Simplicity will save you time and efforts.
It would be great if you share with us your organizing tips to make this post a refrence to many of us. Don't forget to mention your site and name as it will be mentioned below your tip.
#6 was the only useful one.
Sorry, I can’t agree with “Break your CSS into sheets” since another rule in optimization recommands to have the least possible. And it’s right.
Secund, the “separator” comments make sheets ugly and almost unreadable sometimes.
Very nice list, I agree but I cannot really go along with #3…
I’m actually a backend web-programmer and not a css designer and in a current project I have a quite hard time with css files that use a single line instead one line per attribute:
Even though it may read better when editing, it does not work correctly in a standard version control diff and merge (CVS, Subversion, etc), as most of those tools work per line. Even with decent diff-editors that work per-character I’m having problems when comparing or merging 2 css files because the lines will not fit on the (half) screen and i have to scroll left and right. There is a reason that programmers have a well-established convention that a line of code should be not much more then 80 characters wide.
Also, not following #1, #2 or #4 because “it will waste bandwidth” is not a good reason for me… There are tools that can compress and merge css files.
I’m not sure I see the sense of arguments against chunking out your css into multiple using @imports. Sure, this impacts the load time of the first page served, but then they’ll be cached. We could time it, but I suspect that forcing every user to pull down all the styles used in every section of a large site is just as slow as having them do a few requests for small files (especially when they may not even visit some of those parts of the site).
Nice list, but like the others I dont agree with putting all your attributes on one line, when you could just combine all the font attributes into one declaration like:
font: 12px/14px “Helvetica Neue”, Arial, Helvetica, Geneva, sans-serif;
I would also recommend that you indent all your styles, it makes everything look so much more organized!
As for naming standards, I put together a great article on what to call things:
http://onerutter.com/css/css-naming-standards.html
I actually think css is overrated. There will come a time when separating everything arbitrarily will work against us.
I’ve been doing most of these for a while, even the alphabetical ordering of properties and indenting of selectors, but I don’t think I’ve ever seen these things all written down in one place.
Good work.
Hi Noupe, nice tips and nice site.
h5{color: #dfdfdf; font-size: 80%; margin: 5px; padding: 10px;}
LOLZ
While it is nothing new, I have created a simple but powerful CSS server-side framework in C# that allows me to specify one .cssx file that concatenates all of the referenced .css files – and does some magic along the way.
Its great because it allows me to have each set of styles in separate files for easy understanding without the extra HTTP overhead associated with multiple GETs.
It supports server-side variables which makes it simple to update colors, and other common attributes.
It also uses the exact same compression algorithm found in YUI’s CSS Compressor – so it does a nice job of saving valuable bytes, as well as cleaning out comments.
One of the properties of the .cssx file is cache control – it allows the developer to set different files to have different lifetime’s.
Combine these features with server-side gzipdeflate and you will save yourself alot of valuable bandwidth.