CSS :after not adding content to certain elements

标签 css pseudo-element

我无法理解 CSS 的行为 :after属性(property)。根据规范(herehere):

As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document tree content.

这似乎没有限制哪些元素可以有 :after (或 :before )属性(property)。但是,它似乎只适用于 specific elements ... <p>作品,<img>没有,<input>没有,<table>做。我可以测试更多,但重点是。请注意,这在浏览器中似乎非常一致。 是什么决定了一个对象是否可以接受一个:before:after属性(property)?

最佳答案

imginput都是replaced elements .

A replaced element is any element whose appearance and dimensions are defined by an external resource. Examples include images (<img> tags), plugins (<object> tags), and form elements (<button>, <textarea>, <input>, and <select> tags). All other elements types can be referred to as non-replaced elements.

:before:after仅适用于非替换元素。

来自 the spec :

Note. This specification does not fully define the interaction of :before and :after with replaced elements (such as IMG in HTML). This will be defined in more detail in a future specification.

span:before, span:after ,DOM 看起来像这样:

<span><before></before>Content of span<after></after></span>

显然,这不适用于 <img src="" /> .

关于CSS :after not adding content to certain elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29254229/

相关文章:

javascript - 如何使用户名框突出显示?

android - NativeScript:字体系列未在 Android 上加载

jquery - 当悬停另一个元素时,如何从一个元素中删除框阴影效果?

javascript - 如何更改 Fancybox 预加载器图像?

html - :before selector not taking full height 中的 css 边框样式

CSS缩放元素不影响:after pseudo-element

html - 将 div 类放在导航栏 Bootstrap 之上

jquery - CSS nth-child(n) 在附加 jQuery 后没有响应

css - 伪元素之前的响应式 CSS 转换

CSS伪元素全高