CSS Formatting Tips

“…there is a huge likelihood that the systems will evolve from lower to higher entropy, which is a rough statement of the second law of thermodynamics.”
— Brian Greene

Add Meta Information

Without a scaffold, CSS has the tendency to grow like an untamed garden during the development process. To begin designing a scaffold for your styles, it’s helpful to make comparisons between your stylesheet and a book.

Much like stylesheets, books are descriptive narratives that rely on a strict organizational scheme in order to make sense. So it follows that—just as books use a “table of contents” to allow for easy navigation between sections—we too can generate a system that allows us more easily navigate and maintain our CSS.

At the top of each stylesheet, I list meta-information about the stylesheet itself and provide a table of contents that will help other designers understand the structure of my markup. My list usually includes these items:

  • Author
  • Title
  • Description
  • Table of Contents
  • Important Colors
 1 Title: application.css
 2 
 3 Author(unless otherwise noted): billy@8thlight.com
 4 
 5 Description: This is the main stylesheet for www.inserturlhere.com
 6 
 7 Contents:
 8 
 9 1. =R ESET
10 2. =G LOBAL
11 3. =T YPOGRAPHY
12 4. =S TRUCTURAL
13 5. =M EDIA QUERIES
14 
15 Colors:
16 
17 rgb(12,75,38); — Green
18 rgb(255,143,27) — Orange
19 rgb(33,41,201) — Blue

By prepending an “=” sign to your content list entires, you can easily navigate between important sub-sections of your stylesheet by using the “find” feature of your chosen editor. For example, if you wanted to quickly navigate to the “typography” section, you would search for “=T.”

 1 /* =T Y P O G R A P H Y
 2 ---------------------------------------------------- */
 3 
 4 h1, h2, h3, h4, h5, h6 { font-weight: 300; }
 5 
 6 h1 { 
 7   border-bottom: 2px solid;
 8   font-size: 2em; 
 9   font-weight: 300;
10   letter-spacing: 0.0446em; 
11   line-height: 1em;
12   padding-bottom: 0.07143em;
13   text-transform: uppercase; 
14 }
15 
16 /* etcetera... */

Organize Property/Value Pairs

Craftsmanship is all about paying attention to details: meticulousness begets quality. I always make it a point to alphabetize the property/value pairs inside my declaration blocks. Doing so makes it much easier locate specific properties inside of long declaration blocks and adds a level of predictability and usability to my stylesheets.

Of the two examples below, which do you think is easier to parse?

 1 /* Example One: Alphabetized Declarations */
 2 
 3 p[role="complementary"] a {
 4   background-color: rgb(235,107,16);
 5   border: 2px solid rgb(69,64,61);
 6   color: rgb(255,255,255);	
 7   display: block;
 8   font-size: 1.2em;
 9   font-weight: bold;
10   margin: 0 auto 1em auto;
11   padding: 0.75em;
12   text-align: center;
13   text-decoration: none;
14   text-shadow: 2px 2px 1px rgba(0,0,0,0.2);
15   width: 75%;
16 }
 1 /* Example Two: Randomized Declarations */
 2 
 3 p[role="complementary"] a {
 4   margin: 0 auto 1em auto;
 5   font-weight: bold;
 6   background-color: rgb(235,107,16);
 7   width: 75%;
 8   text-align: center;
 9   border: 2px solid rgb(69,64,61);
10   color: rgb(255,255,255);	
11   font-size: 1.2em;
12   padding: 0.75em;
13   text-decoration: none;
14   text-shadow: 2px 2px 1px rgba(0,0,0,0.2);
15   display: block;
16 }

Group Dependent Property/Value Pairs

We can take the organization of declaration blocks one step further by logically grouping dependent property/value pairs. This is a technique I use sparingly so as not to disrupt the rhythm of my markup, but I have found it helpful in two common situations: with positioning offsets and with browser specific CSS3 declarations.

