Style HR Horizontal Rule with CSS

May2710May 27, 10

Update: (May 27th, 2010) Corrected IE issue, removed outdated Opera and Safari workarounds.

It's a tricky subject, the <hr /> element. Like the <br /> element, it's syntactically odd and browsers have specific default styles for it that vary quite a bit. Since the goal is always to keep our HTML uncluttered, I thought I would explain how to work with this unruly piece of a document.

First, the HTML for this tutorial is always the following:
<hr />

 - That's how the HTML specifications say we are to indicate a horizontal line, so that's all we want to use in the content layer. Presentation, on the other hand, will be a little more messy and unusual. First, let's remember the major browser quirks associated with the HR element, many of them from this article:
Internet Explorer allows color to define the background color, and gives it precedence over background-color
The border property does not behave correctly in Opera
The height property is (by default) broken in Safari
With these rendering issues in mind, the goal is to make our HR elements look like they have been cut into the page - a common visual effect used most notably by Apple:


Not that we should do everything Apple does, but they can whip up a mean UI.

To avoid this going on forever, here are the two bug fixes that need to be used to get things working across the major browsers:

The workarounds below are no longer needed by Opera and Safari:
Fixing the height property in Safari:
This one is simple, but not intuitive. For whatever reason, the height property is limited to a minimum of 3px in Safari. The easiest way to get around this is to set the max-height property to whatever the height of your HR needs to be. In our case, we're only using borders to style the element, so the max-height should be 0.
height: 0;
max-height: 0;

Fixing Opera's borders:
It looks like Opera renders the border-top and border-bottom properties correctly. That's a bonus, because those are the only ones we need. You might have noticed, though, that Opera has the same problem as Safari when setting the height of a horizontal rule element. Thankfully, the fix is also the same.


Making Internet Explorer 6 Behave:
First, we're not using the color or background-color properties, so that eliminates those IE bugs. Second, I don't test on IE6 because that browser only accounts for about 2% of the visitors to most of my sites. But, everyone should at least be able to view the un-styled HTML properly. The beauty of using only a single HR element to show horizontal lines is that the degradation is already built into every browser.

However, it's always a good idea to predict when IE6 can be made to work with little extra effort. Typically, the problems people run into are with minimum heights due to the font-size property having precedence, and floats not breaking properly. To avoid these potential annoyances, I decided to include a few lines:

font-size: 1px;
line-height: 0;
overflow: visible;

The rest is the same as you would have used if styling an empty div element instead of an hr element:

display: block;
position: relative;
padding: 0;
margin: 8px auto;
width: 100%;
clear: both;
border: none;
border-top: 1px solid #AAA;
border-bottom: 1px solid #FFF;
font-size: 1px;
line-height: 0;
overflow: visible;

Now we've got a working horizontal rule, styled properly using only CSS. 
It looks like this:



And in Internet Explorer 5.5, we have the exact same style applied:

Beautiful, sleek, and it looks identical across all the browsers people use.


About Jason Miller:

