Obsessive CSS Code Formatting: Indentation and Spacing

Published Sunday, June 1, 2008 @ 2:25 pm • 17 Responses

In the intriguing discussion following the first obsessive CSS formatting article, Jordan Gray brought up the age-old question regarding spacing: tabs or single spaces? I smugly responded that the issue has long-since been resolved, with tabbed spacing as the obvious winner. Let’s take a look at some serious CSS spacing examples..

1) Strictly Single Spacing

Here we have several code blocks showing consistent spacing via single blank space. Three key areas where single spacing is seen in this example: after the selector, and before each property and its corresponding value:

div#example_01 {
 padding: 1px;
 margin: 11px;
 width: 111px;
}
div#example_02 {
 padding: 2px;
 margin: 22px;
 width: 222px;
}
div#example_03 {
 padding: 3px;
 margin: 33px;
 width: 333px;
}

The resulting code is nice, clean, and readable, however, the scene quickly changes when various code blocks are indented:

div#example_01 {
 padding: 1px;
 margin: 11px;
 width: 111px;
}
div#example_02 {
 padding: 2px;
 margin: 22px;
 width: 222px;
}
 div#sub-div_01 {
  padding: 1px;
  margin: 11px;
  width: 111px;
 }
 div#sub-div_02 {
  padding: 2px;
  margin: 22px;
  width: 222px;
 }
 div#sub-div_03 {
  padding: 3px;
  margin: 33px;
  width: 333px;
 }
div#example_03 {
 padding: 3px;
 margin: 33px;
 width: 333px;
}

Yikes! As you can see, the strict single-spacing approach renders code-block indentation practically pointless. The indented code blocks really “blend in” with the surrounding code, making it difficult to quickly identify designated subordinate selectors. Perhaps two or more single spaces would work better?

2) Single Spacing, Tabbed Indents

This method takes advantage of both single spacing and tabbed spacing. Single spaces rest between selector and opening bracket, and also between properties and their values. Tabbed spaces occur before each declaration block, and also before the closing bracket:

div#example_01 {
	padding: 1px;
	margin: 11px;
	width: 111px;
	}
div#example_02 {
	padding: 2px;
	margin: 22px;
	width: 222px;
	}
	div#sub-div_01 {
		padding: 1px;
		margin: 11px;
		width: 111px;
		}
	div#sub-div_02 {
		padding: 2px;
		margin: 22px;
		width: 222px;
		}
	div#sub-div_03 {
		padding: 3px;
		margin: 33px;
		width: 333px;
		}
div#example_03 {
	padding: 3px;
	margin: 33px;
	width: 333px;
	}

To my (admittedly obsessive-compulsive) eye, this method produces the best results. Each block of code is clearly distinguished from the others, and subordinate code blocks are easy to identify. Nice.

3) Minimalist Spacing

Removing all spaces from within each code block is another commonly employed spacing technique. Placement of selectors and declaration blocks on individual lines fosters overall code-block readability while keeping file size to a minimum:

div#example_01{
padding:1px;
margin:11px;
width:111px;
}
div#example_02{
padding:2px;
margin:22px;
width:222px;
}
	div#sub-div_01{
	padding:1px;
	margin:11px;
	width:111px;
	}
	div#sub-div_02{
	padding:2px;
	margin:22px;
	width:222px;
	}
	div#sub-div_03{
	padding:3px;
	margin:33px;
	width:333px;
	}
div#example_03{
padding:3px;
margin:33px;
width:333px;
}

Within this strategy, tabbed indentation of subordinate code blocks greatly facilitates recognition, however, readability of individual declaration blocks (properties and their values) seems tedious at best. Yet, with fewer characters, perhaps this method is a good compromise between optimization and usability.

4) Other Hybrid Spacing Techniques

Here are a few other frequently observed spacing techniques:

