CSS3学习笔记

CSS3选择器

概念:

CSS是层叠样式表(Cascading Style Sheets)的简称,并且它是增量式,向后兼容,所以它支持CSS3以前的所有的版本,当然包括CSS2的内容,CSS3也称作级联样式表或者是层级样式表。CSS3是CSS技术的升级版本,CSS3语言开发是朝着模块化发展的

基本选择器:

  1. 回顾css选择器:通配符选择器,元素选择器,类选择器,ID选择器,后代选择器
  2. 新增选择器:子选择器,相邻兄弟选择器,通用兄弟选择器,群组选择器
基本选择器——子选择器
  1. 概念:子选择器只能选择某元素的子元素
  2. 语法格式:父元素>子元素
  3. 兼容性:ie8+
基本选择器——相邻兄弟选择器
  1. 概念:可以选值紧接在另一元素后的元素,而且他们具有相同的父元素
  2. 语法格式:元素+兄弟相邻元素
  3. 兼容性:ie8+
  4. 相邻兄弟选择器例子
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        section > div + article{
    
            color: #f00;
        }
    </style>
</head>
<body>
<section>
    <div>article外面的文字</div>
    <article>
        <div>article里面的文字</div>
    </article>
    <article>
        <div>article里面的文字</div>
    </article>
</section>   

效果:相邻兄弟选择器效果

基本选择器——通用兄弟选择器
  1. 选择某元素后面的所有兄弟元素,而且他们具有一相同的父元素
  2. 语法格式:元素~后面所有兄弟相邻元素
  3. 通用兄弟选择器例子
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        section > div ~ article{
    
            color: #f00;
        }
    </style>
</head>
<body>
<section>
    <article>
        <div>article里面的文字</div>
    </article>
    <div>article外面的文字</div>
    <article>
        <div>article里面的文字</div>
    </article>
    <article>
        <div>article里面的文字</div>
    </article>
    <article>
        <div>article里面的文字</div>
    </article>
    <article>
        <div>article里面的文字</div>
    </article> 

效果:
通用兄弟选择器

基本选择器——群组选择器
  1. 概念:把具有相同样式的元素分组放在一起,每个选择器之间使用逗号“,”,隔开
  2. 语法格式:元素一,元素二,…,元素n
  3. 兼容性:ie6+
  4. 群组选择器例子
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        section > article,
        section > aside,
        section > div{
    
            color: #f00;
            margin-top: 10px;
            background: #abcdef;
        }
    </style>
</head>
<body>
<section>
     <article>article</article>
     <aside>aside</aside>
     <div>div</div>
</section>

效果:
在这里插入图片描述

属性选择器:

  1. 对带有指定属性的HTML元素设置样式
  2. 使用css3属性选择器,可以只指定元素的某个属性,或者还可以同时指定元素的某个属性和其对应的值
Element[attribute] //元素【属性】 选择器
  1. 概念:选值所有带有attribute属性的元素
  2. 兼容性:ie8+
  3. Element[attribute]例子
<style type="text/css">
        a[href]{
    
            text-decoration: none;
        }
    </style>
</head>
<body>
 <a href="#">attribute</a>   
</body>

效果:
在这里插入图片描述

Element[attribute=“value”] //元素【属性的某一个值】 选择器
  1. 概念:选择所有使用attribute="value"的元素
  2. 兼容性:ie8+
  3. Element[attribute="value"]例子
 <style type="text/css">
        a[href]{
    
            text-decoration: none;
        }
        a[href="#"]{
    
            color: #f00;
        }
    </style>
</head>
<body>
 <a href="attribute.html">attribute</a> 
 <a href="#">attribute</a>    
</body>

效果:
在这里插入图片描述

Element[attribute~=“value”] //元素【属性~=一个值】 选择器
  1. 概念:选择attribute属性包含单词value的元素
  2. Element[attribute~=“value”]选择器用于选取属性值中包含指定词汇的元素,只要是指定的属性,并且属性值列表中包含value,而不是在某个值中以value开头或结尾

  3. 兼容性:ie8+
  4. Element[attribute~="value"]例子
    <style type="text/css">
        a[href]{
    
            text-decoration: none;
        }
        a[href="#"]{
    
            color: #f00;
        }
        a[class~="two"]{
    
            color: #f00;
        }
    </style>
</head>
<body>
 <a href="attribute.html">attribute</a> 
 <a class="one two" href="#">attribute</a>  
 <a class="two three" href="#1">attribute</a> 
 <a href="#2">attribute</a> 
 <a href="#3">attribute</a> 
 <a href="#4">attribute</a>   
</body>

效果:
在这里插入图片描述

Element[attribute^=“value”] 和 Element[attribute$=“value”]
  1. 概念:选择attribute属性值以value (某一个值/单词/名字) 开头/结尾的所有元素
  2. 兼容性:ie8+
  3. Element[attribute^=“value”]匹配指定属性的指定value值开头的元素,如果class中有多个value值,第一个值中的第一个字母不是指定的值,即使后面的属性中首字母是指定的值,那么也不能匹配上

  4. Element[attribute^="value"] 和 Element[attribute$="value"]的例子
    <style type="text/css">
        a[href]{
    
            text-decoration: none;
        }
        a[href="#"]{
    
            color: #f00;
        }
        a[class~="two"]{
    
            color: #f00;
        }
        a[href^="#"]{
    
            color: #0f0;
        }
        a[href$="#"]{
    
            color: #f00;
        }
    </style>
</head>
<body>
 <a href="attribute.html">attribute</a> 
 <a class="one two" href="#">attribute</a>  
 <a class="two three" href="#1">attribute</a> 
 <a href="#2">attribute</a> 
 <a href="#3">attribute</a> 
 <a href="#4">attribute</a>  
 <a href="2#">attribute</a> 
 <a href="3#">attribute</a> 
 <a href="4#">attribute</a>  
</body>

效果:
在这里插入图片描述

Element[attribute*=“value”]
  1. 概念attribute属性值包含value(包含这个字符串就好)的所有元素
    Element[attribute*="value"] 的例子
    <style type="text/css">
        a[href*="#"]{
    
            color: #f00;
        }
    </style>
</head>
<body>
 <a href="attribute.html">attribute</a> 
 <a class="one two" href="#">attribute</a>  
 <a class="two three" href="#1">attribute</a> 
 <a href="#2">attribute</a> 
 
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值