Noupe Editorial Team November 10th, 2007

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]

  1. #main {
  2. width: 530px;
  3. padding: 10px;
  4. float: left;
  5. }
  6. #main #nav{
  7. background: #fff;
  8. width:100%
  9. }
  10. #main #left-col {
  11. background: #efefef;
  12. margin: 8px 0;
  13. }
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.

  1. /* Header Styles Go Here **************/
  2. ...CSS Code Goes Here…
  3. /* End Header Styles *************/
  1. Header
  2. Structure
  3. Navigation
  4. Forms
  5. Links
  6. Headers
  7. Content
  8. Lists
  9. Common Classes

And a sample separator that is most easily noticeable

  1. /* -----------------------------------*/
  2. /* >>>>>>>>>>>>> Menu<<<<<<<<<<<<<<<<-*/
  3. /* -----------------------------------*/
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:

  1. h2{ color: #dfdfdf;
  2. font-size: 80%;
  3. margin: 5px;
  4. padding: 10px;
  5. }

Do this:

  1. 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.

  1. #main { @import "/css/layout.css";
  2. @import "/css/typography.css";
  3. @import "/css/design.css";
  4. @import "/css/design-home.css";
  5. @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

  1. *{ margin: 0; padding: 0; }

Do This

  1. 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 {
  2. margin: 0;
  3. padding: 0;
  4. border: 0;
  5. outline: 0;
  6. font-weight: inherit;
  7. font-style: inherit;
  8. font-size: 100%;
  9. font-family: inherit;
  10. vertical-align: baseline;
  11. }
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.

  1. /* 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:

  1. #header #header_menu #header_logo
8)Alphabetical Properties

It makes specific properties much easier to find.

  1. body {
  2. background:#fdfdfd;
  3. color:#333; font-size:1em;
  4. line-height:1.4;
  5. margin:0;
  6. 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]

  1. .width100 { width: 100%; }
  2. .width75 { width: 75%; }
  3. .floatLeft { float: left; }
  4. .alignLeft { text-align: left; }
  5. .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.

Noupe Editorial Team

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

56 comments

  1. Hmm, I can’t go along with 3 and 4.

    3 I find harder to read, and page length I don’t find an issue with due to using firebug a lot and a half decent code editor with good syntax hightlighting and line numbers.

    4 I find you end up having to remember which file something is in. You get to the point of needing to make an amend to one area, but editing 3 files to do it. Or being lazy and sticking the amend in one file, and then struggling to find it later.

    Again, with tools like firebug and a good code editor, I don’t find long files a problem to deal with.

    8 I can’t be arsed with, and don’t see the point, and 9 is just wrong IMHO. Using class names with things like ‘left’ or ‘red’ is a bad idea.
    Using class names that relate to the content of the object, not how it is rendered. Don’t forget alternate devices, when on a mobile phone, ‘right’ might not have much meaning…

  2. Ofcourse the ethics are gud but it will bloat the style sheet and increase the loading time.

    When normal optimisation techniques are for a minimum number of stylesheets , the ethics show otherwise

  3. I do think it’s a choice of style but I switched to writing my styles on a single line about a year ago and can’t imagine going back. Cuts down on the length and file size of the style sheet too. I try to maintain two sets of style sheets, production and development, the production set is optimized and all lumped together in a single css.

  4. zParacha, Adrian and Kingstone
    When building a large site, I really like the move to separate module CSS into separate files – this makes maintainance and overriding much easier. You don’t have to create like 10 style sheets, 3 or 4 is fine as long as you separate the style in a good manner like putting defaults like fonts, anchor colors and behavior, headers, and any other properties that all pages will share in the default CSS file.
    The best way to group rules and selectors is by their relationship to each other and the rest of the page. For example, if you have a container, header and footer that drive your layout, group them together.

    I hope this makes sense to you and all can be optimized later in one file when placed on server to reduce the number of HTTP requests to lighten the load on your server.

  5. I agree with Noupe and Chris Olberding for number 3 — I, too, went to single line styles a while ago and also cannot imagine going back. My stylesheets are easy to scan, it’s easy for me to find the thing I’m looking for, and they are a LOT faster to write now that I’m not having to do all that formatting.

  6. 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.

    For WordPress users there’s a plugin called WP-CSS-Streamliner that does exactly that. When it works, it’s awesome, but some hosts seem to have porblems with it. My Mac testing server works flawlessly, but my web host spits out an error when it’s turned on.

  7. Nice set of tips but I have to go with many of the other commentators on points 3 and 4. Single line formatting of attributes seems to lead to less readability for me.

    I also found out the hard way that, although separating your sheets by function seems to be a good idea, in practice it just leads to a lot of going back and forth from sheet to sheet having to find and tweak the same tags or id’s over and over. It becomes pretty unwieldy after a while, particularly when you want to port the sheets over to another site and need to change properties in all of your sheets.

    I say this as someone who has been using the method for over a year now. I regret using it now.

Leave a Reply

Your email address will not be published. Required fields are marked *