The Problem: the background-color set for fieldset colours in more than just what’s inside the boundaries of the fieldset, i.e. its borders. What happens is it colours in from the top of the legend to the bottom of the fieldset.
What can I do to make it only colour what’s inside the boundaries (borders) of the fieldset? I tried putting a <span> inside the fieldset, setting it as block and giving it a background colour, but it didn’t colour the part where the legend overlaps the fieldset.
Have you thought of the fact the fieldset might have its own specific boundaries, which go a bit further then just wrapping whats inside, just like form tags.
You can try setting the margin and padding to 0 for the fieldset and see how it goes.
Yep the fieldset has these irritating boundaries where it goes well above the actual top of the fieldset itself, it includes the legend.
I did a bit of research on the matter, and it seems that this is a problem with no solution at the moment, so I think i’ll just leave it for now, it’s not too much of a problem anyway.
If you are just using the fieldset to make borders and not using it for accessibility reasons around forms etc then you could just revert to basic css and use something like this.
I will be using the fieldset for forms, but also in some places, without forms, but I’ve found that in the context of my design, having the background be the same as its parent’s background seems to make it fit in better; thanks for the link however, as I’m sure it will come in handy for other things :).
Hi,
Thanks midnight - now why didn’t I think of that doh! I did exactly the same thing in my demo above but used other elements.
I just tested it out and it works fine in firefox and ie6 but opera needs another div around the fieldset otherwise it places the legend at the top of the screen.
in the css for the legend tag… without that IE was placing the legend on the top right side of the legend in my browser. Great hack though, saved me a lot of time, thanks
fieldset is a block element. so adjust its boundaris.give margin of legend a negative value & made padding-top of fieldset none!
fieldset{
padding-top:0px;
background-color: black;
color: white;
}
legend {
padding: 2px;
border: 1px solid green;
}
*+html legend
{
margin:-8px 0 0 0;
}
It will work
I’ve used the span-inside-the-legend trick… while I’ve been able to positioned the legend completely offscreen using pos: absolute and both a left: -9999em for regular browsers (incl IE) and margin-left: -9999em for Gecko, when positioning on the page somewhere, I’ve needed the span to not hack so much for IE then. I can set the fieldset to pos: rel and the span inside the legend to pos: abso, and this has worked out (maybe because IE has trouble positioning someone who’s nearest positioned ancestor isn’t a direct parent but a grandparent or whatever). I don’t like the extra markup though, but it works.