a 标签为什么能够包含块级元素?
按照html嵌套规则行内元素不能包含块级元素,a标签为什么可以包含
回复内容:
这个和 CSS 没关系。而且按新的 HTML 规范,已经不按 inline 和 block 来区分元素类型了。在规范中每个元素会规定如下两项:
- 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:
我以前写样式经常这样写或者后来才知道这是错的,因为后台快疯了,看着a标签总是包含在最外面,虽然效果是可以显示出来的,但是后台已疯 p>div, 这个不行; a>div单看可以 最新的HTML标准是a标签不能包含div,在IE下没事,但是在其它浏览器中就截断了 HTML5标准定义可以这么做:p { display: inline } span { display: block }로그인 후 복사
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 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











HTML의 테이블 테두리 안내. 여기에서는 HTML의 테이블 테두리 예제를 사용하여 테이블 테두리를 정의하는 여러 가지 방법을 논의합니다.

HTML 여백-왼쪽 안내. 여기에서는 HTML margin-left에 대한 간략한 개요와 코드 구현과 함께 예제를 논의합니다.

HTML의 Nested Table에 대한 안내입니다. 여기에서는 각 예와 함께 테이블 내에 테이블을 만드는 방법을 설명합니다.

HTML 테이블 레이아웃 안내. 여기에서는 HTML 테이블 레이아웃의 값에 대해 예제 및 출력 n 세부 사항과 함께 논의합니다.

HTML 순서 목록에 대한 안내입니다. 여기서는 HTML Ordered 목록 및 유형에 대한 소개와 각각의 예에 대해서도 설명합니다.

HTML 입력 자리 표시자 안내. 여기서는 코드 및 출력과 함께 HTML 입력 자리 표시자의 예를 논의합니다.

HTML에서 텍스트 이동 안내. 여기서는 Marquee 태그가 구문과 함께 작동하는 방식과 구현할 예제에 대해 소개합니다.

HTML onclick 버튼에 대한 안내입니다. 여기에서는 각각의 소개, 작업, 예제 및 다양한 이벤트의 onclick 이벤트에 대해 설명합니다.
