SitePoint Design View February 7th, 2004 
Issue 6 

Newsletter Archives | Advertising | Contact us  
The Useful, the Usable and the Unbelieveable in Design for the Web 

In This Issue...

Introduction

Alex Walker Welcome a new year of the Design View. After a short break, we're rearing to go and looking forward to a medium-sized year in Web design -- I figure they can't all be big years.

So far this year, we've been particularly impressed with the response to Trenton Moss's article, 'Introduction to Browser-Specific CSS Hacks', which has generated a lot of positive feedback to accompany its 9/10 rating. If you haven't read it as yet, this tutorial walks you through the basics of exploiting browser idiosyncrasies and bugs to target specific CSS rules to different browsers.

In my experience, using CSS hacks is a little like dabbling in the black arts. You learn to recite a mostly incomprehensible string of goobledygook, and then grin a 'Potter-esque' grin as you watch a nasty rendering bug vanish in a puff of pixels. There's certainly a small but undeniable buzz when you find a new hack that does exactly what you want it to.

Nevertheless, when you're toying with dark forces, there's always a chance they'll come back to bite you. Bugs do get patched and often new versions of browsers don't interpret your hacks as you'd like.

In short, keep a few points in mind.

  1. Is there a way to accomplish your goal without the hack(s)? Hacks are habit-forming and often it's tempting to conjure them up even when they're not really necessary. For instance, IE5's well-documented trouble with 'padding' can be defused by simply choosing not to use 'padding', instead using 'margin' in the internal elements.
  2. Is it possible to concentrate a few hacks at 'focal points' in your CSS, rather than sprinkling them liberally throughout your code? For instance, IE5+ renders its default 'font-size' significantly larger than most other browsers. Rather than setting the 'font-size' twice on each element, you can target a single hack to reset the body default 'font-size' in IE5, and use percentages elsewhere to scale all other elements.
  3. Would anyone lacking your 'special powers' be able to comprehend your hackery? Hacks are, by their nature, obscure little creatures. At the very least, comment your CSS so it is clear which rule targets which browser. Even better, leave a URL in the comments pointing to an explanation of how the hacks work. You may end up referring to it yourself in future.
  4. Is it possible to make a small concession on the original design, rather than constructing a mind-bending, 'Escher-like' concoction of hacks? Cross browser, pixel-perfect design is often rated more highly than it needs to be. Screen <> Paper.
  5. Is it practical to separate your hacks into separate sheet? Housing your hacks together makes them easier to care for. If you find a better hack, you can replace your old ones quickly and easily. You'll also know where to troubleshoot problems first.

Stick to sensible methods like these and you'll be controlling your hacks -- rather than the other way around.

Alex Walker,
Editor
design@sitepoint.com

Top


The CSS Anthology: 101 Essential Tips, Tricks & Hacks

Our newest release, The CSS Anthology: Essential Tips, Tricks & Hacks by Rachel Andrew will save you hours of time with:

  • answers to 22 questions about text styling & other basics
  • 9 tips for using CSS for navigation elements
  • 11 ways to use CSS for forms and user interfaces
  • 19 solutions to cross-browser compatibility issues
  • 14 essential tricks to using CSS to replace tables for layout
  • 12 experimental or browser-specific CSS hacks
  • ...and dozens more!

The CSS Anthology Book CoverBuy the book today for only US$39.95. We ship worldwide.

Bonus: Get a free CSS Quick Reference Poster for your wall valued at $9.95 for ordering directly through us.

Top


Horizontal Rulez OK!

Hail the humble HR tag. These letters generally denote 'Horizontal Rule', but if you've ever grappled with styling it, 'Horribly Random' might seem more appropriate.

The unfortunate part is that, as a structural layout widget that's recognized by everything from Netscape 2.0, to PalmOS, to IE Mac, to Searchbots, it should be very useful -- if only we had a little control over its look and feel.

So, let's have a closer look at the lay of the land, before investigating what we can do to improve things.

The first thing to know is this: If your design requires a thin milky grey line -- one that closely resembles the scuzz that grows between your bathroom tiles -- boy, are you in luck, mister!

The second thing to know is hardly any browsers agree on anything else about HRs and how to render them.

