开发者

Are divs and spans allowed inside form elements?

I am asking this question to test the validity of my HTML. I can very w开发者_如何学Cell 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?


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


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.


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>


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


Yes.

Did you even try this yourself?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