Can we add a <span> inside an <h1> tag?

Html

Html Problem Overview


Is it a proper method to use a <span> tag inside an <h1> tag?

<h1>
    <span class="boardit">Portfolio</span>
</h1>

I know we can write it in this way...and am also following the below syntax in my own website..

<h1 class="boardit">
  <span>Portfolio</span>
</h1>

However, I Just wanted to know the cleaner form of html..

Html Solutions


Solution 1 - Html

Yes you can.

HTML4 has this to say:

<!ENTITY % heading "H1|H2|H3|H4|H5|H6">
<!--
  There are six levels of headings from H1 (the most important)
  to H6 (the least important).
-->

<!ELEMENT (%heading;)  - - (%inline;)* -- heading -->

And %inline; is:

<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">

And %special; includes <span>.

The current HTML has this to say:

> Content contents
> Phrasing content

And Phrasing content includes <span>.

Solution 2 - Html

Yes you can. It can be used to format a part of a h1 block:

<h1>Page <span class="highlight">Title</span></h1>

If the style applies to the entire h1 block, I do this:

<h1 class="highlight">Page Title</h1>

Solution 3 - Html

Yes, it's typically fine to use a span inside of an h1. span is an inline element, so it's typically okay to use it inside anything (that allows elements inside it!)

And there's not really a cleaner way to do it sometimes, say if you want to style only part of the h1.

On the other hand... don't do it if it's not necessary, as it is a little ugly : )

Solution 4 - Html

Yes that's fine, but why not

   <h1 class="boardit">
      Portfolio
   </h1>

If thats all you're doing?

Solution 5 - Html

Yes, you can. The span displays inline, so it should not affect the styling of the H1.

Solution 6 - Html

Yes, we can use span tag with header tags and there is nothing wrong in it. Indeed this is widely used for styling header tags, specially for coloring a particular word or letter.

Solution 7 - Html

Yes, we can use span tag with header tags and there is nothing wrong in it. Indeed this is widely used for styling header tags, specially for coloring a particular word or letter.

Solution 8 - Html

<h1 style="display:inline;">Bold text goes here</h1> 
<span style="display:inline;">normal text goes here</span>

Think in above lines - Worked for me - use display:inline prop

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodeobsessionView Question on Stackoverflow
Solution 1 - Htmlmu is too shortView Answer on Stackoverflow
Solution 2 - HtmlmnsrView Answer on Stackoverflow
Solution 3 - HtmlDaveView Answer on Stackoverflow
Solution 4 - HtmlSimon SarrisView Answer on Stackoverflow
Solution 5 - HtmlgeetfunView Answer on Stackoverflow
Solution 6 - HtmlTouhid RahmanView Answer on Stackoverflow
Solution 7 - HtmlTouhid RahmanView Answer on Stackoverflow
Solution 8 - HtmltakrishnaView Answer on Stackoverflow