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选择器样式为准

时间: 2024-08-10 23:49:38

css笔记09:选择器优先级的相关文章

CSS中选择器优先级与!important权重使用

原文:CSS中选择器优先级与!important权重使用 CSS中的选择器优先级与!important权重使用# .class选择器要高于标签选择器. #id选择器要高于.class选择器. 标签选择器是优先级最低的选择器. !important的属性它的权重值优先级最高的,大于所有的选择器. 标签选择器和.class选择器# 让我们进入标签选择器和.class选择器谁的优先级高实践,实践内容如:将HTML页面中的h2标签设置文本颜色. 代码块 Copy <!DOCTYPE html> <

CSS选择器优先级【转】

样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External style sheet <(内部样式)Internal style sheet <(内联样式)Inline style 有个例外的情况,就是如果外部样式放在内部样式的后面,则外部样式将覆盖内部样式. 示例如下: <head>     <style type="text/css&

我给女朋友讲编程CSS系列(2)- CSS语法、3大选择器、选择器优先级

首先看一下使用Css设置h1标签字体颜色和大小的例子,效果图如下: 新建一个网页test.html,然后复制粘贴下面的内容: <html> <head> <style type="text/css"> h1 {color:red; font-size:14px;} </style> </head> <body > <h1>使用Css让h1标签字体变红</h1> </body> &

css选择器 优先级的问题

最近写代码时发现,有时候写出的样式不会被引用.看了一些其他人写的网站源码后,我发现了这样写可以解决问题,如下: 比如说<div class="abc">  <div class="edf">     <div class="fgg">    </div>  </div></div>确定class="fgg"时,写成:.abc .efg .fgg{/* 代码

导航栏布局时遇到的问题以及解决办法 css选择器优先级

得到的导航栏效果 添加#menu ul li{width:30px;} 效果如图 将会使列表项和分隔区域的宽度同时改变因为id选择器的优先级高于类选择器,此时应该为列表项添加内联样式如图 才能得到如下效果 或者可以使用第二种方法 #menu ul li{ float:left; margin-left:10px;text-align:center;padding-left:10px;实现列表项文字的居中 line-height:28px; height:28px; width:40px; bor

CSS选择器优先级(转)

原文:http://www.cnblogs.com/wangfupeng1988/p/4285251.html 另外,w3c有文章介绍了CSS选择器的特定性,见https://www.w3.org/TR/css3-selectors/#specificity 1. 引言 上一节<css知多少(5)——选择器>最后提到,选择器类型过多将导致一些问题,是什么问题呢?咱们直接举例子说明. 上图中,css中的两个选择器都是针对<span>的,而且两个设置的颜色不一样,这里的<span

CSS总结(四)—— CSS选择器优先级

css选择器优先级 优先顺序: 1.!important (IE6不支持此属性) 2.html中元素标签添加的style=“”(css难以管理,不推荐) 3.一个或多个id选择器 例:#top{margin:0;} 4.一个或多个类选择器(包括属性.伪类选择器) 例:.outer{}   [data-job="frontend"]{}    a:active{} 5.标签选择器 例:div{margin:3px;} 6.通配选择器 例:*{margin:10px;} ★ 选择器规则:

一道关于CSS选择器优先级的题

<span style="font-size:14px;"><html> <head> <style type="text/css"> #redP p { /* 权值 = 100+1=101 */ color:#F00; /* 红色 */ } #redP .red em { /* 权值 = 100+10+1=111 */ color:#00F; /* 蓝色 */ } #redP p span em { /* 权值 =

CSS常用的选择器和优先级的权重问题

CSS注释 CSS修改页面中的所有标签,必须借助选择器选中.选择器中,可以写多对CSS属性,用{}包裹:每个属性名与属性值之间用:分隔,多对属性之间,必须用;来分隔 选择器{ 属性1:属性值1; 属性2:属性值2; [选择器的命名规范] 1.只能有字母.数字.下划线.减号组成.2.开头不能是数字,也不能是只有一个减号.一般,起名要求有语义,使用英文单词与数字的组合. 1.标签选择器 写法:HTML标签名{}作用:可以选中页面中,所有与选择器同名的HTML标签. 例如: li{ color:red