a) Single-line code blocks with tabbed subordinates — here, spacing is similar to the single-spacing, tabbed-indents method, only with each code block placed on a single line:

div#example_01 { padding: 1px; margin: 11px; width: 111px; }
div#example_02 { padding: 2px; margin: 22px; width: 222px; }
	div#sub-div_01 { padding: 1px; margin: 11px; width: 111px; }
	div#sub-div_02 { padding: 2px; margin: 22px; width: 222px; }
	div#sub-div_03 { padding: 3px; margin: 33px; width: 333px; }
div#example_03 { padding: 3px; margin: 33px; width: 333px; }

b) Single-line declaration blocks with tabbed subordinates — I haven’t actually seen this method used before, but it sure looks promising. It’s like some quasi single-line, single-spacing/tabbed-indent hybrid (notice the spacing within the declaration blocks):

div#example_01 {
	padding:1px; margin:11px; width:111px; 
	}
div#example_02 {
	padding:2px; margin:22px; width:222px; 
	}
	div#sub-div_01 {
		padding:1px; margin:11px; width:111px; 
		}
	div#sub-div_02 {
		padding:2px; margin:22px; width:222px; 
		}
	div#sub-div_03 {
		padding:3px; margin:33px; width:333px; 
		}
div#example_03 {
	padding:3px; margin:33px; width:333px; 
	}

c) Meta-spacing for columnar organization — or something. Okay, I realize I’m going out on a limb trying to classify and name these different spacing techniques, but it sure is fun! ;) This meta-spacing technique employs inconsistent spacing to organize properties and values into two left-aligned columns:

* {  
	vertical-align: baseline;
	font-family:    inherit;
	font-style:     inherit;
	font-size:      100%;
	border:         none;
	padding:        0;
	margin:         0;
}

Wrap up..

These different spacing and indentation methods are merely a small sampling of the many diverse syntactical formats used in contemporary CSS implementations. One of the great things about writing CSS code is its flexibility and adaptability. Regardless of your specific style of writing CSS, consistency, usability, and practicality are absolutely essential.

Share your thoughts

What is your preferred spacing and/or indentation method? Do you use one of the above methods, or something else? Share your thoughts! ;)


Dialogue

17 Responses Jump to comment form

1Dennison Uy

June 1, 2008 at 9:23 pm

I personally use and prefer single line code blocks. The primary benefit is that I do not have to scroll too much to find what I am looking for and second, arranging all my declarations on one line allows me to quickly sort through and find the relevant ID / class / element that I need to work on.

2web design company

June 2, 2008 at 2:59 am

Great tut’s as always. Enjoyable too.

3Perishable

June 2, 2008 at 10:08 am

Hey Dennison :) Do you use tabs (indentation) to distinguish subordinate selectors? What about the spacing within the code blocks? And, within each declaration block, are you using single spaces between items (brackets, properties, values, etc.), or maybe something else (or not at all)? A million questions, I know, but inquiring minds want to know! ;)

4Eric

June 10, 2008 at 5:30 am

Interesting article; I never gave my own obsession too much thought until now — I guess I am a bit of a hybrid, what I do is with short styles is post as a single line with single space between brackets, and for styles with several lines I sort of single-space things, but then I tab out the styles in between. Make sense?

5Perishable

June 10, 2008 at 10:24 am

Yeah, that sounds like a great approach! Seems like a hybrid of method #2 and #4a. I also keep short blocks on a single line, and then tend to use them as meta-punctuation throughout the document to further organize things. I think tabbing the individual declaration blocks is much better than not spacing them or single spacing them. Thanks for the comment, Eric!

6Niels Bom

June 20, 2008 at 10:06 am

I think I prefer laying out my css with the opening curly brace on a new line, on the same column as the closing brace.
(although I must say this looks better in languages where things are truly nested)

