RSS Feed  October 12th, 2008

Styling Horizontal Rule (hr) Tags with CSS

Added: January 17th, 2007 (tagged with: coding, css)

When developing an HTML template for a new site I am working on at work, I ran into an issue when trying to style a horizontal rule so that it appears the same way across all browsers. Rule number one when I design the style sheet for a web site is to remove all margin and padding settings to square all browsers: * { margin: 0px; padding: 0px; }. This removes most of the issues regarding rendering differences in Firefox and Internet Explorer (and Opera, Safari, etc.). Issues still arise, mainly in styling Unordered Lists (<ul>), but a majority of the differences are resolved with that one rule. As I have found out, horizontal rules fall outside that rule.

Here is a list of some of the Horizontal Rule quirks I have run across:

The point of this post is that horizontal rules are a pain to style cross-browser. If you want dividing points in your document for visitors who do not see style sheets, wrap your <hr> tags with <div> tags, style your division elements and don't display your horizontal rule elements:

CSS:
div.hr { margin: 5px 0px; border: none; border-top: 1px solid #D3D3D3; }
div.hr hr { display: none; }

HTML:
<div class="hr"><hr /></div>

This way, people who come to your site able to see styles will see what looks like a stylized horizontal rule, and people who cannot see styles will still see the divisor.

Example Styled

Sample Site - Styled

Example Unstyled

Sample Site - Untyled

© 2006 - 2008 Michael J. Sepcot - michael (dot) sepcot (at) gmail (dot) com