css笔记09:选择器优先级

1.

(1)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>选择器</title>
<link rel="stylesheet" href="my.css"  type="text/css"/>
</head>


<body>
你好,北京!
   <span class="style1"  id="special_new">新闻1</span>
   <span class="style1">新闻2</span>
   <span class="style1">新闻3</span>
   <span class="style1">新闻4</span>
   <span class="style1">新闻5</span>

   <span id="style2">这是一则<span>非常重要</span>的新闻</span><br />
 
   <a href="#">goto sohu</a><br />
   <a href="#">goto sina</a><br />
</body>
</html>

(2).my.css文件如下:

@charset "utf-8";
/* CSS Document */

/*html的选择器*/
body {
    color:orange;
}

a:link {
    color:black;
    text-decoration:none;
}

a:hover {
    text-decoration:underline;
}

a:visited {
    color:red;
}



/*.style1 就是类选择器*/
.style1 {
    font-weight:bold;
    font-size:20px;
    background-color:pink;
    color:black;
}

#special_new {
    font-style:italic;
    color:red;
}

/*#style2就是id选择器*/

#style2 {
    font-size:30px;
    background-color:silver;
    color:black;
}

/*#style2 span 就是父子选择器, #style2是父,span是子*/
#style2 span {
    font-style:italic;
    color:red;
}

/*#style2 span 就是父子选择器, #style2是父,span是子,也包含层次关系*/
#style2 span span{
    font-size:50px;
}

如果一个元素被id和class同时修饰时,id选择器的优先级>class选择器

2.一个元素最多有一个id选择器,但是可以有多个类选择器

(1)html文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>选择器</title>
<link rel="stylesheet" href="my.css"  type="text/css"/>
</head>


<body>
你好,北京!
   <span class="style1" id ="special_new">新闻1</span>
   <span class="style1">新闻2</span>
   <span class="style1 style4">新闻3</span>
   <span class="style1">新闻4</span>
   <span class="style1">新闻5</span>

   <span id="style2">这是一则<span>非常重要</span>的新闻</span><br />
    
   
   <a href="#">goto sohu</a><br />
   <a href="#">goto sina</a><br />
</body>
</html>

这里"新闻3"修饰的是两个.class类,如果修饰发生冲突,到底哪个为准呢?

(2)my.css文件

@charset "utf-8";
/* CSS Document */

/*html的选择器*/
body {
    color:orange;
}

a:link {
    color:black;
    text-decoration:none;
}

a:hover {
    text-decoration:underline;
}

a:visited {
    color:red;
}



/*.style1 就是类选择器*/
.style1 {
    font-weight:bold;
    font-size:20px;
    background-color:pink;
    color:black;
    font-style:normal;
}

.style4 {
    font-style:italic;
    text-decoration:underline;
    color:green;
}

#special_new {
    font-style:italic;
    color:red;
}

/*#style2就是id选择器*/

#style2 {
    font-size:30px;
    background-color:silver;
    color:black;
}

/*#style2 span 就是父子选择器, #style2是父,span是子*/
#style2 span {
    font-style:italic;
    color:red;
}

/*#style2 span 就是父子选择器, #style2是父,span是子,也包含层次关系*/
#style2 span span{
    font-size:50px;
}

这里基准是:在css文件中,那个.class文件在后面定义的,就显示它的效果:比如这里的.style4定义在.style1后面,所以以.style4效果为准

 

3.

--->在引用多个class选择器时候,用空格隔开

--->当class选择器发生冲突时候,在css文件中,最后出现的class选择器样式为准

原文地址:https://www.cnblogs.com/hebao0514/p/4624055.html