简体   繁体   English

h1 标签中的两行

[英]Two lines in h1 tag

I need to fit two lines in one h1 tag (instead of making two separated h1 tags).我需要在一个 h1 标签中放入两行(而不是制作两个分开的 h1 标签)。

How can I create a line break inside of h1 tag?如何在 h1 标签内创建换行符?

使用:

<h1>Line 1 <br/> Line 2</h1>

W3C 验证的方法是

<h1>Line 1 <span style = "display: block;">Line 2</span></h1>

Summarizing all clever answers, this is what https://validator.w3.org says for each one:总结所有聪明的答案,这就是https://validator.w3.org对每个答案所说的:

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错误:在此上下文中不允许元素 p 作为元素 h1 的子元素


<h1>
  <div>line1</div>
  <div>line2</div>
</h1>

Reason:原因:

Error: Element div not allowed as child of element h1 in this context.错误:在此上下文中不允许元素 div 作为元素 h1 的子元素。


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>

您可以在h1插入标记,这样您就可以简单地执行<h1>foo<br>bar</h1>

Standard quote that br inside h1 is valid h1br有效的标准报价

Let's teach more people to read the current standard让我们教更多人阅读现行标准

4.3.6 "The h1, h2, h3, h4, h5, and h6 elements" says: 4.3.6 “h1、h2、h3、h4、h5 和 h6 元素”说:

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”的定义,这导致3.2.5.2.5“Phrasing content”它说:

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, ... ..., br, ..., 跨度, ...

so we see that br is in the huge list of phrasing content elements, and therefore can go inside h1 .所以我们看到br在巨大的短语内容元素列表中,因此可以进入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;然后使span display: inline-block; with CSS.使用 CSS。

You can use the line break tag您可以使用换行标记

<h1>heading1 <br> heading2</h1>

You can also do it with PHP你也可以用 PHP 来做

$str = "heading1 heading2 heading3";
foreach(explode(" ",$str) as $data)
{
 echo '<h1>' .  $data  . '</h1>' . '<br>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM