我听说在内联元素中放入块元素是一种HTML错误:
<a href="http://example.com">
<div>
What we have here is a problem.
You see, an anchor element is an inline element,
and the div element is a block level element.
</div>
</a>
但是如果在样式表中将外部锚设置为display:block呢?还是错的吗?关于块级和内联元素的HTML 4.01规范似乎是这样认为的:
样式表提供了实现的方法
指定任意的呈现
元素,包括是否为元素
呈现为块或内联。在
有些情况,比如内联样式
对于列表元素,这可能是
恰当,但总的来说,
不鼓励作者
超越常规
HTML元素的解释
这种方式。
有没有人对这个问题有进一步的建议?
2023年1月更新:最新HTML5版本
Nothing is wrong with placing a div inside a tag. In fact, you can place just about anything inside a tag as long as they are categorized as transparent, except that no descendant may be interactive content (eg: buttons or inputs) or an a element, and no descendant may have a specified tabindex attribute.
Please refer to these documentations:
https://www.w3.org/TR/2011/WD-html5-20110525/text-level-semantics.html#the-a-element
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#properties.
If you inspect Angular Material official website, you can tell that they are indeed using div inside a element.
这是我从他们的网站上截取的一个例子:
<a href="/components/badge">
<div class="mat-list-item-content">
<div mat-ripple="" class="mat-ripple mat-list-item-ripple"></div>
<div class="mat-list-text"></div>
</div>
</a>