Getting Fieldset Backgrounds and Legends to Behave in IE

Posted on 4/08/2006 by Matt

I’m sure someone has posted on this issue before I just haven’t found an answer I’m looking for. Jeff Croft has a close answer, but he is trying to solve a different problem. I built off of his exploration for this solution.

The “problem” occurs when you are building a form with a fieldset tag and a legend tag, and then style the fieldset with a background color like so:

HTML

<

fieldset>

User Information
<input ... />
<input ... />

CSS

fieldset{
background: #ccc;
}

Proper rendering (in every browser but IE is close to this: (this specific screenshot is from Firefox)

Correct Rendering of Form

IE does something a little different: it causes the background color to be applied to the legend as well, mysteriously causing the background color to “spill” out of the fieldset.

Default Rendering in IE

The solution is to dissociate the legend tag from the normal flow of the document. That is, absolutely position it. Here’s how: (Since IE is the only browser that needs help with this, we are going to use the Holly Hack, so it only applies to IE)

  1. Give the fieldset a position: relative; to make sure we are positioning the legend tag relative to the fieldset.

    * html fieldset{
    position: relative;
    }

  1. Give the legend a position: absolute; to break it out of the normal flow of the fieldset. This will position the top left corner of the legend with the top left corner of the fieldset, so we need to bump it up half a character and to the right half a character. Like so:

    * html legend{
    position:absolute;
    top: -.5em;
    left: .5em;
    }

  1. Now we’re almost there. By way of a little cleanup, we are going to apply some top padding to the fieldset and a top margin to the fieldset in order to make space for the label tag which is just floating on top of everything.

    * html fieldset{
    position: relative;
    margin-top:1em;
    padding-top:.75em;
    }

  1. Voila! You have a form with a fieldset and a legend that looks nearly identical in all browsers.

Fixed form in IE

See an example here.

Comments

  • http://www.kralloyun.org kralloyun

    thank you.

  • http://tefflox.net Jesse

    A w e s o m e !

  • bob

    THANK YOU FOR THIS!!

  • Pingback: Mega Awesome CSS Resource List! | WebDevLounge | design, development, SEO and wordpress | articles, discussion and community

  • http://www.paolo.cc Paolo

    Thanks, useful guide. it's easy and simple. I'll try your solutions right now… :)

  • Pingback: 40 CSS Web Form Style Tutorials For Web Developers | Social CMS Buzz

  • Pingback: KMC | Web & Internet Teknolojileri Günlüğü » Getting Fieldset Backgrounds and Legends to Behave in IE

  • http://pemberthy.blogspot.com jpemberthy

    tysir!

  • MP145

    Thanks Matt…i have to say Voila! too :)

  • asdfasdf

    asdfsdaf

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • http://www.paranoiaparadise.com T-Shirt Girl

    Wow this is a two year old bug and I was still searching for it – thanks for finding a solution two years ago ;-) Léonie.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • Rollo

    Thanks Matt for this useful tip. Really helped.

  • http://www.tavit-pr.co.il יחסי ציבור

    So very useful! thanks!

  • http://www.tavit-pr.co.il יחסי ציבור

    So very useful! thanks!

  • http://www.tavit-pr.co.il יחסי ציבור

    So very useful! thanks!

  • http://www.tavit-pr.co.il יחסי ציבור

    So very useful! thanks!

  • http://www.tavit-pr.co.il יחסי ציבור

    So very useful! thanks!