CSS常用选择符和权重

1.css语法

css语法由两部分组成:选择符、声明

语法:选择符{属性:属性值;属性:属性值;}

说明:
(1)每个css样式由两部分组成,即选择符和声明,声明又分为属性和属性值
(2)属性必须放在花括号中,属性与属性值用冒号链接
(3)每条声明用分号结束
(4)当一个属性有多个属性值得时候,属性值与属性值不分先后顺序
(5)在书写样式过程中,空格、换行等操作不影响属性显示。
例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css"> 
            div {width: 100px;
               height: 100px;
               background: #f00;
               border:#000  10px solid;}
        </style>
    </head>
    <body>
        <div>
        </div>
    </body>
</html>

2.常用的选择符

选择符表示要定义样式的对象,可以是元素本身,也可以是一类元素或者制定名称的元素

常用的选择符有:
类型选择符(标记选择器)、类选择符(class选择符)、ID选择符(id选择器)、伪装选择器、群组选择器(集合选择器)、通配符(*)、伪对象选择符、包含选择符(后代选择器)

元素选择符/类型选择符(element选择器)
语法:
元素名称{属性:属性值;}
说明:
元素选择符就是以文档语言对象类型为选择器,即使用结构中元素名称作为选择符。例如body、div、p、em、strong、span等。所有的页面元素都可以作为选择符。
用法:
如果想改变某个元素得默认样式时,可以使用类型选择符。
当统一文档某个元素的显示效果时,可以使用类型选择符。

ID选择符
语法:
#id{属性:属性值;}

说明:
当我们使用id选择符时,应该为每一个元素定义一个id名
起名时要取英文名,不能用关键字:(所有标记和属性都是关键字)
一个id名称只能对应文档中一个具体的元素对象,因为id只能定义页面中某一个唯一的元素对象
最大用处:创建页面外围结构。 

类选择符(class选择符)
语法:
.class名{属性:属性值;}
说明:
当我们使用类选择符时,应先为每一个元素定义一个类名称
class选择符更适合定义一类样式
例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css"> 
            div {width: 300px;
               height: 30px;
               background: #f00;
               margin-bottom: 10px;}
            h1 {width: 600px;
                height: 50px;
                background: #0f0;              
            }
            span {color: #fff;}
            #box {width: 500px; height: 50px; background: #0ff;}
            #box1 {width: 500px; height: 50px; background: #ff0;}
            .aa {border:5px #000 dotted; }
        </style>
    </head>
    <body>
        <div id="box">房东的猫</div> 
        <div class="aa">房东的猫</div>
        <div>房东的猫</div>
        <div class="aa">房东的猫</div>
        <div >房东的猫</div>
        <div class="aa">房东的猫</div>
        <div>房东的猫</div>
        <div class="aa">房东的猫</div>
        <div>房东的猫</div>
        <div><span>房东的猫</span></div>
        <h1><span>天下</span></h1>
        <h1>天下</h1>
        <h1>天下</h1>
        <h1 class="aa">天下</h1>
        <h1>天下</h1>
        <h1>天下</h1>
        <h1 class="aa">天下</h1>
        <h1>天下</h1>
        <h1>天下</h1>
        <h1 id="box1">天下</h1>
    </body>
</html>
*通配符选择符
语法:*{属性:属性值;}
说明:
通配选择符的含义就是所有元素,常用来重置样式
例:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
    * {margin: 0; padding: 0;}
    div {width: 300px; height: 30px; background: #f00;}
    h1 {background: #f0f;}
    p {background: #0f0;}
    </style>
</head>
<body>
    <div>当真</div>
    <div>当真</div>
    <h1>当真</h1>
    <p>当真</p>
    <ul>
        <li>当真</li>
        <li>当真</li>
        <li>当真</li>
        <li>当真</li>
    </ul>
</body>
</html>
群组选择符
语法:
选择符1,选择符2,选择符3{属性:属性值;}
说明:
当有多个选择符应用相同的样式时,可以将选择符用“,”分隔的方式,合并为一组。
例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
        .box,h1,#fox {width: 600px; height: 50px; border: 5px #000 dashed;}
        .box { background: #f00;}
        h1 {  background:  #00f;}
        #fox { background: #0f0;}
        </style>
    </head>
    <body>
        <div class="box">白茶清欢无别事</div>
        <h1>山有木兮木有枝</h1>
        <p id="fox">房东的猫</p>      
    </body>
</html>
包含选择符
语法:
选择符1、选择符2{属性:属性值;}
说明:
选择符1和选择符2用空格隔开,含义就是选择符1中包含的所有选择符2

例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
        .box1,.box2 {width: 600px;border: 5px #000 solid; margin-bottom: 20px;}
        .box1 span {color: #f00;}
        .box2 span {color: #f0f;}
        </style>
    </head>
    <body>
      <div class="box1">我于杀戮之中绽放,亦如黎明中的花朵
          <span>指尖星火,请君共赏</span>
      </div>
      <div class="box2">我于杀戮之中绽放,亦如黎明中的花朵
        <span>指尖星火,请君共赏</span>
    </div>
    </body>
</html>
伪类选择符
语法:
a:link{属性:属性值;}超链接的初始状态
a:visited{属性:属性值;}超链接被访问后的状态
a:hover{属性:属性值;}鼠标划过超链接时的状态
a:active{属性:属性值;}鼠标按下时超链接的状态

例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
        a:link {color: #f00;}
        a:visited {color: #eee;}
        a:hover {color: #0f0;}
        a:active {color: #00f;}
        </style>
    </head>
    <body>
        <h1><a href="#">百度</a></h1>
    </body>
</html>
说明:
当这4个超链接伪类选择符联合使用时,应注意他们的顺序,正常顺序为:
a:link、a:visited、a:hover、a:active,错误顺序有时候会使超链接的样式失效
为了简化代码,可以把伪类选择符中相同的声明提出来放在a选择符中

例如:a{color:red;} a:hover{color:green;}表示超链接的三种状态都相同,只有鼠标划过变颜色

例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
        a {color: #f00;}
        a:hover {color: #0f0;}
        </style>
    </head>
    <body>
        <h1><a href="#">百度</a></h1>
    </body>
</html>

3.选择符的权重

css中用四位数字表示权重,权重表达方式如:0,0,0,0
类型选择符权重为:0001
class选择符权重为:0010
id选择符权重为:0100
属性选择符权重为:0010
伪类选择符的权重为:0010
伪元素选择符的权重为:0010
包含选择符的权重:包含的选择符权重值之和
内联样式的权重为1000
继承样式的权重为:0000

当不同选择符的样式设置有冲突的时候,高权重选择符的样式会覆盖低权重选择符的样式
相同权重的选择符,样式遵循就近原则,那个选择符最后定义,就采用那个选择符样式。
例:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            #box {width: 300px; height: 300px; background: #f00;}
            .aa {width: 100px; height: 100px; background: #0f0;}
            .aa {width: 100px; height: 100px; background: #00f;}
        </style>
    </head>
    <body>
        <div id="box" class="fox"></div>
        <div class="aa"></div>
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值