Are divs and spans allowed inside form elements?

2019-06-15 07:26发布

问题:

I am asking this question to test the validity of my HTML. I can very well try this out (and I have, and it's possible), but I'm simply curious whether or not this is allowed in HTML. If not, how can one simulate a div or span element inside a form? Using blockquote?

回答1:

form is a block-level element in HTML. Typically, block-level elements allow both block-level and inline children. Both div and span are valid children of form.

There are a ton of resources online to learn more about this topic, for example:

http://www.w3.org/TR/html4/struct/global.html#h-7.5.3

It may also benefit you to read about the box model, as this is one of the most fundamental concepts of web design/development.

http://www.w3.org/TR/CSS2/box.html



回答2:

Yes, you can. And it is also "officially allowed" by the XHTML standard, if you look into the XHTML XSD, you will find

<xs:complexType name="form.content">
  <xs:annotation>
    <xs:documentation>
      form uses "Block" excluding form
    </xs:documentation>
  </xs:annotation>
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:group ref="block"/>
    <xs:group ref="misc"/>
  </xs:choice>
</xs:complexType>

"block" encompasses div and "misc" contains span. The "documentation" part points out one particular thing you are not allowed to do: nest a form within another one.



回答3:

Yes it's valid and you can use any number of divs, spans or blockquotes inside a form. You can always use W3C Markup Validation Service to check your html.

Eg:

<body>
    <form id="Form1">
    <div id="wrap">
         <div id="content-wrap" class="content-wrap-admin">
         </div>
    </div>
    </form>
</body>


回答4:

I must correct emboss' answer.

In the XHTML 1.0 Strict DTD that he quotes, the group misc does not refer to inline elements. Instead, it refers to the following 4 elements: noscript, ins, del and script.

<!ENTITY % misc.inline "ins | del | script">
<!ENTITY % misc "noscript | %misc.inline;">

So to answer the question, XHTML 1.0 Strict does not allow span elements inside form elements. You'll need to wrap them inside block elements such as p, dip or fieldset.

This is not the case with XHTML 1.0 Transitional, though. Indeed, the DTD indicates that inline elements are allowed inside form elements:

<!ENTITY % form.content "(#PCDATA | %block; | %inline; | %misc;)*">

For reference: XHTML 1.0 - DTDs



回答5:

Yes.

Did you even try this yourself?



标签: forms html