加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

用于渲染新行的HTML规范?

发布时间:2020-12-14 19:35:29 所属栏目:资源 来源:网络整理
导读:我正在尝试将一些简单的 HTML文档(大部分包含div和br标签)呈现为纯文本,但是我正在努力添加新行.我认为这将是非常简单的 div和 br /生成新行,但它似乎有各种微妙的规则.例如: divone line/divdivtwo lines/divhr/divone line/divdiv/divdivstill two lines
我正在尝试将一些简单的 HTML文档(大部分包含div和br标签)呈现为纯文本,但是我正在努力添加新行.我认为这将是非常简单的< div>和< br />生成新行,但它似乎有各种微妙的规则.例如:
<div>one line</div>
<div>two lines</div>

<hr/>

<div>one line</div>
<div></div>
<div>still two lines because the empty div doesn't count</div>

<hr/>

<div>one line<br/></div>
<div></div>
<div>still two lines because the br tag is ignored</div>

<hr/>

<div>one line<br/></div>
<div><br/></div>
<div>three lines this time because the second br tag is not ignored</div>

<hr/>

<div><div>Wrapped tags generate only one new line<br/></div></div>
<div><br/></div>
<div>three lines this time because the second br tag is not ignored</div>

所以我正在寻找一个关于如何在HTML文档中渲染新行的规范(当不应用CSS时).任何想法,我可以找到这种文件?

解决方法

如果您正在寻找< div>的规范和< br>
你不会在一个地方找到它,因为他们都遵循不同的规则.
DIV元素遵循块格式规则,而BR元素遵循文本流规则.

我相信你的混乱的原因是他们遵循相同的新规则的假设.
让我解释.

BR元素.

BR在HTML4 Specification Section 9.3中定义关于行和段落:

The BR element forcibly breaks (ends) the current line of text.

并在HTML5 Specification Section 4.5年关于文本级语义:

The <br> element represents a line break.

规范说明了第三个例子:

<div>one line<br/></div>
<div></div>
<div>still two lines because the br tag is ignored</div>

在那里,BR元素根本不被忽略,因为它标记该线必须在那一点被破坏.
换句话说,它标志着当前文本行的结束.
这不是关于创建新行.

在你的第四个例子中:

<div>one line<br/></div>
<div><br/></div>
<div>three lines this time because the second br tag is not ignored</div>

BR元素也标志着该行的结束.
由于该行具有零字符,因此将其呈现为空行.

因此,在第三和第四个例子中,规则是一样的.
没有任何东西被忽略.

DIV元素.

在没有显式样式表的情况下,应用默认样式.
DIV元素是默认的块级元素,这意味着它遵循块格式化上下文
定义在CSS Specification Section 9.4.1:

In a block formatting context,boxes are laid out one after the other,vertically,beginning at the top of a containing block.

因此,这也不是创建新行,因为在块格式化的上下文中,没有线的概念.
它是关于从上到下依次放置块元素.

在你的第二个例子中:

<div>one line</div>
<div></div>
<div>still two lines because the empty div doesn't count</div>

空DIV具有零高度,因此它对下一个块级元素的呈现没有影响.

在你的第五个例子中:

<div><div>Wrapped tags generate only one new line<br/></div></div>
<div><br/></div>
<div>three lines this time because the second br tag is not ignored</div>

外部DIV作为Section 9.1.2定义的包含块
内部DIV定义为上面引用的第9.4.1节.
因为没有应用CSS,所以DIV元素默认为零边距和零填充,
这使得内部DIV的每个边缘与外部DIV接触相应的边缘.
换句话说,内部DIV呈现在与外部DIV完全相同的位置.

我相信这就是一切.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读