Sorry guys, I'd like to just confirm this, is it correct to have a p tag inside a form tag? Something like

<form action="demo_form.asp">
<p>This is a test.</p>
First name: <input type="text" name="FirstName" value="Mickey"><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
<input type="submit" value="Submit">
</form>

thanks

Dani AI

Generated

As and noted, putting a p element inside a form is valid HTML: the form element accepts flow content (which includes paragraphs), and a form must not contain another form. (MDN: form element). (developer.mozilla.org)

The p element itself takes phrasing content, and common form controls (like input) are phrasing content too, so simple fields can live inside a paragraph. Paragraphs will, however, automatically close if a block-level element (for example div or fieldset) is encountered; that can produce unintended DOM structure if a block element is placed inside a p. For accessible markup prefer using label for each control and fieldset/legend for grouped controls, and avoid using br or empty p tags for layout — use CSS spacing instead. (MDN: p element, MDN: label element). (developer.mozilla.org)

Minimal pattern that keeps semantics and accessibility clear:

<form>
  <fieldset>
    <legend>Contact</legend>
    <label for="fname">First name</label>
    <input id="fname" name="fname" type="text">
    <label for="lname">Last name</label>
    <input id="lname" name="lname" type="text">
    <button type="submit">Submit</button>
  </fieldset>
</form>

As suggested, running markup through an HTML validator is a good final check for nesting or omission issues.

Recommended Answers

All 4 Replies

Absolutely nothing wrong with this, the form tag shall only look for 'inputs' and shall ignore everything else.

Shouldn't be a problem. You can always use an online validated when you are unsure if you are using elements and attributes correctly.

http://validator.w3.org/

Hi,

Absolutely no wrong in this. U can darely use P tag inside form :-)

thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.