httpremix.com

About the Author

Jonah Ellison lives in Seattle, Washington and works at a web firm developing websites and custom web applications for the LAMP solution stack. He enjoys optimization, front-end usability, databases, clean code and clever solutions. Contact him or learn more.

 

Topics

Related Articles

A Different Approach to Writing CSS 2008 May 3

Topics: css — by jonah ellison

How do you write your HTML/CSS? Do you use unordered lists for links? Lots of whitespace in your CSS file? Don't know when to use an id selector or a class selector? Learn how and why to change your coding style.

First, let's take a look at a design element that every website has: the navigation menu.


  1. <ul class="navMain">
  2.     <li><a href="#" class="active">Home</a></li>
  3.     <li><a href="#">Products</a></li>
  4.     <li><a href="#">FAQ</a></li>
  5.     <li><a href="#">About Us</a></li>
  6.     <li><a href="#">Contact Us</a></li>
  7. </ul>

And the ugly CSS:

  1. ul.navMain {
  2.     list-style: none;
  3.     margin: 0;
  4.     padding: 10px 5px;
  5.     padding-top:5px;
  6.     font-weight: bold;
  7.     }
  8.  
  9. ul.navMain li {
  10.     display: inline;
  11.     padding: 0 8px
  12.     }
  13.  
  14. ul.navMain a {
  15.     text-decoration: none;
  16.     padding: 0 0 3px;
  17.     border-bottom: 4px solid #ffffff;
  18.     color: #999999;
  19.     }
  20.  
  21. ul.navMain a.active {
  22.     border-color: #F60;
  23.     color:#06F
  24.     }
  25.  
  26. ul.navMain a:hover {
  27.     border-color: #F60;
  28.     color: #666666;
  29.     }

There are several things that bug me about the above code:

  1. Overuse of HTML tags (<ul><li>)
    The point of CSS is to use less HTML tags, not more. Why use unordered lists and then completely change the functionality of how they display when we can do without them? And what happens when we need to override how the core <ul> and <li> elements display (e.g.: How to Style Perfect HTML Lists) but didn't "reset" the list in our CSS? (Did you catch how ul.navMain li { ... } is missing margin: 0?) Less code is always better!
  2. id selector vs class selector
    The id selector (id="idName") and class selector (class="className") have almost the exact same functionality. The general rule of thumb is that if your element/style only appears once on your page, use an id selector. Otherwise, use a class. Multiple classes may also be used within a single element (class="className1 className2 className3"), while id selectors cannot because they're designed to only be used once as a way to identify an unique element.
  3. Ugly Formatting
    Welcome to the great debate of single-line vs multi-line! My god, look at all that hideous whitespace! Now, whitespace is great for learning, but we're not newbies anymore, right? If you don't understand the CSS you're writing (or copy-pasting) or it's rather convoluted, then I don't blame you for wanting to see every little property on the screen. But otherwise, single-line is great if you want to hack out CSS quickly.

    The advantage is that a shorter document is created: less scrolling is involved making it much easier to pick out elements. Quicker changes = more efficiency = time saved. And it's not like single-line CSS looks horribly messy--it's incredibly easy to know what's going on with a single glance. We can look at any random line in a CSS file and know exactly where we are within the document, and that's beauty.

    Does less vertical scrolling equal more horizontal scrolling? Not necessarily. When making updates, there are certain properties we're just not going to change, like display or float or even width. We put these at the very end of the line. color and font properties are nice to have up front (as clients love to play with these). We can also group similar attributes together, which we should be doing anyway, or use shorthand CSS (e.g., background:#000 url(../img/scan_lines.gif) repeat-y; margin:10px 20px; etc.). And with widescreen monitors becoming more prevalent, this leaves developers with more horizontal real estate that should be put to use.

    Now there are several arguments against that I can understand: you use/need a special third-party tool to help you write CSS; you have word-wrapped turned on; you like to validate your CSS and want an exact line; or you are working with single-line haters. Otherwise, it's not "personal preference" when there is an obvious benefit to writing CSS using single lines. It took me a couple years before I switched over, but once I did I've never looked back.

The above CSS, rewritten:

  1. <div id="navMain">
  2.     <a href="#" class="active">Home</a>
  3.     <a href="#">Products</a>
  4.     <a href="#">FAQ</a>
  5.     <a href="#">About Us</a>
  6.     <a href="#">Contact Us</a>
  7. </div>

  1. #navMain { font-weight:bold; padding:10px 5px; }
  2. #navMain a { color:#999; padding-bottom:3px; margin-right:1em; border-bottom:4px solid #fff; text-decoration:none; float:left; display:block; }
  3. #navMain a.active { border-color:#f60; color:#06f }
  4. #navMain a:hover { color:#666; border-color:#f60;}

I'd rather look at 4 lines of CSS than 29 lines any day of the week.


2 Comments »

1. Comment by Martinique – 2008 October 7 @ 2:00 pm

Using list elements to build navigation menus is not really waste of HTML, because not everybody uses a regular browser. Screen readers, for example, can skip a ul-based list of boring navigational links and move on to your actual content quite easily, while your div-based approach would probably force the listener to hear them on every one of your pages. A non-CSS browser such as Lynx, which is often used for debugging or emergency situations, also benefits from proper use of list elements. Very often links wrapped in list elements are also helpful when doing some more exotic CSS styling.

2. httpremix.com – Comment by jonah ellison – 2008 October 9 @ 3:52 pm

99% of one’s visitors use a “regular browser.” For accessibility, much more care must be taken, as simply creating ul-li links does not ensure that a screen reader will “skip” them, and other techniques can be used anyway (see http://www.webaim.org/techniques/skipnav/). Lynx will still display the links, only they won’t be in bullet form (and let’s admit it: using Lynx is a rarity). And links can just be wrapped in a div for advanced styling.


Leave a comment

RSS feed for comments on this post - TrackBack URL