A clean way to code CSS

16.05.2008

I don't know if it is the sites i am visiting but every time I want to take a look at the css files I wonder why nobody writes its CSS files in a nice and clean way. Most declarations are just listed in an endless file nobody can understand(not even the developer who wrote it.... noo you can't....stop thinking you can!).

I'm not saying that I invented the way I am proposing now. pixel2life.com uses this kind of formating on its own site and I immediately fell in love with it :-)

Indentation!!

every developer does it, it's done in every language and it helped make things clearer and more readable since.... well.. i dont' know since when... but it was a long time ago..So why does nobody do it with CSS? Just because every declaration is closed for itself it doesn't mean you can't indent them.

So here is how it goes:

1. Take a look at your html code

Let's say your html looks like this

  1. <div id="site">
  2. <div id="header">The header image</div>
  3. <div id="body">
  4. <div id="content">My super content</div>
  5. <div id="sidebar">My widgets</div>
  6. </div>
  7. <div id="footer">Footer information</div>
  8. </div>

 

2. Indent exactly the same way!

  1. div#site {
  2. width:800px;
  3. overfloat:hidden;
  4. margin-left:auto;
  5. margin-right:auto;
  6. }
  7. div#header {
  8. overflow:hidden;
  9. }
  10. div#body {
  11. border:1px solid #333333;
  12. overflow:hidden;
  13. }
  14. div#content {
  15. padding-top:20px;
  16. padding-left:20px;
  17. width:560px;
  18. margin-right:200px;
  19. }
  20. div#sidebar {
  21. float:right;
  22. width:190px;
  23. border-left:1px solid #990000;
  24. }
  25. div#footer {
  26. height:37px;
  27. width:814px;
  28. margin-left:auto;
  29. margin-right:auto;
  30. overflow:hidden;
  31. }

This is so easy, every html/css coder can do this. I hope you can see the advantages...

No? Well:

  • You will find your declarations multiple times faster
  • Others will start to understand your css file

I can't think of more reasons besides that I started to code much better CSS files after starting to code CSS like this.

I think there are many other ways to enhance readability but another thing you can do is:

Divide CSS Sections

What you can do as well is divide the css sections in your css file. I usually divide the sections in three parts:

1. Tags first

On top of all write down all mayor tag-selectors like headings, paragraphs, links, tables etc. This way you have a first main styling for your site. Example:

  1. /**
  2.  * Tags
  3.  */
  4. p {
  5. margin-top:0px;
  6. line-height:14px;
  7. }
  8. h1, h2, h3, h4, h5, h6 {margin-bottom:2px;margin-top:2px;}
  9. h1, h2, h3 {
  10. color:#9FD4FF;
  11. font-size:20px;
  12. }
  13. h4, h5, h6 {
  14. color:#85c440;
  15. font-size:12px;
  16. margin-top:0px;
  17. }
  18. a, a:visited, a:link {
  19. color:#85c440;
  20. text-decoration:none;
  21. }
  22. a:hover, a:active {
  23. text-decoration:line-through;
  24. }

2. Go on with Class-selectors

after that you can start listing css class selectors:

  1. /**
  2.  * Classes
  3.  */
  4. .code {
  5. display:block;
  6. border:solid 1px #333333;
  7. background-color:#FEFEFE;
  8. font-family:Courier New, Courier, monospace;
  9. color:#333333;
  10. padding:5px;
  11. }
  12. .highlight {
  13. font-weight:bold;
  14. color:#FF0000;
  15. }

3. Finally id-selectors

In the end, all your id-selectors can be listed

  1. /**
  2.  * Global Layout
  3.  */
  4. div#site {
  5. width:800px;
  6. overfloat:hidden;
  7. margin-left:auto;
  8. margin-right:auto;
  9. }
  10. div#header {
  11. overflow:hidden;
  12. }

I hope somebody struggling with his or her CSS files can find this useful :-)

Cheers & till next time, Guille

2 responses to "A clean way to code CSS"

22.05.2008 | Chuong Says:
bam. Good Tips. Thx for it.
03.08.2009 | Boris Says:
Hey Guille or should i say billy ;)
Thanks for this inspiration its a good way to arrange css-files more comfortable ... and all the best for negdis

Post a response: