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.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!

  • 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!

  • http://www.222tl.cn/ 剑侠世界资料站
  • Pingback: WEB 3.0 » Blog Archive » ¿Así que querías saber de CSS?

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • nicolas

    Damn! I was having yhe very same problem! Good I quickly found this page. Thank you!

    IE sucks, and it ever will.

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • hjgj

    gfhfghg

  • http://www.abercromby.eu John

    Have to agree, IE always causes pain at the wrong times!!! It’s better than it used to be though to be fair!

    Cheers, John

  • Pingback: Ted Roche’s weblog » Blog Archive » Semantic markup and styling of data entry forms, IE falls short

  • Sedat Kumcu

    Thanks for this article. Best regards man.

  • thetooman

    It appears the iE8 has some weird different spacing. My ugly solution was contitional commenting thusly:

    fieldset legend { position: absolute; /* all ie browsers except .. */ top: -0.8em; left: 0.7em;

    /* … ie6,7 http://paulirish.com/2009/browser-specific-css-ha… */ *top: -0.5em; *left: 0.2em; }

  • pnanh

    thanks yours tip

  • http://www.splinelab.de Mirko

    Thanks for the nice trick!

  • Fentex

    The fundemental problem with fieldset legends is that they can’t be rendered using CSS to style them.

    The way legends appear for fieldsets requires displaying partial transparencies just not supported by any CSS standard (they have to be transparent, but not to borders).

    Thus each browser manufacturer has created their own solution to how to display fieldsets with legends.

    The method here generally works but fails when certain borders are specified because IE still leaks a borders width above the top border.

    The way to fix that problem is to not give the fieldset a top border and use a x repeating background image in it’s stead.

    • http://www.namaroundtheworld.net NaM – The French frog x)

      Thanks a lot for both tricks! As mentionned by Fentex, after have applied the first trick, another problem was still there : the top-border of the fieldset that overflows to the right. I simply deleted the top border and thanks to the background applied to my fieldset i don’t need to add any other trick!! This is just perfect! Thanks so much x)

  • Pingback: A CSS Hack for Internet Explorer 8 at From the Code Mill

  • http://atompad.com John Griffiths

    mine was a bit more intense, ended up using position:static to fix it.

  • לימודי הנדסת מכונות

    Finally a solution!! Thanks!

  • chanteur06

    Wonderful ! TKS a lot!