In the case of positioning, I feel that it makes sense to indent and group positioning offsets like “top,” “right,” “bottom” and “left” underneath the property they’re helping to define, in this case “position.” I often change the value of more than one offset at a time and to me it makes sense to place them in the context of each other in the mark up.

1 h1 + p:after {
2   content: url(../images/birds-in-flight.png);
3   position: absolute;
4     right: -25%;
5     top: 0;
6   width: 10.7059em;
7 }

Quarantine CSS3 Declarations

Browser specific CSS3 declarations tend to be unruly and visually disruptive so I make it habit to quarantine them from the rest of my property/value pairs. I feel it also makes sense to separate these styles so they are easier to update as browser begin to support them natively (in which case the browser specific extensions can be dropped).

Admittedly, when doing this you’ll need to take the cascade into account, but as a rule I find this technique to be a helpful time saver.

 1 h1 + p {
 2   background: rgb(250,234,207);
 3   font-weight: normal;
 4   line-height: 1.3em;
 5   margin: 0 0 0 -2em;
 6   padding: 0.4286em 3em 0.4286em 2em;
 7   width: 75%;
 8 
 9   -webkit-border-radius: 5px;
10   -moz-border-radius: 5px;
11    border-radius: 5px;
12 
13   -webkit-box-shadow: 3px 3px 0px rgb(237,220,189);
14   -moz-box-shadow: 3px 3px 0px rgb(237,220,189);
15    box-shadow: 3px 3px 0px rgb(237,220,189);
16 
17   -webkit-transform: skew(-10deg);
18   -moz-transform: skew(-10deg);
19   -o-transform: skew(-10deg);
20    transform: skew(-10deg);
21 }

Format Declaration Blocks Consistently

The way your format your stylesheet is largely a matter of personal preference&emdash;I have seen many different approaches&emdash;but there is at least one rule that must be followed strictly: be consistent!

I format my declaration blocks following a simple method. If there are less than three property/value pairs, I keep the declaration block on a one line:

1 section footer { background: transparent; margin: 2em 0; padding: 0; }

If there are more than three property/value pairs, I format the declaration block using multiple lines.

1 header a { 
2   color: rgb(255,255,255);
3   float: right; 
4   font-size: 1.7em;
5   font-weight: bold;
6   line-height: 3em;
7 }

Add Some Panache

Finally, a little trick I picked up from designer/front-end developer Dan Cederholm: add a little panache to your stylesheet by including an ascii version of your client’s logo.

 1 /*
 2                                          -oydNMNd+
 3                                      `/yNMMMMMMMMh
 4                                    /yMMMMMMMMMMMm.
 5                                 -sNMMMMh/.hMMMMm. 
 6                  `...`        /dMMMMm+`  hMMMMd`  
 7             `+ymNMMMMMMmho:`+mMMMMy:    hMMMMm`   
 8            :NMMMMMMMMMMMMMMMMMMNs.     yMMMMm.    
 9            +MMMho:.   ./NMMMMMM:      yMMMMN.     
10             .-`       /NMMMMMMMM+    oMMMMM:      
11                     :mMMMNo`/NMMM+  /MMMMM+       
12                   :dMMMMs`   :MMMM--MMMMMo        
13                 /mMMMMy.      +MMMdNMMMMy         
14              `oNMMMMy.         NMMMMMMMh          
15            /hMMMMMs.           yMMMMMMm`          
16        ./yNMMMMmo`             sMMMMMN.           
17 /hdhhdNMMMMMMy:                hMMMMM/            
18 mMMMMMMMMNy/`                 `NMMMMy             
19 .shddhs+-                     .mNNmy`                
20 
21 
22 Title: artisan.css
23 
24 Author (unless otherwise noted): billy@8thlight.com 
25 
26 etcetera...
27 
28 */

While perhaps completely unnecessary, I’ve always felt that taking this extra step lends an air of quality to your work. It’s a memorable and surprising touch that says: I care.

Here is a link to my favorite ascii art generator:
http://www.text-image.com/index.html

Billy Whited was an 8th Light UX Craftsman from March 2011 - April 2012.