Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my legend respect the padding of the parent fieldset?

Tags:

html

css

Look at this:

enter image description here

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

like image 297
Adam Zerner Avatar asked Sep 18 '15 22:09

Adam Zerner


People also ask

What does a legend define for a Fieldset?

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.

Can I use legend without Fieldset?

You should not use the <fieldset> and <legend> when: You have a single form field that asks for a single piece of information.

Can a Fieldset have multiple legends?

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.

What is legend in Fieldset in HTML?

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.


1 Answers

Because the Rendering section of the HTML5 spec says so

10.3.13 The fieldset and legend elements

If the fieldset element has a child that matches the conditions in the list below, then the first such child is the fieldset 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 the fieldset element as a 'block' box (overriding any explicit 'display' value).

like image 150
Oriol Avatar answered Sep 19 '22 08:09

Oriol