Taking full advantage of CSS

May 30th, 2006

by

CSS (Cascading Style Sheets) offers many more benefits beyond that of streamlined web pages with table-less layouts and precise positioning (no more transparent 1-pixel spacer GIFs!), mentioned in my previous post. Indeed, that’s just scratching the surface of CSS.

Here are some more clever things you can do with CSS to get your website really humming:

  • Reorder your content to sit above your top and left navigation in the HTML. That will boost the keyword prominence on your pages, which is good for SEO. Then use CSS to get the page to still display as you want. CSS Zen Garden is a great example of this in action… for example, notice how the HTML doesn’t change between this layout with left-side nav and this one with right-side nav; it’s only the CSS that’s changing.
  • If you must use graphical navigation or headings instead of text-based, then use the CSS “image replacement” technique to substitute in a text link or heading tag, respectively, when the CSS is not loaded (as is the case when the search engine spiders come to visit). For example, northland.edu uses this technique well. Currently, this is much more effective for SEO than Alt attributes.

    Many of the image replacement techniques physically move the text off the screen (text-indent: -9999em; left:-9999em;display:none, etc). This isn’t desirable as search engines are starting to examine external CSS files more closely to look for spam. There are a few image replacement techniques that don’t do this type of hiding and are still accessible, namely The Leahy/Langridge Method, The Gilder/Levin Method and The “Shea Enhancement”. Each of these methods have their pluses and minuses, which the Mezzoblue article outlines quite well. (Thanks to our CSS guru Darren for this last bit of advice.)

  • Learn to code in “CSS shorthand.” With shorthand, hex codes for colors, margins, box dimensions and borders can all be abbreviated, for instance. More about this here. The efficiency of CSS shorthand translates into not only a speedier download for your customers, but also compact and tidy code that’s easier to maintain.
  • Make code that “degrades gracefully” (or, as they prefer to say now in geek circles, “enhances progressively”). Creating a separate “low-bandwidth version” or “printer friendly version” or “mobile version” of your site will sound ludicrous in years to come (heck, I think it sounds ludicrous NOW!), because CSS makes such a thing unnecessary. Check out how gracefully gotomedia.com degrades on a cell phone or PDA, for instance.
  • Correct for browser incompatibility snafus with browser-specific CSS. Does something look awry in your page layout when loading your site with the Safari browser, for instance? Internet Explorer doesn’t always play nice with the other browsers. Until the days where all the browsers follow all the same standards to the letter, browser-specific stylesheets are a useful crutch.
  • Separate the presentation layer from the content layer as much as possible and move it into an external stylesheet (in other words, a separate .CSS file). That way it gets cached by the web browser and doesn’t have to reload with each new page.
  • Plan for site-wide changes. Things change — colors, sidebars, ads, copyright dates, etc. Utilize CSS files and/or server-side includes to make future site-wide updates as painless as possible.
  • Make use of the cascading nature of CSS. Most of the styles you define will be used site-wide. Some will only be for one particular page. Then there will be occasions where you’ll want to “cascade” styles, and have certain sections of your site adopt a particular look/layout/theme that overrides or branches off from the site-wide styles. Clever use of cascading styles can lead to very efficient and elegant code.

    Warning! Geek speek ahead:

  • Be careful of overriding previously declared statements. And also be aware that specificity is important in the cascade. Declare all your tag styles first then declare your id and class selectors down the doc. That way the cascade works and can be overwritten with new selectors. (Thanks again to our CSS guru Darren for this last bit of advice.)

CSS coders: the Web Developer Firefox extension is an awesome tool for coding, debugging, and tweaking style sheets. You can display the stylesheet and the rendered page simultaneously side-by-side and then interactively edit the CSS, immediately viewing the effect of the change on the rendered page. And it makes identifying errors (be they validation, CSS, or JavaScript) a piece-of-cake. Did I mention the plugin is free? 🙂