Two lines in h1 tag
HtmlCssHtml Problem Overview
I need to fit two lines in one h1 tag (instead of making two separated h1 tags).
How can I create a line break inside of h1 tag?
Html Solutions
Solution 1 - Html
Using:
<h1>Line 1 <br/> Line 2</h1>
Solution 2 - Html
A W3C validated method is
<h1>Line 1 <span style = "display: block;">Line 2</span></h1>
Solution 3 - Html
Summarizing all clever answers, this is what https://validator.w3.org says for each one:
Validated:
<h1>Line 1 <br/> Line 2</h1>
<h1>Line 1<br>Line 2</h1>
<h1>Line 1 <span style = "display: block;">Line 2</span></h1>
Invalid
<h1>
<p>Line1</p>
<p>Line2</p>
</h1>
Reason:
> Error: Element p not allowed as child of element h1 in this context
<h1>
<div>line1</div>
<div>line2</div>
</h1>
Reason:
> Error: Element div not allowed as child of element h1 in this context.
Tested code:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>Line 1 <br/> Line 2</h1>
<h1>Line 1<br>Line 2</h1>
<h1>
<p>Line1</p>
<p>Line2</p>
</h1>
<h1>Line 1 <span style = "display: block;">Line 2</span></h1>
<h1>
<div>line1</div>
<div>line2</div>
</h1>
</body>
</html>
Solution 4 - Html
You can insert markup inside h1
, so that you can simply do <h1>foo<br>bar</h1>
.
Solution 5 - Html
Standard quote that br
inside h1
is valid
Let's teach more people to read the current standard
4.3.6 "The h1, h2, h3, h4, h5, and h6 elements" says:
> Content model: Phrasing content.
Then we click the definition of "Phrasing content", which leads to 3.2.5.2.5 "Phrasing content" which says:
> Phrasing content is the text of the document, as well as elements that mark up that text at the intra-paragraph level. Runs of phrasing content form paragraphs. > > ..., br, ..., span, ...
so we see that br
is in the huge list of phrasing content elements, and therefore can go inside h1
.
This also shows us that another option would be to do something like:
<h1><span>ab</span><span>cd</span></h1>
and then make the span
be display: inline-block;
with CSS.
Solution 6 - Html
You can use the line break tag
<h1>heading1 <br> heading2</h1>
You can also do it with PHP
$str = "heading1 heading2 heading3";
foreach(explode(" ",$str) as $data)
{
echo '<h1>' . $data . '</h1>' . '<br>';
}