Look at this:
The <fieldset>
has a padding of 50px
on top. <legend>
doesn't respect this, but <p>
does. Why is this?
Note: I'm using Bootstrap in the picture below and in my Code Pen, but the question applies regardless of whether Bootstrap is used.
HTML
<div class='placeholder'></div>
<fieldset>
<legend>LEGEND</legend>
<p>PARAGRAPH</p>
</fieldset>
CSS
.placeholder {
height: 100px;
background-color: red;
}
fieldset {
padding-top: 50px;
}
Code Pen
The LEGEND element defines a caption for form controls grouped by the FIELDSET element. The LEGEND element must be at the start of a FIELDSET, before any other elements. While the LEGEND element is not supported by old browsers, it can still be used safely if a block-level element immediately follows the LEGEND.
You should not use the <fieldset> and <legend> when: You have a single form field that asks for a single piece of information.
Every fieldset element must contain exactly one legend element. Multiple legend elements contained in the same fieldset may result in the improper calculation of labels for assistive technologies.
Definition and Usage The <fieldset> tag is used to group related elements in a form. The <fieldset> tag draws a box around the related elements.
Because the Rendering section of the HTML5 spec says so
10.3.13 The
fieldset
andlegend
elementsIf the
fieldset
element has a child that matches the conditions in the list below, then the first such child is thefieldset
element's rendered legend:
- The child is a
legend
element.- The child is not out-of-flow (e.g. not absolutely positioned or floated).
- The child is generating a box (e.g. it is not 'display:none').
A
fieldset
element's rendered legend, if any, is expected to be rendered over the top border edge of thefieldset
element as a 'block' box (overriding any explicit 'display' value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With