I am a JavaScript developer from Waterloo, Ontario, Canada. When I am not typing green code onto a black screen, you might find me at the nearest coffee pub checking out the brew. I run a internet firm called developIT and maintain blogs and web apps when I can.
Comments
wsvap#
Not working in IE6 :) (test this page in IE6)
jason#
What does it look like in IE6? I no longer test in that browser (I can't find a Windows install old enough and got sick of running VMs while Photoshop was open)

I don't care about IE6 as long as the fallback is some kind of horizontal bar. If it's completely disappeared, then something nasty with the star hack might be in order.
ohreally#
"presidence" ??
jason#
@ohreally: Good catch, I must have had Spell Check disabled when I wrote this.
Peter#
Yep, works great, though I had to remove 'clear: both' to get them all spaced correctly (using more than one on a page). First one was incorrectly spaced above the line, before I removed this, the rest were fine.
Drew Geraets#
You should also add height: 0; - otherwise you end up with a pixel space between the borders in Chrome and Safari.
jason#
@drew: On what platform? I'm not seeing any space in Chrome or Safari under Windows or OS X...
Lisa#
I'm seeing a huge gap in between paragraphs in Google Chrome. How can I fix this to make it look like the one I see in Internet Explorer 9?
jason#
@lisa: that would seem to be your paragraph styling, which differs wildly between browsers. Try this:

p {
padding :0;
margin: 0.5em 0;
}
Drew#
Hi Jason. HTML5 spec for the hr tag is simply <hr>, and not <hr />. Your post is showing the spec for XHTML, and not current HTML spec. Hope that helps!
Drew#
Hi Jason. HTML5 spec for the hr tag is simply <hr>, and not <hr />. Your post is showing the spec for XHTML, and not current HTML spec. Hope that helps!
jason#
@Drew: This post was written before HTML5 was a thing! I used to love the idea of XHTML, likely because I am bad at writing parsers. Perhaps I will correct the self-closing tag if I ever start writing content for this site again ;)
chanyuan#
http://www.nikelunarglide.us
http://www.poloralph.us.com
http://www.toryburchoff.us.com
http://www.belstaffoutlet.us.com
http://www.cheapraybans.ca
http://www.christianlouboutinshoessale.org.uk
http://www.birkenstockshoes.org.uk
http://www.truereligion-jeans.us
http://www.michael--kors.me.uk
http://www.salvatoreferragamo.in.net
http://www.michael-korstaschen.ch
http://www.juicycoutureoutlet.us.com
http://www.basketballshoesstore.us.com
http://www.vibram-fivefingers.us.com
http://www.fingerlingsmonkey.org
http://www.ralphlauren.in.net
http://www.adidasnmdforsale.us.com
http://www.adidasjeremyscott.in.net
http://www.nikekyrie3.us.com
http://www.outlet-celine.com
http://www.y3shoes.us.com
http://www.ralph-laurenpolouk.org.uk
http://www.burberry-outlet-stores.us.com
http://www.huarache.us.com
http://www.ray-banssunglasses.org.uk
http://www.louisvuitonnoutlet.com
http://www.tory-burch.org.uk
http://www.guccislides.us.com
http://www.pradaoutletonlinestore.us.com
http://www.newbalance-shoes.us.com
http://www.nikecortez.net
http://www.supremeparis.fr
http://www.katespadeoutletsale.us.com
http://www.ralph-laurens.co.uk
http://www.kobe12shoes.us.com
http://www.audemarspiguet.us.com
http://www.converse.me.uk
http://www.valentino.us.org
http://www.nikeoff.com
http://www.rayban-sunglassesonsale.us.com
http://www.ralphlaurenoutletofficial.us.com
http://www.adidasoutletadidas.us.com
http://www.michaelkorsoutlet70off.us.com
http://www.cheapmlbjerseysshop.us.com
http://www.converseshoes.us.org
http://www.redbottomsshoes.me.uk
http://www.hermesbags.me.uk
http://www.jordan1.us
http://www.cheapnfljerseysnike.us.com
http://www.pradahandbagsoutlet.us.com
http://www.raybansunglassesforwomen.us.com
http://www.alexandermcqueen.us.com
http://www.christianlouboutinoutletus.us.com
http://www.pandorajewelrysoutlet.us.com
http://www.jordan5.us
http://www.adidaswings.name
http://www.louisvuittonofficial.us.com
http://www.nikeairmaxuk.co.uk
http://www.rayban.net.co
http://www.oakley-sunglassesonsale.us.com
http://www.balenciaga-triples.us.com
http://www.korsmichaelkors.org.uk
http://www.katespade-outlet.in.net
http://www.michaelkorss--outlet.us.com
http://www.cheapbestnbajerseys.us.com
http://www.louis--vuittonhandbags.us.com
http://www.airmax95.us.org
http://www.soccerjersey.us.com
http://www.timberlandoutlet.us.org
http://www.coach-factoryonline.us.org
http://www.nike-airhuarache.me.uk
http://www.kobe11.in.net
http://www.cheapreplicawatches.us.com
http://www.jimmychoooutlet.us.com
http://www.lightup-shoes.us.com
http://www.coachoutlet-factory.us.com
http://www.swarovskijewelry.co.uk
http://www.oakleysunglassescanada.ca
http://www.coachfactoryoutlet-onlinestore.us.com
http://www.louboutins.us.com
http://www.oakleysunglassesparts.us.com
http://www.cheapnfljerseysstore.us.com
http://www.fitflopsoutlet.us.com
http://www.coachfactoryoutlet-online.eu.com
http://www.nikeairforce1.net
http://www.bcbg.us.com
http://www.undefeated.us.com
http://www.pumaslides.us.com
http://www.airforce1.in.net
http://www.cartierjewelry.us.com
http://www.airhuarache.fr
http://www.taghuer.us.com
http://www.toryburchbags.us.com
http://www.fila.us.com
http://www.baseballjersey.us.com
http://www.pandorajewelrycanada.ca
http://www.suicoke.us.com
http://www.stuartweitzmanshoes.us.com
http://www.supreme-newyork.com
http://www.rayban.in.net
http://www.longchampoutlet-online.com
http://www.coachoutlet70off.us.com
http://www.runningshoes.us.org
http://www.linksoflondonuk.me.uk
http://www.suprashoesclearance.us.com
http://www.pandoracharmjewelry.us.com
http://www.michael-kors-uk.me.uk
http://www.malonesouliers.us.com
http://www.raybansunglassesoutlet.net.co
http://www.mizuno.us.org
http://www.jordans-fusion.com
http://www.tomsoutlet.in.net
http://www.mbtscarpe.it
http://www.nikeblazer.fr
http://www.louisvuittonoutletbuy.us.com
http://www.nikeairmax.me.uk
http://www.nikejordans.co.uk
http://www.balenciaga.us.org
http://www.sunglassesoakleycheap.us.com
http://www.montblancpen.in.net
http://www.airjordan11.us
http://www.supremeclothing.us.com
http://www.swarovskijewelryoutlet.us.com
http://www.louisvuittonusa.us.com
http://www.newbalance.me.uk
http://www.cheapraybansunglasses.net.co
http://www.newcoachoutlet.us.com
http://www.manoloblahnik.us
http://www.hardenvol2.us.com
http://www.nhlhockey-jerseys.us.com
http://www.dolceandgabbana.us.com
http://www.truereligionoutletjeans.us
http://www.adidasflipflops.us.com
http://www.cheapnhljerseysshop.us.com
http://www.fitflopssaleclearance.us.org
http://www.burberryoutletonline.us.org
http://www.airmax2017.us.com
http://www.ferragamooutlet.us.com
http://www.nikeairmaxschweiz.ch
http://www.coachsoutletstoreonline.us.com
http://www.rolexwatchess.us.com
http://www.balenciagasandals.us.com
http://www.rolexwatches.us.com
http://www.ralph-lauren.com.co
http://www.montblancfountainpens.us.com
http://www.rolexwatch.us.com
http://www.poloralphlaurenpascher.fr
http://www.toryburchoutletsaleonline.us.com
http://www.nikerevolution.us
http://www.michael-korsoutletstores.us.org
http://www.rolexwatch.me.uk
http://www.ray-bansunglassesoutlet.com.co
http://www.adidasstoreuk.me.uk
http://www.cheap-jordans.us.com
http://www.montblanc.com.co
http://www.coachfactory--outlet.us.com
http://www.ralplaurenuk.org.uk
http://www.jerseys.us.org
http://www.michaelkorshandbagsmacys.us.com
http://www.michaelkorsoutlet.us
http://www.adidasslides.us.com
http://www.airjordan-14.us
http://www.louis-vuittonoutletus.us.com
http://www.nikeukstores.me.uk
http://www.givenchyhandbags.us.com
http://www.fitflopsandals.org
http://www.marcjacobs.me.uk
http://www.bestnfljerseyswholesale.us.com
http://www.louis-vuitton-borse.it
http://www.coachonline.us.org
http://www.nikeoutletus.us.com
http://www.niketn.fr
http://www.jordans.us.org
http://www.asics.us.org
http://www.flops.us.com
http://www.adidasoriginalsstansmith.us.com
http://www.breitling.us.com
http://www.nikeshoesnike.us.com
http://www.cheapmichaelkorshandbags.us.org
http://www.niketennis.us.com
http://www.adidasoutletstores.net
http://www.kevindurantshoes.us.com
http://www.hollisteroutlet.us.org
http://www.vansshoesoutlet.us.com
http://www.jordans.in.net
http://www.coachoutlet-online.eu.com
http://www.jordan12.org
http://www.coachhandbagsfactoryoutletonline.us.com
http://www.converseshoesoutlet.us.com
http://www.louisvuittonparis.fr
http://www.ralphlauren-poloshirts.co.uk
http://www.airmaxplus.org
http://www.prada.us.org
http://www.coachwallets.us.com
http://www.poloralphlaurenoutlet.org.uk
http://www.nfljerseyshop.us.com
http://www.newbalancesandals.us.com
http://www.burberrysale.us.org
http://www.stussy.us.com
http://www.nikesshoes.us.com
http://www.louisvuittonstores.us.com
http://www.nikepresto.us.com
http://www.viphandbags.us
http://www.pandorajewelry-canada.ca
http://www.maccosmeticsofficial.us.com
http://www.shoecarnival.us.com
http://www.christianlouboutinoutletuk.org.uk
http://www.ralph-lauren.fr
http://www.katespadehandbagsoutlet.us.org
http://www.airmax2018.in.net
http://www.nikeepicreactflyknit.us.com
http://www.airmax270.org
http://www.authenticlouisvuittonhandbags.us.com
http://www.jordanshoes.us.org
http://www.polooutletinc.us.com
http://www.nikefreeruns.us.com
http://www.mcmbackpack.us.com
http://www.oakleysunglassescom.us.com
http://www.longchampoutletcom.us.com
http://www.tods.us.com
http://www.nikeroshe.us.com
http://www.beatsbydrdresolo.us
http://www.underarmouroutletclearance.com
http://www.birkenstockoutletonline.us.com
http://www.mulberryukhandbags.org.uk
http://www.pandora-jewelry.name
http://www.longchampoutlet-store.us.com
http://www.louboutinchristian.us.com
http://www.pradabags.us.com
http://www.hermesbeltoutlet.com
http://www.gucci-outlet.cc
http://www.coachoutletonlinestore.ca
http://www.adidasultra-boost.us.com
http://www.air-jordan.us.com
http://www.nikeairmax90.us.org
http://www.coach-bags.us.org
http://www.hermeskellybag.us.com
http://www.off-whiteclothing.us.com
http://www.nikefree.net
http://www.coachoutletonline-sale.us.com
http://www.michaelkorsoutletoff.us.com
http://www.bottega.in.net
http://www.coachoutletonsale.us.com
http://www.adidasshoess.us.com
http://www.supremeuk.co.uk
http://www.coachcom.us.com
http://www.truereligion-jeans.me.uk
http://www.vancleefarpelsjewelry.us.com
http://www.timberlandoutletuk.org.uk
http://www.longchamphandbagstotes.us.com
http://www.birkenstocksandalsonsale.us.com
http://www.hermesoutlets.us.com
http://www.michaelkorsoutletcheapest.us.com
http://www.oksunglasshut.com
http://www.yeezydesertrat500.us.com
http://www.nikefree-run.fr
http://www.stansmithadidas.org.uk
http://www.wholesaleoakleysunglasses.us.org
http://www.adidasoutletstores.com
http://www.poloshirtsoutlet.us.com
http://www.baseballbats.us.com
http://www.columbia-sportswear.us.com
http://www.manchesterunitedjersey.us.com
http://www.nikesb.in.net
http://www.burberryscarves.us.com
http://www.authenticmlbjerseyscheap.us.com
http://www.michaelkorsoff.us.com
http://www.cheapnfljerseys.us.org
http://www.outletmichaelkorsoutletonline.us.com
http://www.cheapjerseys.in.net
http://www.airjordan.me.uk
http://www.christianlouboutinoff.us
http://www.blackhawksjersey.us.com
http://www.louisvuitton-handbags.com.co
http://www.kanyeyeezyshoes.us.com
http://www.beatsheadphones.us.com
http://www.louis-vuittonoutlets.us.org
http://www.nikefree.me.uk
http://www.michaelkorsonline.ca
http://www.mishka.us.com
http://www.snapbacks-wholesale.us.com
http://www.cheap-oakleysunglasses.com
http://www.hermesbirkinbags.us.com
http://www.weddingshoes.us.com
http://www.tomsoutletonline.us.com
http://www.superdry.me.uk
http://www.mbt.us.com
http://www.hardenvol1.us.com
http://www.fitflopssale.org.uk
http://www.louboutins.me.uk
http://www.redbottomshoes.org.uk
http://www.ralphlaurenpolo.in.net
http://www.pandorajewelry.us.org
http://www.vipbestbags.com
http://www.coachoutletstoreonlineofficial.us.com
http://www.nikeairmax90.com.de
http://www.airjordanretro.fr
http://www.coachoutletfactoryofficial.us.com
http://www.coachoutletonline70off.us.com
http://www.cheapraybansunglass.co.uk
http://www.nikefactoryoutletstores.us.com
http://www.off-white.us.com
http://www.michaelkorsoutletcheapest.us.com
http://www.katespadestoreoutlet.us.com
http://www.timberlandoutletonline.us.com
http://www.brequet.us.com
http://www.airmax.us.org
http://www.airmaxnl.nl
http://www.chaussuresnikeairmax.fr
http://www.oakleysunglasshut.us.com
http://www.outletralph-lauren.us.com
http://www.nikeflipflops.org
http://www.hervelegerdresses.us.com
http://www.louisvuittonlvoutlet.us.com
http://www.timberland.us.org
http://www.pandoraukcharms.co.uk
http://www.oakleysunglasses-vault.us.com
http://www.louboutinoutlets.us.com
http://www.truereligionjeans-outlet.us.com
http://www.barcelonajersey.us.com
http://www.dansko.us.com
http://www.airmax97.org
http://www.poloralphoutlet.us.com
http://www.miumiu.us.com
http://www.cheapjordansshoes.in.net
http://www.asics.org.uk
http://www.redbottoms.us.org
http://www.truereligionsale.com.co
http://www.ferragamoshoesoutlet.us.com
http://www.cheapmlbjersey.us.com
http://www.vansshoes.us.org
http://www.adidasfootballboots.org.uk
http://www.longchamp-outletstore.us.com
http://www.burberryoutlet-canada.ca
http://www.jordanxx9.in.net
http://www.louis-vuittonoutlet.net.co
http://www.katespadehandbagssale.us.org
http://www.jordanretro.us
http://www.air-jordanpascher.fr
http://www.michaelkorsshandbags.us.com
http://www.coachoutletstore.eu.com
http://www.nikeshox.in.net
http://www.nikedunks.us
http://www.louisvuittonoutletmalls.us.com
http://www.louissvuitton.us.com
http://www.timberlandpro.us.com
http://www.raybansunglassescheapest.us.com
http://www.coachoutlet-handbags.us.com
http://www.nikemercurial.us
http://www.rayglasses.us
http://www.adidasnmdoutlet.us.com
http://www.christian-louboutins.fr
http://www.lebron15.in.net
http://www.pumafentysandals.us.com
http://www.katespade-outlet.us.com
http://www.adidassuperstarshoes.us.com
http://www.nikeblazers.us
http://www.airmax1.us
http://www.fitflops.us.org
http://www.stephen-curryshoes.us.com
http://www.adidasyeezy350boost.us.com
http://www.niketrainers.org.uk
http://www.airmaxnike.me.uk
http://www.christiandiorhandbags.us.com
http://www.nikeoutletstoreonline.us.com
http://www.longchampsacsoldes.fr
http://www.giuseppe-zanotti.us.com
http://www.flipflops.me.uk
http://www.bvlgari.us.com
20185.18wengdongdong
Leave a Comment

Post Comment