html5嵌套规则,HTML5的元素嵌套规则

内联元素不能嵌套块元素

元素和

元素不能嵌套块元素

那么到底什么是块元素,什么是内联元素?

以下是W3C CSS2.1规范中对块元素和内联元素的定义:

Block-level elements are those elements of the source document that are formatted visually as blocks (e.g., paragraphs). The following values of the 'display' property make an element block-level: 'block', 'list-item', and 'table'.

Inline-level elements are those elements of the source document that do not form new blocks of content; the content is distributed in lines (e.g., emphasized pieces of text within a paragraph, inline images, etc.). The following values of the 'display' property make an element inline-level: 'inline', 'inline-table', and 'inline-block'. Inline-level elements generate inline-level boxes, which are boxes that participate in an inline formatting context.

我们可以这样理解:块元素一般都从新行开始,内联元素在一行内显示,我们也可以通过CSS属性display的'inline' 或 ' block' 来改变元素为内联元素或块元素,当然这是CSS中对元素的分类,显然用 'display' 的属性值来对html元素进行分类是不严谨的。

HTML5的元素嵌套规则

元素的嵌套规则和页面头部申明的DTD有着千丝万缕的关系,那么在最新的HTML5规范中是否对元素嵌套有着新的规范呢?

让我们先了解下W3C在最新的HTML5规范中对元素的分类方式:

879a324812c9f587d87faea38af38a79.png

如上图,元素的分类不再是块元素或内联元素这样来分类(其实从来就没有这样分),而是按照如下分类来分:Flow(流式元素)、Heading(标题元素)、Sectioning(章节元素)、Phrasing(段落元素)、Embedded(嵌入元素)、Interactive(交互元素)、Metadata(元数据元素)。

Flow(流式元素)

在应用程序和文档的主体部分中使用的大部分元素都被分类为流式元素。

a, abbr, address, area(如果它是map元素的后裔), article, aside, audio, b, bdi, bdo, blockquote, br, button, canvas, cite, code, command, datalist, del, details, dfn, div, dl,em, embed, fieldset, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, i, iframe, img, input, ins, kbd, keygen, label, map, mark, math, menu, meter,nav, noscript, object, ol, output, p, pre, progress, q, ruby, s, samp, script, section, select, small, span, strong, style(如果该元素设置了scoped属性), sub, sup, svg, table,textarea, time, u, ul, var, video, wbr, text

Heading(标题元素)

标题式元素定义一个区块/章节(section)(无论是明确的使用章节式内容的元素标记,或者标题式内容自身所隐含的)的标题。

h1, h2, h3, h4, h5, h6, hgroup

Sectioning(章节元素)

章节式元素是用于定义标题及页脚范围的元素。

article, aside, nav, section

Phrasing(段落元素)

段落式元素是文档中的文本、标记段落级文本的元素。

a(如果其只包含段落式元素), abbr, area(如果它是map元素的后裔), audio, b, bdi, bdo, br, button, canvas, cite, code, command, datalist, del(如果其只包含段落式元素), dfn, em, embed, i,iframe, img, input, ins(如果其只包含段落式元素), kbd, keygen, label, map(如果其只包含段落式元素), mark, math, meter, noscript, object, output, progress, q, ruby, s, samp, script,select, small, span, strong, sub, sup, svg, textarea, time, u, var, video, wbr, text

Embedded(嵌入元素)

嵌入式元素是引用或插入到文档中其他资源的元素。

audio, canvas, embed, iframe, img, math, object, svg, video

Interactive(交互元素)

交互式元素是专门用于与用户交互的元素。

a, audio(如果设置了controls属性), button, details, embed, iframe, img(如果设置了usemap属性), input(如果type属性不为hidden状态), keygen, label, menu(如果type属性为toolbar状态),object(如果设置了usemap属性), select, textarea, video(如果设置了controls属性)

Metadata(元数据元素)

元数据元素是可以被用于说明其他内容的表现或行为,或者在当前文档和其他文档之间建立联系的元素

base,command,link,meta,noscript,script,style,title

各分类会有交叉或重叠的现象,这说明在html5中,元素可能属于上述所有分类中的一个或多个。

例子(1):

~

元素:

Categories:

Flow content.

Heading content.

Palpable content.

Contexts in which this element can be used:

As a child of an hgroup element.

Where flow content is expected.

Content model:

Phrasing content.

其中的「Categories」说明该元素的类别,「Contexts in which this element can be used」说明该元素能在何种场景下被使用,也就是它的父元素是什么,「Content model」说明该元素可以包含的内容是什么,由于页面中的元素是层层嵌套的,一个元素有可能既是父元素同时也是子元素的角色,所以下面我们以「Content model」也就是可包含的子元素做讨论。

那么对于h1~h6元素:

它们同时属于Flow content 、Heading content 和 Palpable content三个分类

它们的父元素可以是,同时那些子元素是流式元素的元素也可以作为h1-h6元素的父元素

它们允许的子元素是段落式元素

例子(2):

元素

Categories:

Flow content.

Palpable content.

Contexts in which this element can be used:

Where phrasing content is expected.

Content model:

Flow content.

对于

元素:

同时属于Flow content 、 Palpable content分类

父元素必须是那些子元素为段落式元素的元素

允许的子元素是流式元素

元素允许的子元素是流式元素,流式元素基本涵括了页面中的大部分元素,所以我们在用
时可以不用担心嵌套错误的问题。

但对于

~

元素,它们允许的子元素为段落式元素,而段落式元素并不包含诸如

  1. 之类的元素,这就说明按照html5的规范,是不允许在标题元素内部嵌入

  1. 之类的元素。

例子(3):元素

Categories:

Flow content.

Phrasing content.

Interactive content.

Palpable content.

Contexts in which this element can be used:

Where phrasing content is expected.

Content model:

Transparent, but there must be no interactive content descendant.

对于元素:

同时属于Flow content 、 Phrasing content、Interactive content、Palpable content分类

父元素必须是那些子元素为段落式元素的元素

允许的子元素是以它的父元素允许的子元素为准,但不能包含交互式元素

这样看元素还是挺有意思的,允许的子元素要看它的父元素所能包含的子元素。

再来看这段代码

这时的父元素为

,对于

~

的标题元素上面已经提过,允许的子元素是段落式元素,那么此时对于

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值