Internet Explorer: All versions of IE (including IE5.2 Mac) seem to treat the HR as an opaque foreground element -- almost like a flattened, inverted non-breaking space -- meaning you color it with '{color: color}'. Borders can be applied to this object, and will wrap around the outside of the line element. However, only the Mac version runs any 'background-color' behind the border (i.e. background-color will show through between each border dash).

Firefox/Moz: All Gecko-powered browsers, on the other hand, seem to see the HR as the equivalent of a very thin grey DIV. Following that logic, you re-color it using its background color: {background-color:color}. Borders can be added to all sides, but they will always render on top of that background color (i.e. your background-color will show between each border dash).

Safari & Opera: I've grouped these two because they seem to take similar approaches to rendering the HR. Neither 'color' nor 'background-color' have any effect on their default grey line ... at least, until you add a border, whereupon they both suddenly acknowledge the background-color, and blot out the default grey line. Go figure.

At this stage, it would seem that, with enough browser-specific hacks, we should be able to select our own color, height and width for our HR. Unfortunately, to me, that seems like a truckload of effort for what is, ultimately, a very limited result. We need a more flexible solution.

Ok, maybe we could get full control over the look and feel by using the 'background-image' -- perhaps a variation on the classic CSS 'image-replacement' technique?

In a word,... no.

IE attachs a pseudo border to every
background-image All versions of Internet Explorer tested attach a thin grey 'pseudo-border' to any background-image applied to a HR. Now the weird thing is that this isn't the true CSS border property (marked by an 'X' in the diagram) and, judging by my own and others' testing, cannot be removed in any way whatsoever. It's a very light grey, but enough to be extremely annoying.

Back to the drawing-board...

At this point, we're probably all coming to the realization that the poor old HR is waaaaay out of its depth and needs a little assistance.

What if we were to wrap our HR in a DIV? We could still retain its structural meaning within the document, but, with a little CSS to hide the real HR, we can use the more robust DIV as the vehicle for our fancy line. Non-CSS capable devices will ignore the DIV and render our 'old-skool' HR.

The XHTML/CSS is pretty simple:

XHTML:

 <div class="line"><hr /></div>

CSS:

 div.line hr{ /* take out
the troublemaking HR */
   display:none;
   }
div.line { /* DIV that wraps and replaces the HR */ background: transparent url(hr-3.gif) no-repeat center center; height: 30px; }

Ok ... that's better than nothing, you're thinking, but it's pretty clunky. You have to make sure all future HRs have the wrapping DIV or it breaks!

Let's see if a dash of well-mannered JavaScript might do the trick.

Last year, Simon Willison wrote a very cool article on making rounded corners using JavaScript. Without delving into the hardcore JavaScript hocus-pocus, the underlying concept is pretty simple: send only stripped-back, sensible mark-up to all browsers, but also include JavaScript instructions on how an advanced browser might go about 'remodelling' our basic XHTML to allow for our more advanced rendering tricks.

While Simon used this technique to add rounded corners to a box, we're going to use something similar to wrap our HR.

In theory, simpler devices (old Netscapes, Palms, Lynx, etc.) will be oblivious to these instructions and render our semantically perfect HR, while more modern browsers will build the extra structure they need to hang our fancier presentation on.

In essence, the wrapping DIV is nothing more than a figment of your browser's runtime imagination.

Here's the function that you'll need to include.

function fancyRules() { 
 if (!document.getElementsByTagName) return;  

 var hr = document.getElementsByTagName("hr");   
 
 for (var i=0; i<hr.length; i++) {           
  var newhr = hr[i]; 
  var wrapdiv = document.createElement('div');    	
  wrapdiv.className = 'line';                     
  newhr.parentNode.replaceChild(wrapdiv, newhr);  
  wrapdiv.appendChild(newhr);                     
 }
} 
window.onload = fancyRules;

I'm not going to do a line-by-line breakdown of its workings in this newsletter, but I've commented the code here. In short, if you include this function in your page, editing the CSS is all you'll need to do to control the look and feel of your HRs.

Here's an example of the code in action. View the source to witness the plain, old HRs in the mark-up and, if you like, try disabling JavaScript to see how simpler devices see the page. Selecting a HR in Firefox and selecting 'View Selection Source' is the only evidence you'll find of the wrapping DIV.

