Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

p tag in button tag not allowed?

Tags:

html

I have a <button> element and within it, a <p> element. The <p> element is used in combination with css, margin-top, in a class to vertical align the text within the button (the button has a specific height).

The html looks like this:

<button class="someClass">
    <img ... />
    <p class="anotherClass">Caption</p>
</button>

This works fine, the text is vertically aligned like it should be. However I get a warning inside visual studio 2012 saying:

Element 'p' cannot be nested inside element 'button'.


My questions: why isn't the <p> element allowed inside a <button> element? And what is the alternative?

like image 449
Cornelis Avatar asked Sep 23 '14 18:09

Cornelis


People also ask

Can I use P tag inside a tag?

This is not allowed. An <a> element may not be a descendant of another <a> element.

How do I add a button to my P tag?

Instead, you only need to specify P tag to become inline-block element, so that button gets to its side. just like this “ <p style="display:inline-block; ”>" , cheers. Done.

Can I have UL inside P?

The short answer is that ol elements are not legally allowed inside p elements.

Can we use P tag inside Li?

For semantic purposes, content within the li element should be wrapped in a p tag. If there is more than one paragraph or section in a li , do not close the li until after the last element. This differs from the current standard, but is cleaner for accessible content.


1 Answers

That is correct, it isn't allowed, because;

  • The content model of button element is phrasing content (with no interactive content descendant).
  • And the p can only be used where flow content is expected.

An alternative is getting rid of the p element, and instead using a span element with display: block:

.anotherClass {
  display: block;
}
<button class="someClass">
  <img ... />
  <span class="anotherClass">Caption</span>
</button>
like image 144
Oriol Avatar answered Sep 17 '22 13:09

Oriol