一线和第一子女在CSS中的特异性? [英] The specificity of first-line and first-child in CSS?

查看:129
本文介绍了一线和第一子女在CSS中的特异性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下html代码:

 <!DOCTYPE html> 
< html>
< head>
< style>
:第一行{
color:red;
}
:first-child {
color:blue;
}
< / style>
< / head>
< body>
< p> asdasdasdsad< br> sdfsdfs< / p>
< / body>
< / html>

输出为:



asdasdasdasd< ; - 红色



sdfsdfs < - blue。



但是,我想的是p标签是身体的第一个孩子。 first-child是一个伪类,它的特异性为10,第一行是一个伪元素,它的特异性为1.因此,两行都应该显示为蓝色文本,但是我显然错了。 / p>

请指出我的错误。非常感谢。

解决方案

:第一行直接到第一行,而:first-child 选择器继承自其适用的元素。



直接应用于项目的样式优先于继承的样式。



您可以使元素的选择器更具体,例如添加元素,id和类选择器,但它仍然不能优先于应用于第一行的样式。演示: http://jsfiddle.net/Guffa/3H4Ab/


I have the following html code:

<!DOCTYPE html>
<html>
<head>
<style>
:first-line {
    color: red;
}
:first-child {
    color: blue;
}
</style>
</head>
<body>
<p>asdasdasdsad<br>sdfsdfs</p>
</body>
</html>

The output is:

asdasdasdasd <- red

sdfsdfs <- blue.

However, what I am thinking is that the p tag is the first child of body. first-child is a pseudo-class, which has a specificity of 10 and first-line is a pseudo-element, which has a specificity of 1. Therefore, both lines should be displayed as blue text, but I am apparently wrong.

Please point out my mistake. Thank you very much.

解决方案

The :first-line selector applies directly to the first line, while the :first-child selector is inherited from the element that it applies to.

A style that applies directly to the item takes precedence over inherited styles.

You can make the selector for the element more specific, by for example adding elements, id and class selectors, but it still can't take precedence over the style applied to the first line. Demo: http://jsfiddle.net/Guffa/3H4Ab/

这篇关于一线和第一子女在CSS中的特异性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