Mar 06

If a design calls for a graphical horizontal rule, often front end developers will stick in an img tag and call it a day. However, an hr tag would be more semantic - and, most rich text editors for content management systems have a button for the horizontal rule.

These styles allow cross-browser image replacement for horizontal rule. In my implementation, the IE rules needed to be embedded, but it may have simply been a style overriding elsewhere in the stylesheet. Try it in a conditional commented stylesheet first.

The modern method uses background-image to replace the hr, which is defined normally by border. IE works some wonky stuff with list-item and you may have to scoot things around to get it to work just so, but it works!

CSS:
  1. /* ------- EMBEDDED ------- */
  2. <!--[if IE]>
  3. <style type="text/css">
  4. #primary-content  hr {
  5.     display : list-item;
  6.     list-style : url(/assets/images/wide-rule.gif) inside;
  7.     filter : alpha(opacity=0);
  8.     margin-left: -10px;
  9.     width : 0;
  10.   }
  11. #secondary-content hr{
  12.     display : list-item;
  13.     list-style : url(/assets/images/med-rule.gif) inside;
  14.     filter : alpha(opacity=0);
  15.     margin-left: 0px;
  16.     width : 0;
  17. }
  18. </style>
  19. <![endif]-->

CSS:
  1. /* ----- STYLESHEET ----- */
  2.  
  3. hr{
  4.       background:url(short-rule.gif) repeat-x top left;
  5.       border: 0;
  6. }
  7. #secondary-content hr{
  8.       background:url(med-rule.gif) no-repeat top center;
  9.       border: 0;
  10. }

9 Responses to “HR Image Replacement”

  1. Joshua Curtiss Says:

    Those stinkin' front end developers. Who can trust them? :-D

    Very cool. Good to keep in mind. Thx.

  2. roScripts - Webmaster resources and websites Says:

    HR Image Replacement › Neatly Sliced

    HR Image Replacement › Neatly Sliced

  3. Skylog » Blog Archive » links for 2008-04-08 Says:

    [...] HR Image Replacement (tags: css) [...]

  4. Using CSS to Do Anything: 50+ Creative Examples and Tutorials Says:

    [...] HR Image Replacement with CSS- How to replace the HR tag with an image that works cross browser. [...]

  5. Aaron Says:

    I have tried the code and in firefox works great but in IE 7 it does not repeat the image background.

  6. 50多个极富创意的CSS演示和教程 | 帕兰映像 Says:

    [...] HR Image Replacement with CSS- 如何使用一张图片来替换HR标签且兼容各大主流浏览器. [...]

  7. KMC | Web & Internet Teknolojileri Günlüğü » CSS ile 50′den Fazla Yaratıcı Örnek ve Makale Says:

    [...] HR Image Replacement with CSS- How to replace the HR tag with an image that works cross browser. [...]

  8. Using CSS to Do Anything: 50+ Creative Examples and Tutorials | SEO & Web Design Says:

    [...] HR Image Replacement with CSS- How to change the HR attach with an ikon that entireness interbreed browser. [...]

  9. Beyonce Says:

    CSS "Cascading Style Sheets" Lessons
    css list style Properties and examples -- http://css-lessons.ucoz.com/list-css-examples.htm

Leave a Reply