EDIT: As of 2012-06-11 this bug has been finally fixed! https://bugs.webkit.org/show_bug.cgi?id=35981#c1
I have some pretty straightforward markup:<
Sorry to post an answer to such an old thread, but there's actually a pretty easy solution for this that doesn't require any hacks. All you need to do is add padding to the top of your fieldset
element.
fieldset { padding: 10px 0 0; }
This might make what I'm trying to say a little more clear: http://jsfiddle.net/8fyvY/
I meet this problem and everything look fine on chrome but safari make the problem. In that case if I add this code
fieldset > legend:first-of-type
{
-webkit-margin-top-collapse: separate;
margin-bottom: 3px;
}
I get double margin on Chrome. Then just decide to do the following
fieldset > legend + *{
padding-top:3px;
}
Hope that help. Cheers!
After a bit of research I found a work-around for this that I believe to be the least "hacky" method for solving it. Using the nasty webkit targeting hacks really weren't an option, but I found that the -webkit-margin-collapse: separate
property seems to work in stopping the margins on the elements from collapsing just as it describes.
So in my scenario the following fixes the issue by adding a margin to the top of the first label element (right below the legend) in the fieldset:
fieldset > label:first-of-type
{
-webkit-margin-top-collapse: separate;
margin-top: 3px;
}
Not perfect, but better than nothing, other browsers should just collapse the margins normally.
If anyone is curious someone actually did file a bug report about this # 35981
https://bugs.webkit.org/show_bug.cgi?id=35981
Thanks for everyone's input.
Well, <legend>
really isn't "just another block-level element." Maybe it should be, but the fact is that it inherently is going to have layout peculiarities in that it's supposed to do something pretty weird, as elements go. Between IE and Firefox, the effects of margin and padding on <legend>
elements are a lot different.
Do you want to just separate <fieldset>
content from the top of the box? If so, I'd try playing with padding-top
of the fieldset itself.
Ive just found adding a 1px padding to the fieldset seems to make it suddenly aware of the margins it contains (the spacing created is more than 1 px).
To get a legend working with bottom border and margin in all browsers I insert a span inside the legend, put the border on the span, set the legend margin to 0 and add padding to the bottom of the legend.
e.g.
legend {
border: 0;
margin-bottom: 0;
padding-bottom: 20px;
}
legend span {
display: block;
border-bottom: 2px solid #f0ebe6;
}