Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to have paragraph elements inside of a heading tag in HTML5 (P inside H1)?

Tags:

Is it ok to have paragraph elements inside of a header tags in HTML5?

In other words: is this markup correct in HTML5? What are the drawbacks of using this?

<h1>     <p class="major">Major part</p>     <p class="minor">Minor part</p> </h1> 

If not, how can I style this elements with CSS correctly?

like image 816
Slava Fomin II Avatar asked Nov 04 '13 23:11

Slava Fomin II


People also ask

Can you put P inside H1?

vgarcia: H1 is a block element like P, but H1 and P can only have inline content inside of them, unlike other block elements like forms and divs. Therefore, you can't nest headings inside paragraphs (and if you think about it a heading really should never be in a paragraph).

Can we have a tag inside p tag in HTML?

HTML tags do not always need to be inside of a p tag. 'P' is for 'paragraph', so if you want to have a link, icon, etc inside a text paragraph, then those tags would go inside the p tag.

Can we use P tag inside a?

Inside a tag can be tags with default display: inline or inline-block . tags like span , em , strong etc. You can change your p to span with some class and write some styles in CSS for this class. P.S.: You can't use a inside a .

How do you put a paragraph under a header in HTML?

If you want to explain something on your web you use a paragraph. For a paragraph we use a <p> tag. Note: <h1> to <h6> and <p> add by default one line break. And <br> is used for a line break.

What can you put inside a header tag in HTML5?

In other words you can use the following convenient elements inside of a header tag in HTML5: a, em, strong, code, cite, span, br, img. See the full list here. The W3C validator will give you the following error if you will try to validate this markup: Element p not allowed as child of element h1 in this context.

Can header tags be inside <P> tags?

The <p> tag can only contain inline elements. The header tags are block-level elements, and cannot go inside <p> tags even when you style them to display inline. Show activity on this post. This is basic HTML (or any other markup language).

Is it possible to use a span tag inside a header?

As stated before, span tag is perfectly valid inside of a header elements (h1-h6). And you can apply " display: block; " style to it to make it render as a block level element (each on a different line). It will save you a br tag. Of course you will need to change this CSS selectors according to your use case.

What is the correct content for Header elements in HTML?

It is stated that the correct content for header elements is a " phrasing content ", which is phrasing elements intermixed with normal character data. In other words you can use the following convenient elements inside of a header tag in HTML5: a, em, strong, code, cite, span, br, img. See the full list here.


1 Answers

Actually, no. This markup is not correct according to W3C. You should avoid using this.

It is stated that the correct content for header elements is a "phrasing content", which is phrasing elements intermixed with normal character data.

In other words you can use the following convenient elements inside of a header tag in HTML5: a, em, strong, code, cite, span, br, img. See the full list here.

The W3C validator will give you the following error if you will try to validate this markup: Element p not allowed as child of element h1 in this context.

The one major drawback of using this markup that you should consider is that Search Engines can incorrectly parse your heading tag and miss important data. So this practice can be bad for SEO.

If you would like a better SEO results it is a good practice to include only textual data inside of a heading elements. But, if you also need to apply some styles, you can use the following markup and CSS:

<h1>     <span class="major">Major part</span>     <span class="minor">Minor part</span> </h1>  <style type="text/css">     h1 span {         display: block;     }     h1 span.major {         font-size: 50px;         font-weight: bold;     }     h1 span.minor {         font-size: 30px;         font-style: italic;     } </style> 

See the jsfiddle for this.

As stated before, span tag is perfectly valid inside of a header elements (h1-h6). And you can apply "display: block;" style to it to make it render as a block level element (each on a different line). It will save you a br tag.

Of course you will need to change this CSS selectors according to your use case.

And yes, as @stUrb said it's not semantically correct to use paragraphs inside of a headings. The most important idea behind HTML is that it must be a semantics first, presentation later.

like image 199
Slava Fomin II Avatar answered Oct 13 '22 16:10

Slava Fomin II