Home > Article > Web Front-end > Is it Valid to Nest Paragraph Elements Inside Heading Tags in HTML5?
Question:
Is it valid to use paragraph (P) elements within heading (H1) tags in HTML5?
Answer:
No, it is not valid according to W3C guidelines. Header elements should contain only "phrasing content," which includes elements like A, EM, and SPAN.
Drawbacks of Using Nested Paragraphs:
Alternative Styling Options:
If you need to style elements within headings, use valid elements like SPAN instead. You can then apply CSS styles to these elements as follows:
<h1> <span class="major">Major part</span> <span class="minor">Minor part</span> </h1>
h1 span { display: block; } h1 span.major { font-size: 50px; font-weight: bold; } h1 span.minor { font-size: 30px; font-style: italic; }
This approach uses semantic markup while allowing you to style elements as desired. Remember to adjust the CSS selectors as needed for your specific use case.
The above is the detailed content of Is it Valid to Nest Paragraph Elements Inside Heading Tags in HTML5?. For more information, please follow other related articles on the PHP Chinese website!