All-in-all, I think this is a reasonably elegant and easy-to-maintain alternative to a seething knot of CSS hacks, and the inclusion of a single small JS makes this a worthwhile solution in many (but not all) situations.

The previous 12 months has certainly seen a lot of new attention focused on these kinds of unobtrusive JavaScript methods, and we're expecting that to increase in the coming year.

In fact, we've almost completed the editing for SitePoint's DHTML Utopia book, written by renowned member of the 'JavaScripterati', Stuart Langridge. I read this title over the break and it is, quite simply, awesome. Keep an eye out for it soon.

Simon, who 'tech-edited' the book, is certainly convinced of a coming JavaScript renaissance. Read his thinking here:

Simon Willison DHTML & CSS Blog: Stylish Scripting
by Simon Willison

2005: The year of the DOM (23 comments)

Top


That's all for this issue -- thanks for reading! I'll see you in a few weeks.

Alex Walker
design@sitepoint.com
Editor, The SitePoint Design View

Top


Help Your Friends Out

People you care about can benefit from the wealth of information on new and maturing technologies available on the Internet. Help them learn how to do it by forwarding them this issue of the SitePoint Design View!

Send this to a friend

Download sample chapters free from every SitePoint Book!


cover shotBuild Your Own Database Driven Website Using PHP and MySQL
by Kevin Yank


cover shotThe CSS Anthology: 101 Essential Tips, Tricks & Hacks
by Rachel Andrew


cover shotThe Flash Anthology: Cool Effects & Practical ActionScript
by Steve Grosvenor


cover shotThe Web Design Business Kit
Brendon Sinclair


cover shotHTML Utopia: Designing Without Tables Using CSS
by Dan Shafer

!More information about SitePoint Books

 What's New on SitePoint!

How to Hire a Web Designer

Susan
Villecroze
By Susan Villecroze

You know you need a Website... but who will build it for you? Susan outlines the cold, hard facts you should consider when researching designers or developers to build your site, or the sites of your clients.

Build Table-less CSS Layouts with eZ publish

Bård
Farstad
By Bård Farstad

Use CSS to theme your eZ publish site! It can be done quickly and easily, as Bard explains in this step-by-step tutorial. Learn how to shuffle page elements, set your own color preferences and make visual style changes in this must-read guide.

Introduction to Browser-Specific CSS Hacks

Trenton
Moss
By Trenton Moss

CSS leaves your layout open to interpretation by browsers... or does it? In this hands-on trouble-shooter, Trenton explains why and how different browser-specific CSS hacks work -- and provides code to help you regain control over your layouts.

Beware of Opening Links in a New Window

Neil
Turner
By Neil Turner

Opening links in a new window is bad news, usability-wise. In this practical how-to, Neil reveals why, and explains best practice procedures for linking to new windows when there's no alternative.

Designing For Clients Made Easy

Derek
Ashauer
By Derek Ashauer

Clients can be among the biggest hurdles to a Web design project's success! As Derek explains, astute designers use a number of tactics to ensure they keep the project in control, on time, and on budget ...and have some creative fun along the way!

Color for Coders - Color and Design for the Non-Designer

Jason
Beaird
By Jason Beaird

Programmers *do* have to work with color sometimes! But even the least artistic coder can choose snappy color combinations with Jason's handy how-to. His must-read tutorial cuts through the artsy hyperbole to provide the nuts and bolts of color theory - and its practical application.

 Hot Forum Topics
 New Blog Entries

Design Blog:
Pixel Perfect

Manage Your Subscription Here.

!You are currently subscribed as to the HTML edition of the Design View.


CHANGE your email address here

UNSUBSCRIBE from the Design View here.

SUBSCRIBE to the Design View here.

SWAP to the 'Text-Only' version of the Design View here.


SitePoint Pty. Ltd.
424 Smith St
Collingwood, VIC 3066
AUSTRALIA


SitePoint is Hosted by
Ventures Online


Thanks for reading!

 © SitePoint 1998-2005. All Rights Reserved.

Back to the archives

Newsletter signup

Design, coding, community or marketing? Select the right newsletters right for your needs...