I use this in visual studio but the compiler claimed that h1
cannot be nested in legend
element, but browser can render it anyway so i am confused that this is valid or not?
<fieldset>
<legend>
<h1>Caption</h1>
</legend>
</fieldset>
I use this in visual studio but the compiler claimed that h1
cannot be nested in legend
element, but browser can render it anyway so i am confused that this is valid or not?
<fieldset>
<legend>
<h1>Caption</h1>
</legend>
</fieldset>
In HTML5 elements allowed inside
<legend>
elements are those in the Phrasing Content group. Per the docs:In HTML5, you can use an
<h1>
for the same purpose a legend serves in a fieldset, since a fieldset is considered a sectioning root. So if you want to be free from the legend's wonky styling quirks, use this accessible markup:There are many things that browsers do that aren't required by the standards :-)
This page here (HTML4) specifies the
legend
keyword and states that it can contain inline elements, of which the headers are not a part. It does have other possibly useful ones such asbig
orstrong
.In addition, the relevant page for H1 does not list
legend
as one of the items it's allowed to be contained within. You may also be able to use theid
,class
orstyle
attributes of the legend to set the underlying textual properties.no it is not valid inside the legend. you can use wc3 validator to validate your code
The legend element of the fieldset is not designed to allow child elements unless they are considered "phrasing content". While most browsers will not complain (thus making it valid in pratice) you would probably be better off using a style to set the look/feel of your legend element as that is what css is for, elements should be used for logically grouping/identifing content not styling it.
if you check the html standards it says:
The
%inline
part means that it should only contain character level elements and text strings. So while it does render, you should not use it this way because the html is actually not valid.