CSS3选择器新增问题如何实现 CSS3选择器新增问题实现代码

作者:袖梨 2021-01-22

CSS3选择器新增问题如何实现?本篇文章小编给大家分享一下CSS3选择器新增问题实现代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

基本选择器扩展

1.子元素选择器

:tomato: #wrap > .inner {color: pink;} 也可称为直接后代选择器,此类选择器只能匹配到直接后代,不能匹配到深层次的后代元素 总结:>作用于元素的第一代后代,空格作用于元素的所有后代

2. 相邻兄弟选择器

:tomato: #wrap #first + .inner {color: #f00;}它只会匹配紧跟着的兄弟元素

3. 通用兄弟选择器

:tomato: #wrap #first ~ div { border: 1px solid;}它会匹配第二个元素,条件是它必须跟(不一定是紧跟)在第一个元素之后,且他们都有一个共同的父元素所有的兄弟元素

4. 选择器分组

:tomato: h1,p,h3{color: pink;} 此处的逗号我们称之为结合符

属性选择器

1. 子串值属性选择器

:tomato: [attr|=val] : 选择attr属性的值是val(包括val)或以val-开头的元素。

:tomato: [attr^=val] : 选择attr属性的值以val开头(包括val)的元素。

:tomato: [attr$=val] : 选择attr属性的值以val结尾(包括val)的元素。

:tomato: [attr*=val] : 选择attr属性的值中包含字符串val的元素 少数浏览器支持子串选择元素

2. 存在和值属性选择器

:tomato: [attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何。[attr=val]:该选择器仅选择 attr 属性被赋值为 val 的所有元素。

:tomato: [attr~=val]:表示带有以 attr 命名的属性的元素,并且该属性是一个以空格作为分隔的值列表,其中至少一个值为val。 比如位于被空格分隔的多个类(class)中的一个类。比如name="atguigu_llc atguigu"

:tomato: [name =val]:该选择器仅选择 name 属性被赋值为 val 的所有元素。

伪类与伪元素选择器

1. 链接伪类

:tomato: :link 表示作为超链接,并指向一个未访问的地址的所有锚

:tomato: :visited 表示作为超链接,并指向一个已访问的地址的所有锚

:tomato: :target 代表一个特殊的元素,它的id是URI的片段标识符

:exclamation: 注意:link,:visited,:target是作用于链接元素的!前两者只能在a标签上起作用

css;">*{
                margin: 0;
                padding: 0;
            }
            div{
                
                
                line-
                background: palegreen;
                color: hotpink;
                text-align: center;
                display: none;
            }
            a:visited{
                color:orange;
            }
            :target{
                display: block;
            }

2. 动态伪类

:tomato: :hover表示悬浮到元素上

:tomato: :active表示匹配被用户激活的元素(点击按住时)

:tomato:由于a标签的:link和:visited可以覆盖了所有a标签的状态,所以当:link,:visited,:hover,:active同时出现在a标签身上时 :link和:visited不能放在最后!!!

:tomato: 隐私与:visited选择器只有下列的属性才能被应用到已访问链接:color、background-color、border-color

:exclamation: 注意:hover,:active基本可以作用于所有的元素!

3. 表单相关伪类

①:disable 匹配被禁用的表单

②:checked 匹配被选中的表单

③:focus 匹配获焦的表单

④:enabled匹配可编辑的表单

实操练习1


    
    
    


    
    
    
    

实操练习2 自定义单选按钮


    
    
    


    
    
    

4. 伪元素

:tomato: 概念:伪元素表示页面中一些特殊的并不真实存在的元素(特殊的位置)

:tomato: 语法使用 ::开头

:tomato: 类别:①::first-letter ②::first-line ③::selection ④::before ⑤::after 注意:④和⑤必须结合content属性来使用

:tomato: 代码示例:


    
    
    
    


    
Hello are you okay

我还是一个段落 我是一个很多很多解放碑还不错保持经济刺激 哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈

5. 结构性伪类(重点)

:tomato: index的值从1开始计数!!!!index可以为变量n(只能是n)index可以为even odd

:tomato: #wrap ele:nth-child(index)表示匹配#wrap中第index的子元素,这些伪类都是根据所有的子元素进行排序,这个子元素必须是ele

:tomato: #wrap ele:nth-of-type(index)表示匹配#wrap中第index的ele子元素

除此之外:nth-child和:nth-of-type有一个很重要的区别!!nth-of-type以元素为中心,在同一类型中排序,nth-child (相对于:first-child:last-child 或者 :nth-child(1):nth-last-child(1))

/* even表示偶数

odd表示奇数

first-of-type:在p类型中排

first-child:所有元素排

*/

:tomato::nth-child(index)系列

:first-child

:last-child

:nth-last-child(index)

:tomato::nth-of-type(index)系列

:first-of-type
:last-of-type
:nth-last-type(index)
:only-of-type(相对于:first-of-type:last-of-type
 或者 :nth-of-type(1):nth-last-of-type(1))    
:not        
:empty(内容必须是空的,有空格都不行,有attr没关系)

代码实例

*{
            margin: 0;
            padding: 0;
        }
        #wrap li:nth-of-type(1){
            color: pink;
        }
        p:only-of-type{
            border: 1px solid;
        }

6. 伪元素选择器

::after

::before

::firstLetter

::firstLine

::selection

#wrap::before{
        content:"";
        display:block;
        
        
        background: #00FFFF;
    }
    #wrap::after{
        content:"";
        display:block;
        
        
        background: #0000FF;
    }

相关文章

精彩推荐