Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS 'schema' how-to

Tags:

css

styling

How does one go about establishing a CSS 'schema', or hierarchy, of general element styles, nested element styles, and classed element styles. For a rank novice like me, the amount of information in stylesheets I view is completely overwhelming. What process does one follow in creating a well factored stylesheet or sheets, compared to inline style attributes?

like image 770
ProfK Avatar asked Oct 08 '08 14:10

ProfK


4 Answers

I'm a big fan of naming my CSS classes by their contents or content types, for example a <ul> containing navigational "tabs" would have class="tabs". A header containing a date could be class="date" or an ordered list containing a top 10 list could have class="chart". Similarly, for IDs, one could give the page footer id="footer" or the logo of the website id="mainLogo". I find that it not only makes classes easy to remember but also encourages proper cascading of the CSS. Things like ol.chart {font-weight: bold; color: blue;} #footer ol.chart {color: green;} are quite readable and takes into account how CSS selectors gain weight by being more specific.

Proper indenting is also a great help. Your CSS is likely to grow quite a lot unless you want to refactor your HTML templates evertime you add a new section to your site or want to publish a new type of content. However hard you try you will inevitably have to add a few new rules (or exceptions) that you didn't anticipate in your original schema. Indeting will allow you to scan a large CSS file a lot quicker. My personal preference is to indent on how specific and/or nested the selector is, something like this:

    ul.tabs {
    list-style-type: none;
    }
        ul.tabs li {
        float: left;
        }
            ul.tabs li img {
            border: none;
            }

That way the "parent" is always furthest to the left and so the text gets broken up into blocks by parent containers. I also like to split the stylesheet into a few sections; first comes all the selectors for HTML elements. I consider these so generic that they should come first really. Here I put "body { font-size: 77%; }" and "a { color: #FFCC00; }" etc. After that I would put selectors for the main framework parts of the page, for instance "ul#mainMenu { float: left; }" and "div#footer { height: 4em; }". Then on to common object classes, "td.price { text-align: right; }", finally followed by extra little bits like ".clear { clear: both; }". Now that's just how I like to do it - I'm sure there are better ways but it works for me.

Finally, a couple of tips:

  1. Make best use of cascades and don't "overclass" stuff. If you give a <ul> class="textNav" then you can access its <li>s and their children without having to add any additional class assignments. ul.textNav li a:hover {}
  2. Don't be afraid to use multiple classes on a single object. This is perfectly valid and very useful. You then have control of the CSS for groups of objects from more than one axis. Also giving the object an ID adds yet a third axis. For example:

    <style>
    div.box {
    float: left;
    border: 1px solid blue;
    padding: 1em;
    }
    
    div.wide {
    width: 15em; 
    }
    
    div.narrow {
    width: 8em; 
    }
    
    div#oddOneOut {
    float: right;
    }
    </style>
    
    <div class="box wide">a wide box</div>
    <div class="box narrow">a narrow box</div>
    <div class="box wide" id="oddOneOut">an odd box</div>
    
  3. Giving a class to your document <body> tag (or ID since there should only ever be one...) enables some nifty overrides for individual pages, like hilighting the menu item for the page you're currently on or getting rid of that redundant second sign-in form on the sign-in page, all using CSS only. "body.signIn div#mainMenu form.signIn { display: none; }"

I hope you find at least some of my ramblings useful and wish you the best with your projects!

like image 68
Ola Tuvesson Avatar answered Nov 07 '22 10:11

Ola Tuvesson


There are a number of different things you can do to aid in the organisation of your CSS. For example:

  • Split your CSS up into multiple files. For example: have one file for layout, one for text, one for reset styles etc.
  • Comment your CSS code.
  • Why not add a table of contents?
  • Try using a CSS framework like 960.gs to get your started.

It's all down to personal taste really. But here are a few links that you might find useful:

  • http://www.smashingmagazine.com/2008/08/18/7-principles-of-clean-and-optimized-css-code/
  • http://www.smashingmagazine.com/2008/05/02/improving-code-readability-with-css-styleguides/
  • http://www.louddog.com/bloggity/2008/03/css-best-practices.php
  • http://natbat.net/2008/Sep/28/css-systems/
like image 36
Lee Theobald Avatar answered Nov 07 '22 09:11

Lee Theobald


Think of the CSS as creating a 'toolkit' that the HTML can refer to. The following rules will help:

  • Make class names unambiguous. In most cases this means prefixing them in a predicatable way. For example, rather than left, use something like header_links_object2_left.

  • Use id rather than class only if you know there will only ever be one of an object on a page. Again, make the id unambiguous.

  • Consider side effects. Rules like margin and padding, float and clear, and so on can all have unexpected consequences on other elements.

  • If your stylesheet is to be used my several HTML coders, consider writing them a small, clear guide to how to write HTML to match your scheme. Keep it simple, or you'll bore them.

And as always, test it in multiple browsers, on multiple operating systems, on lots of different pages, and under any other unusual conditions you can think of.

like image 22
Marcus Downing Avatar answered Nov 07 '22 09:11

Marcus Downing


Putting all of your CSS declarations in roughly the same order as they will land in the document hierarchy is generally a good thing. This makes it fairly easy for future readers to see what attributes will be inherited, since those classes will be higher up in the file.

Also, this is sort of orthogonal to your question, but if you are looking for a tool to help you read a CSS file and see how everything shakes out, I cannot recommend Firebug enough.

like image 2
Ryan Avatar answered Nov 07 '22 11:11

Ryan