This is an older version of Design Intellection. Access the new one: http://designintellection.com/.

Design Intellection is a small web design company committed to making the web a better place.

Contact

Subscribe to the blog feed

200

Writing CSS Code: Inline and/or/vs. Block

A while ago there was a post by Steve Smith of Ordered List about how he formats his CSS on a single line per selector, brackets and attributes. I call this inline CSS. For the longest time I strictly adhered to a formatting quite the opposite, which I call block CSS. An example of both is shown in Figures 1 and 2.

  1. #example-div { width:400px; margin:0; padding:0; float:left; border:1px solid #999; background-color:#ddd; }
Figure 1: This is an example of what I'm labeling inline CSS.

  1. #example-div
  2. {
  3. width:400px;
  4. margin:0;
  5. padding:0;
  6. float:left;
  7. border:1px solid #999;
  8. background-color:#ddd;
  9. }
Figure 2: This is an example of what I'm labeling block CSS.

I'm in the camp that would rather have a file with hundreds of lines than have to look through all of the attributes as they're formatted in Figure 1. However, there are some cases when block CSS seems a little overkill, such as Figure 3.

  1. #example-div a
  2. {
  3. text-decoration:none;
  4. }
Figure 3: This takes four lines for one attribute!

I began to wonder if maybe I was being too strict with the block-only approach. Surely, it seemed, that the inline approach was more beneficial in some cases. Each had their benefits and liabilities, maybe I could combine the two for one harmonious style sheet?

At some point, I'm not exactly sure when, I did begin to mix the two and my CSS began to look more like what's shown in Figure 4.

  1. #example-div
  2. {
  3. width:400px;
  4. margin:0;
  5. padding:0;
  6. float:left;
  7. border:1px solid #999;
  8. background-color:#ddd;
  9. }
  10.  
  11. #example-div a { text-decoration:none; }
Figure 4: A synergistic combination of inline and block CSS.

Now as far as what determines whether the selector gets the block format or the inline format varies; there's no exact criteria, at least not yet. Generally, selectors with few properties will be written inline and those with more styling will get the block. I found that formatting CSS this way lends itself to an easy-to-follow rhythm as the style sheet grows (Figure 5).

  1. #example-div
  2. {
  3. width:400px;
  4. margin:0;
  5. padding:0;
  6. float:left;
  7. border:1px solid #999;
  8. background-color:#ddd;
  9. }
  10.  
  11. #example-div a { text-decoration:none; }
  12. #example-div a:hover { background-color:#0f0; }
  13.  
  14. ul#main-nav { list-style:none; border-top:1px solid #ddd; }
  15. ul#main-nav li { border-bottom:1px solid #ddd; }
  16.  
  17. ul#main-nav a
  18. {
  19. width:150px;
  20. display:block;
  21. font-family:Georgia, "Times New Roman", serif;
  22. font-size:14px;
  23. color:#333;
  24. background-color:#eee;
  25. }
  26.  
  27. ul#main-nav a:hover { color:#f00; background-color:#fff; }
Figure 5: A nice rhythm begins to emerge as the style sheet grows. (Or maybe I'm full of it!) Note that it would look a bit better with syntax highlighting.

Next up:

<br />

vs.

<br/>

! (Just kidding)

Send a Message Have something to say? Use the form below to email your comment directly to me.
If warranted, I’ll do my best to respond in a timely matter.

Legend * = Required

(Hint: It’s David)

Content © David Yeiser, 2007–2009 | Published with Habari.