div#example_01
{
              padding: 1px;
              margin: 11px;
              width: 111px;
}
div#example_02
{
              padding: 2px;
              margin: 22px;
              width: 222px;
}
              div#sub-div_01
              {
                            padding: 1px;
                            margin: 11px;
                            width: 111px;
              }
              div#sub-div_02
              {
                            padding: 2px;
                            margin: 22px;
                            width: 222px;
              }
              div#sub-div_03
              {
                            padding: 3px;
                            margin: 33px;
                            width: 333px;
              }
div#example_03
{
              padding: 3px;
              margin: 33px;
              width: 333px;
}

Like this.

7Perishable

June 23, 2008 at 3:10 pm

Yes, I do like that technique as well, although I am hesitant to use it for CSS. As you say, it works great for formatting programming languages that are logically nested, such as PHP, Perl, and so on. With CSS, this method does help with readability, but there is an additional amount of scrolling involved due to the extra lines required for the opening bracket. Not a huge deal, of course, especially for less-extensive stylesheets. I know that this is a very popular formatting method, but I still find all of the “floating” brackets a bit distracting! ;)

8Brook

July 16, 2008 at 8:54 am

This is a great little series of articles about CSS organisation!

If you use TextMate you might be interested in my short article about single line formatting with TextMate. This uses code folding in the text editor to achieve the benefits of single line code blocks without sacrificing the legibility of multiline blocks.

9SneakyWho_am_i

September 26, 2008 at 2:15 pm

Mine is very close to 4a with just a touch of 4b at the moment. No tabs though. I have my text editor set to override tabs with 4 spaces, but I don’t ever press the button in css.

10Tal

January 2, 2009 at 8:33 am

I like this method but how do you handle the indentation of items with multiple subordinate selectors? For example:

.class {
width:2020px;
position:relative;
}

.classA {
width:1347px;
position:relative;
}

.classB {
width:674px;
position:relative;
}

       .class .sub,.classA .sub, .classB .sub {
       position:relative;
       float:left;
       width:642px;
       height:532px;
       padding:9px 21px 42px 10px;
       }

How could I more clearly illustrate that this subordinate group of selectors applies to all three selectors above and not just .classB. Obviously the selectors tell me what they do but I’m curious how others might approach this with indentation or comments.

11Jeff Starr

January 2, 2009 at 5:07 pm

Hi Tal, that is an interesting question. The way I handle such declarations is to place the grouped selectors first, and then indent each of the individual selector’s declaration blocks. For example:

selector-01, selector-02 {
       padding: 10px;
       margin: 10px;
       border: 10px;
       }
       selector-01 .subClass-01 {
              padding: 10px;
              margin: 10px;
              border: 10px;
              }
       selector-02 .subClass-02 {
              padding: 10px;
              margin: 10px;
              border: 10px;
              }

..of course, each of the individual selectors would have their own unique set of declarations. This method works for me and helps organize even the most convoluted stylesheets. ;)

12Saddam Azad

April 30, 2009 at 7:06 pm

Great writeup on the different kinds of spacing and formatting of CSS. I prefer the Single Spacing, Tabbed Indents method. I wrote a post on how to craft beautiful stylesheets a few days ago which advocates this method.

Cheers.

Subscribe to comments on this post


[ Comments are closed for this post. ]

If you have additional information, contact me.

Contact Perishable Press

  • Contact Jeff via form

Search Perishable Press

About Perishable Press

Perishable Press is the virtual playground of Jeff Starr — visionary, founder and lead developer of Monzilla Media, a small web and graphic design company in the lush desert oasis of Moses Lake, Washington. Perishable Press features articles and tutorials on many aspects of digital design..

Read more..

Perishable on Twitter

heading out of town on a photographic excursion. great way to break up the routine.

Perishable on Tumblr

Office painted a warmish/neutral off-white color. New 2.1...

Tue, 02 Feb 2010


New office digs..


Night view..


Potential..

