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.
hey,
good info, really useful.
thanks
I couldn’t understand some parts of this article nnial 2007 – salvatore iaconesi – del.icio.us poetry, but I guess I just need to check some more resources regarding this, because it sounds interesting.
Loading multiple style sheets slows down your site. We reduced from 5 to 1 at a large financial site and saw a 10% + decrease in page load time.
I like most of your suggestions, but I have to take issue with numbers 3 and 4.
On number 3, maybe there is some sort of mental block on my end, but I can’t imaging how having all of the properties on one line is easier to understand or edit.
It’s much harder to find a single semicolon in a line of text to find the delimiter than it is to just see all of the rules on their own line.
If it is in fact easier, why do all of your examples use the traditional way of placing each rule onto it’s own line?
On number 4, I am mixed on this one. Using import statements, and chunking your CSS into different files is much easier from a development standpoint, especially if you’re coding on a project where multiple people may use it.
However, performance wise, it’s a really bad idea, because it requires a separate HTTP request for every file, and the HTTP spec says that only 2 requests can happen simultaneously.
The optimal solution would be to have a server side script that either builds all of the files into a minified one file for production, or a script that compiles them at runtime and caches the result.
Otherwise, though, great article :)
I would have to agree with Nate on #3. To me personally it is much easier to have my styles broken up with a break in between. The great tip you had in #8 would really be pointless if it weren’t broken up.
I think you should apply the same principle that I discussed (breaking lines between styles) with the color glossary.
I really liked #1 and #8, great points. Overall good article, straight to the point. Nice site by the way too, very easy to nav. Take care!
Nate, Matt: I moved to this method after my CSS files started getting ridiculously long. For me, the most significant advantage is the ability to immediately see the relationships between parent and child elements , it just seems easier on the eyes, especially as you have more attributes and such.
All of this can change when it goes to a live server – it can be optimized and compressed for that, but for development I like to be able to see it all.
It all comes down to a matter of preference and what works for you…
An example of using Indented Single Line for parent and child elements is like this:
Nate: If you create a frameworks of multiple CSS files and concatenate them in one long file, server-side, you are saving the HTTP requests and lightening the load on your server.
I agree with you that this method on large, high-traffic sites, adding five more HTTP connections to every page view may result in angry system administrators. Kevin Cornell suggests two possible solutions to this are:
1. Include everything in a single file, rather than breaking it into modules. The problem here is that you lose the ability to include only certain parts of the framework, and you also make maintenance more difficult.
2. Have a server-side process that dynamically flattens the individual files into a single response. I’ve not seen this done, but it could be very efficient if done well. This way, the individual components are still available, but the entire framework is available in a flattened version, as well.
good
I don’t agree with #4. Dividing your CSS in separate files increases the web page load time. The page load faster if it has reference to less external files (JS, CSS).Also keeping the file size small reduces the data being transferred.