在伪元素上添加 CSS 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10609142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding CSS class on pseudo-element
提问by Zhao Li
I was wondering if there's a way to add a CSS class to pseudo-elements, such as :after.
我想知道是否有办法将 CSS 类添加到伪元素,例如:after。
I want to use :after to append an error message. I also want the error message styled the same way as the other error messages.
我想使用 :after 附加错误消息。我还希望错误消息的样式与其他错误消息的样式相同。
This works:
这有效:
.error:after {
content: "Error Message";
color: red;
}
But can I do something like this to add more than the color styling?:
但是我可以做这样的事情来添加比颜色样式更多的东西吗?:
.error:after {
content: "Error Message";
class: error_message_styles;
}
Also, is there a difference between using "::after" vs ":after"?
另外,使用“::after”和“:after”有区别吗?
Thanks in advance!
提前致谢!
采纳答案by Wesley Murch
There's no way to give an HTML attribute of any kind to a psuedo element.
无法为伪元素赋予任何类型的 HTML 属性。
Just use the selector twice:
只需使用选择器两次:
.error:after {
content: "Error Message";
}
.error:after, .error-style {
color:red;
}
Also, is there a difference between using "::after" vs ":after"?
另外,使用“::after”和“:after”有区别吗?
According to this:
根据这个:
https://developer.mozilla.org/en/CSS/:after
The
::after
notation was introduced in CSS 3 in order to establish a discrimination between pseudo-classes and pseudo-elements. Browsers also accept the notation:after
introduced in CSS 2.Note:Microsoft Internet Explorer 8 supports the :after notation only.
https://developer.mozilla.org/en/CSS/:after
::after
CSS 3 中引入了该符号,以便在伪类和伪元素之间建立区分。浏览器也接受:after
CSS 2 中引入的符号。注意:Microsoft Internet Explorer 8 仅支持 :after 表示法。
I would argue that error messages might be content, not decoration - but that's your call.
我认为错误消息可能是内容,而不是装饰 - 但这是您的要求。