登录  /  注册
首页 > web前端 > H5教程 > 正文

a 标签为什么能够包含块级元素?

php中文网
发布: 2016-06-07 08:41:46
原创
3522人浏览过

按照html嵌套规则行内元素不能包含块级元素,a标签为什么可以包含

回复内容:

这个和 CSS 没关系。而且按新的 HTML 规范,已经不按 inline 和 block 来区分元素类型了。

在规范中每个元素会规定如下两项:
  • Categories
  • Content Model
Categories 是改元素本身的分类,content model 规定了合法的元素的内容(子元素、文本等)类型。

a 元素的 content model 为 transparent
Some elements are described as transparent; they have "transparent" in the description of their content model. The content model of a transparent element is derived from the content model of its parent element: the elements required in the part of the content model that is "transparent" are the same elements as required in the part of the content model of the parent of the transparent element in which the transparent element finds itself.
大意就是这类元素本身内部可以有任何类型的内容,是否合法要看其父元素的 content model 和其内容的 categories。

比如我们要看 p > ins > a > div 是否合法,过程是这样的:
p 元素的 content model 是 phrasing content,ins 本身属于 phrasing content 故可以嵌套;
ins 元素的 content model 是 transparent,故在此时里面是否能有 a 需检查 p > a 的合法性;
a 元素也属于 phrasing content,故 p > ins > a 合法;
a 元素的 content model 也是 transparent,故此时里面包含 div 的合法性向上传递,检查 ins > div 又向上传递,变成检查 p > div;
div 不属于 phrasing content,所以这个嵌套是不合法的。 路过看见
扯一嘴

非要从标准说的话
肯定跟CSS没关系的
得看HTML标准
可是这货呢 HTML4 和 HTML5 说法又完全不一样
H5的鸟规范整出一堆概念
就如同 @顾轶灵 说的一样

H4 里说的简单到爆
这部分基本就一句
通常情况下行内级元素不能包含块级元素”
看看通常情况下呢
所以
这句然并卵
厂商压根不鸟

厂商鸟的只有特定规定
比如
"P元素不能包含其他块级元素(包括P自身)"
等等
所以
H4里“不能”才有约束作用
"通常情况下不能" 就然并卵
就这样咯 原则上不行,但草坪上的小径一样,走的人多了就成了路。一般情况下,注意p,dt等特殊情况就可以了 在html的规则下确实不能包含,嵌套div只会被强行截断。不过innerHTML或appendChild则可以强行忽略,但不建议,可能有浏览器有未知问题。
html解析的步骤先于css生效。
在css环节则没有这种限制。 本来是不可以的,但是很多人都这么用的,所以就可以了。

标准也是要顺应民意的。 建议你可以观察下行内元素包含块级元素浏览器会怎么处理。标准里也明确说明了发生这种事情时应该怎样处理。

Visual formatting model

when an inline box contains an in-flow block-level box, the inline box (and its inline ancestors within the same line box) are broken around the block-level box (and any block-level siblings that are consecutive or separated only by collapsible whitespace and/or out-of-flow elements), splitting the inline box into two boxes (even if either side is empty), one on each side of the block-level box(es). the line boxes before the break and after the break are enclosed in anonymous block boxes, and the block-level box becomes a sibling of those anonymous boxes. when such an inline box is affected by relative positioning, any resulting translation also affects the block-level box contained in the inline box.

This model would apply in the following example if the following rules:

<code class="language-text">p    { display: inline }
span { display: block }
</code>
登录后复制
我以前写样式经常这样写
或者

后来才知道这是错的,因为后台快疯了,看着a标签总是包含在最外面,虽然效果是可以显示出来的,但是后台已疯 p>div, 这个不行; a>div单看可以 最新的HTML标准是a标签不能包含div,在IE下没事,但是在其它浏览器中就截断了 HTML5标准定义可以这么做:
4.5 Text-level semantics
The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links). ...used to make an entire advertising block into a link
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号