Office painted a warmish/neutral off-white color. New 2.1 Altec/Lansing speakers for crisp, clear sound. Two new executive desks: one for computer biz and another workspace for drawing, painting, and other offline pursuits. And of course, a big, cushy black office chair to make it all sweet.

Quick Photoshop Reset

Tue, 19 Jan 2010

Very good Photoshop trick to know: If you hold down ALT+CTRL+SHIFT (Mac: CMD+OPT+SHIFT) while starting Photoshop you can reset all settings back to factory default. Very useful if you have problems with some tools or the interface. – http://bit.ly/4A5LJ5

Insane October

Sun, 01 Nov 2009

By far the most insane month of 2009, October included the following activities:

1st week: Trip to the East Coast, beginning with some business in Connecticut.

2nd week: East Coast trip continues with much pleasure in downtown Manhattan.

3rd week: Photo and art excursions with good friend visiting from Portland, OR.

4th week: Marathon book-editing and fine-tuning for Digging into WordPress.

Now that November is here, things remain busy, but I am hoping to get a chance to restore some balance and regain my equilibrium. Of course, the holidays are right around the corner..

Import Feeds to Facebook

Mon, 07 Sep 2009

Seems like a lot of misinformation and confusion out there on how to import and display your feeds on Facebook. Here is what worked for me:

1. In the lower left-hand corner of your Facebook account, click on “Applications” > “Notes”.

2. In the upper mid-right column, click on “Import a blog” in the “Notes Settings” panel.

3. In the “Import an External Blog” panel, enter your feed URL and check the little box.

4. Click the “Start importing” button and then click on “Confirm Import” on the preview page.

That’s all there is to it. Don’t forget to edit your “Notes Privacy” settings to ensure that people can see and comment on your imported feed items.

Once you successfully import your feed(s), they will appear by clicking on the “Notes” button in the left sidebar of your Home page. Also, your timeline or “Wall” will also display the most recent post from each of your feeds as they are published and pulled into Facebook. This makes it easy for your “Friends” to see what you have been up to elsewhere on the Web.

help me in plain english

Mon, 31 Aug 2009

This has got to be the most ironic comment I have ever read:

“hi i dun a stupid noooby mistake and dint think about encrytion i just put a pass in the change pass box and now when i attempt to see my main.php or index.php its sayin password no and error how can i reset back to having no password or were can i edit the bit so that a pass is automattically seen or if not posable how can i make it so i can put in the pass i made at some point so i can login this way? the 3rd is most prefered as this will help me with other projects i am planning as i am a php noob :s plz sum1 hu is clever help me in plain english”

Thanks, “jay” — you made my week with that one.

Read more on Tumblr..

Subscribe to Comments Recent Dialogue

  • kn33ch41: "But for good browsers such as Opera and Safari, most users are quite savvy, understanding the game and always keeping their browsers...
  • Paul: First off great list. Probably the one of the best I've ever seen. I enjoyed your limiting of The Wall, and Dark Side of the Moon. He...
  • kn33ch41: I use method three, and use method four when a selector only requires one property....
  • r-a-y: Great code. Just wondering if, in certain scenarios, a permalink can be longer than 255 characters. I can think of a weird exam...
  • Jeff Starr: Thanks Oliver! Glad you enjoy the book. Thanks for the great feedback :)...
  • Perry: I've read a number of these Image Preloading techniques today and would like to know the best approach to use. I use PHP 5.3 on th...
  • Infographiste PAO: I didn't know that Jeff, that's a kind of great news, usually ppl don't know about no or dofollow so now it's gonna be alright withou...
  • Bill Brown: @Jakub: Of course, there are many ways to bypass the XML declaration issue when you have PHP short tags enabled. Since the intenti...
  • bucabay: AT Jakub ...
  • Jakub Lédl: Oh sh*t, I came back. What happened to that code I posted? Does this CMS remove PHP from comments or what? I now look like an idiot :...

Read more recent comments..

Attention: Do NOT follow this link!