Getting Fieldset Backgrounds and Legends to Behave in IE

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.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

72 Comments

  1. Yannick

    Hey Matt,

    Thanks! I have had that problem with a few projects, but didn’t know how to solve it so invariably I would have to scrap using a background colour. Now at least I know how to get it to work right.

  2. Nando Vieira

    Great tip! Thanks a lot!

  3. BarelyBlogging » Blog Archive » links for 2006-04-14

    [...] Matt Heerema : Web Design » Getting Fieldset Backgrounds and Legends to Behave in IE (tags: css design html fieldset) [...]

  4. Getting Fieldset Backgrounds and Legends to Behave in IE · Style Grind

    [...] Matt Heerema has some useful tips for the “problem” that occurs when you are building a form with a fieldset tag and a legend tag, and then style the fieldset with a background color which ends up spilling outside of the fieldset. What to know how to fix it for IE? Head over and see what to do. [...]

  5. Mike P.

    Hi, thanks for the great fix!!! I was wondering if you could give some additional advice. I’m nesting Fieldset and Legend and when I do so the Fieldset text overlaps when I implement this fix unless I do a line break in the XHTML. It’s probably a simple margin fix, but I can’t seem to figure it out. I don’t want to use the because it creates too much room in FIrefox/Mozilla, etc.

    I’ve published a couple screens to show you what I’m running into. http://www.wvdhhr.org/bcf/temp_ieBugonFieldset.html

    Thanks again for the tip and for any advice you may give.

  6. That’s It, I Give Up (or Amazing Blog Designs) - Matt Heerema : Web Design

    [...] The main problem with events like CSS Reboot, et al, are that they serve as sort of a Total Perspective Vortex for me. It reminds me how far I have to go, and how much I have to learn. What keeps me from giving up is that I feel I have something to contribute to the noise. I just have to find my niche (solutions to strange CSS problems perhaps?) and write about it, constantly. [...]

  7. Jamie

    Another fix for this problem is to have a background image for the fieldset, 1 pixel wide and much taller than the fieldset will be. Does mean you have to change the image to change the colour though…

    Something along the lines of:

    • html fieldset { background: url(/img/backgrounds/fieldset.gif) repeat-x top; }

    fieldset { background: url(/img/backgrounds/fieldset.gif) repeat-x bottom; margin-bottom: 2em; border: #000066 solid 1px; padding: 1em; padding-top: 0.4em; }

  8. Chris Owens

    This doesn’t seem to work correctly in the current IE7 Beta.

  9. Gordon

    Actually the method does work in IE7. The Holly Hack however doesn’t. The method works fine if you put the above in a seperate stylesheet and load it with a conditional comment.

  10. Matt

    Thanks, Gordon, for pointing that out. I have not had much time to play with IE7. Should probably get on that.

  11. Kim Siever

    “Viola!”

    Personally, I prefer the violin. :)

  12. Matt

    Oh dear.

    So, my ignorance is showing. Isn’t it “víola”, a french word pronounced “Walla!”?

    Or am I way off.

  13. gnelf.de » links for 2006-07-07

    [...] Getting Fieldset Backgrounds and Legends to Behave in IE – Matt Heerema : Web Design (tags: css xhtml webdesign fieldset ie hack) [...]

  14. audrey

    voila – you have the i & o transposed.

  15. Matt

    HA… ya that. oops.

  16. Milosz

    OK. Solution is great but I have to insert 3 fieldsets one under one and now I have big trouble how to apply more or less the same margin between fieldsets in IE and Firefox. Ane ideas? Thanks.

  17. Rhodus

    Opera seems to put the inside the when doing this…

  18. epiphantastic » Blog Archive » Fieldset background IE bug

    [...] I was working on some CSS for a form and realized that applying a background to the fieldset didn’t really have the desired effect. The background flows out at the top of the fieldset in IE (what a surprise!). Luckily, I found a solution to the problem on Matt Heerema’s blog. Works like a charm! [...]

  19. Wild & Wacky

    I think that this answers my prayers – but, after playing around, I think it is missing something!! What I want/need is a box around the ‘tab heading’. And for the whole of this new box to have a coloured background (like the ‘main’ box. Anyone know the cure?

    Please!!!

  20. Hi, my name is Al » Ice to see you..

    [...] As most of you know, I have some kind of shoulder problem. I started seeing a physio November last year about it and have been having treatment since. A few weeks ago I hurt it at Kung Fu, and have been going easy since. Little did I know I had re-wrecked it. Its again out of commission for a month, so no swinging punches. Which is basically what Choy Lay Fut is all about. So ill have to think of something. Lucky for me, Bando has the thumbs up, due to its striking being very tight quick movements. So today, we did sparring. And I seem to be having trouble walking. Now its not every day I get hammered…. actually… I seem to get hurt a lot now I think about it… Hmmm… where was i going with this again…. oh yeah, I tend to not bruise very easily at all. If I do usually get them, they aer usually a few days later and kind of light. However… I got home no more than 3 hours ago and my shins are already getting darker. Good times. I seem to have a slight one appearing on my upper thigh after getting knocked on my ass by Mr Kroll. My only rehab at the moment is after any training, I have to ice the shoulder for ~10 mins. And it gets cold…. DAMN cold…. Anyway, on a less violent note, im starting to prepare for IE7. Not really getting excited, more reading up on how I will have to have the bastard to display CSS properly.. I have been doing a few more Accessible forms as of late and had just found a fix for fieldset background spillage in IE. From what I have read so far in preliminary preports and whatnot all hacks should work, and CSS should render better than before. Of course, this poses a problem. Do we change doing CSS to incorporate the fixes in this version, or would you still allow for IE6 users, making any updates completely irrelevant? Hmmm… I shalll ponder the conundrum over a bottle of water… Also, the Jets are on tomorrow at the Inglewood. Im quite tempted to go… Actually, im quite sure I will. [...]

  21. ant

    Thanks, I’ve been looking for something like this for years!

    One thing, I noticed the background still sticks out the top a bit. You can fix that by doing “legend { margin-top: -[width of fieldset border] }”

  22. lotek

    fieldset the same in FireFox IE7 Opera

    This code is use in PHP but you can do the same in javascript :p

    fieldset { position: relative; border: 1px solid #090; margin: none; padding: 0px 10px 10px; background: #eee; -moz-border-radius: 10px; }

    legend {

    top: -0.5em;
    margin-left: 10px;
    padding: 0px 10px;
    background: #9cf;
    font-weight : bold;
        border: 1px solid #090;
        -moz-border-radius: 10px;
    

    }

  23. lotek

    the website clean my php code …

    fieldset the same in FireFox IE7 Opera

    This code is use in PHP but you can do the same in javascript :p

    fieldset { position: relative; border: 1px solid #090; margin: none; padding: 0px 10px 10px; background: #eee; -moz-border-radius: 10px; }

    legend {

    top: -0.5em;
    margin-left: 10px;
    padding: 0px 10px;
    background: #9cf;
    font-weight : bold;
        border: 1px solid #090;
        -moz-border-radius: 10px;
    

    }

  24. lotek

    fieldset the same in FireFox IE7 Opera

    This code is use in PHP but you can do the same in javascript :p

    fieldset { position: relative; border: 1px solid #090; margin: none; padding: 0px 10px 10px; background: #eee; -moz-border-radius: 10px; }

    legend { (?php if( !eregi( 'opera', $_SERVER[ 'HTTP_USER_AGENT' ])) echo 'position: relative;'; ?) top: -0.5em; margin-left: 10px; padding: 0px 10px; background: #9cf; font-weight : bold; border: 1px solid #090; -moz-border-radius: 10px; }

  25. links for 2006-10-18 at willkoca

    [...] Getting Fieldset Backgrounds and Legends to Behave in IE – Matt Heerema : Web Design (tags: css webdesign) [...]

  26. Dustin Brewer

    For the person asking about multiple fields. I ran into the same trouble. Messing with the margins wasn’t getting me anywhere. I noticed someone said a br tag worked, but created too much space in FF/Opera. Well, if you use conditional comments it works beautifully.

    Hopefully this comes through correctly (remove the spaces).

  27. jonah

    … still doesn’t work in IE7

  28. jonah carneskog

    I got it to work in IE7 by adding a margin-top:-5px; on the legend.

    My stylesheet now looks like this. fieldset { position: relative; border: 1px solid #fff; padding: 0px 10px 10px; background: url(../images/semi.png); width:870px; margin: 20px auto; }

    legend{ margin-top:-5px; top: -0.5em; margin-left: 10px; padding: 0px 10px; background: #000; font-weight : bold; border: 1px solid #fff; -moz-border-radius: 10px; }

    Works like a charm in FF and IE7.

  29. Mike Dowson

    this method seems to work in ff2 and ie7, although problems do occur if the user resizes the window.

    form fieldset legend { background-color: #820027; padding: 8px; color:#FFFFFF; position: relative; top: -20px; display: block; margin-left: 0px; margin-right: -8px; margin-top: 5px;

    }

    form fieldset { border: 1px solid #820027; background-color: #EFECE2;

    }

  30. Cult of Secrets » Gigantus Intro Post

    [...] mattheerema.com – Yet another interesting Blog entry I stumbled onto when researching HTML I think. This entry covers some interesting issues and behaviors of using the legend command in html on Internet Explorer. [...]

  31. margin, fieldset und border - XHTMLforum

    [...] Zitat von Despairing 2. In meinem Gstebuch wird im Internet Explorer-Browser jedes Fieldset fehlerhaft dargestellt. Die Hintergrundfarbe des Fieldsets wird ein paar Pixel ber der Fieldsetbox dargestellt, was nicht so sein sollte. Wie kann ich das unterbinden? Hatte ich letzhin gerade auch. Lsung siehe Getting Fieldset Backgrounds and Legends to Behave in IE – Matt Heerema : Web Design __________________ The only thing to do with good advice is pass it on. It is never any use to oneself. – Oscar Wilde – #me { position: absolute; } [...]

  32. ekashkash

    Awsome.. It worked perfectly.

    Thank you very much!

  33. Frank Topel

    I got it to work in both IE 6 and 7. Problem remains, that IE shows the legend text in blue color, even though I have

    legend { color: #666; }

    in my css.

    Any ideas?

    P.S.:

    fieldset { color: #666; }

    and

    body { color: #666; }

    are also set.

  34. Frank Topel

    The above problem occurs only in IE 7 Final.

  35. Maxim Micheliov

    Thanks for a brilliant solution. Me too found that this method applied “as it is” doesn’t bring the desired result in IE7. However there seem to be another way around the problem – to put the above css rules (by Matt Heerema)into an IE condition comment.

    fieldset { position:relative }

    legend { position:relative; top:-0.7em; margin-bottom:-0.7em; }

    Usually I have such CSS pocket to store all the IE specific hacks in one place. Why not to drop there the discussed rules?

  36. Maxim Micheliov

    Ups the form didn’t let me put the complete code…

    fieldset { position:relative }

    legend { position:relative; top:-0.7em; margin-bottom:-0.7em; }

  37. Maxim Micheliov

    My sorry to a moderator, just want to share a bit of wisdom. Please fit it somehow into the comments if think it is useful. The rules can be placed inside if IE condition statement.

  38. Jon

    thanks for this! it worked perfectly!

  39. sandro

    Thanks for the fix, came straight from google and was happy to find simple well formed code with clear instructions. Perfect!

  40. Quinti

    Buahs

    thank u very much for this post, i’m trying aaaaall the css things to change the color legend in IE,

    f….. IE!!! i hate it!

    greetingss

  41. SalsaShark

    Found one problem with this method. In Safari, it draws the border all the way around the fieldset. Legend text is placed above the border, but still a bit of a problem.

  42. Paul

    Thanks to all! I was pulling my hair out to get this to work.

  43. vivian hua

    thanks for this is quite the frustrating problem!!!

  44. Formular in FF top .. IE flop :( - CSS @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe

    [...] Test auch wirklich sehr gut funktioniert. Ich muss es nur noch auf mein Beispiel anwenden. Im Blog von Matt Heerema lsst sich dieses finden. Gru soyo __________________ Aktuelle Zertifizierung: MCP 70-270 In [...]

  45. Giles

    This is an awesome hack but as ever, IE manages to mess it up however hard you try: I have a fieldset with a semi-transparent PNG background. so:

    .my_fieldset { background:transparent; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='images/bg/1px_gray_20pc.png'); }

    With this kind of CSS PNG background, IE decides to crop the legend at the top of the fieldset. If you change the background to a normal background:#f00; type declaration, the legend displays as normal.

  46. Web 2.0 Announcer

    Getting Fieldset Backgrounds and Legends to Behave in IE…

    [...]The title says it all. Very useful![...]…

  47. Flick

    Thanks! :) This worked great!

  48. IE must die

    Regarding the comment by Giles, this problem also occurs when using a transparent PNG sans the MS filter hack. So, even if you support IE 7 only, you are screwed by IE once again.

    That said, I cannot get any of the posted methods to work in IE 6 or 7. I still get a few pixels of bleed out the top. I stripped the styles down to nothing first to be sure the css applied to real browsers in my first style sheet were not causing issue. I’m glad others have had success, but I can no longer waste time on IE and have decided to display it plain and ugly. Sad, because that’s what most users of my app use( corporate customers ). They’ll never see the great design I had…

  49. Darren Emory

    Nice tutorial. Fixed a stupid issue for me! Thanks

  50. Joel

    Thanks. Fixed a big problem for me.

  51. kralloyun

    thank you.

  52. Jesse

    A w e s o m e !

  53. bob

    THANK YOU FOR THIS!!

  54. Mega Awesome CSS Resource List! | WebDevLounge | design, development, SEO and wordpress | articles, discussion and community

    [...] Getting Fieldset Backgrounds and Legends to Behave in IE I’m sure someone has posted on this issue before I just haven’t found an answer I’m looking [...]

  55. Paolo

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

  56. 40 CSS Web Form Style Tutorials For Web Developers | Social CMS Buzz

    [...] Getting Fieldset Backgrounds and Legends to Behave in IE I’m sure someone has posted on this issue before I just haven’t found an answer I’m looking [...]

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

    [...] mattheerema adresinde gördüğüm faydalı bir makale ekliyorum. Sayfalarınızda Fieldset ve Legend takılarını kullanırken eğer fieldset takısına zemin rengi atarsanız IE tarayıcılarda legend takısına bu zemin renginin taşdığını görüyoruz. Aşağıdaki küçük CSS eklemeleri ile bu sorundan kurtulabiliyoruz. [...]

  58. jpemberthy

    tysir!

  59. MP145

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

  60. asdfasdf

    asdfsdaf

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

  62. Rollo

    Thanks Matt for this useful tip. Really helped.

  63. יחסי ציבור

    So very useful! thanks!

  64. fgfgfg

    nhgbghb

  65. 剑侠世界资料站

    不错不错Getting Fieldset Backgrounds and Legends to Behave in IE – Matt …

  66. WEB 3.0 » Blog Archive » ¿Así que querías saber de CSS?

    [...] Getting Fieldset Backgrounds and Legends to Behave in IE – Matt Heerem [...]

  67. nicolas

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

    IE sucks, and it ever will.

  68. hjgj

    gfhfghg

  69. יחסי ציבור

    Finally a solution!!

  70. Web Design Chester

    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

  71. Sedat Kumcu

    Thanks for this article. Best regards man.

  72. 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; }

Leave a Comment

Digital Designer, Developer, & Strategist

matt heerema

Matt has been creating Web sites for a decade, playing nearly every role on a Web team at least once. A love for art, a knack for code, and a love for people have fueled his career.

Matt is the Sr. Web Manager for Desiring God Ministries. More importantly, he is a Husband, Father, Musician, and Jesus' friend.

Matt writes about technology (of all kinds), productivity, and sometimes theology. Read more about Matt here.