CSS3新增选择器之属性选择器

新增选择器

css3新增了三个选择器,分别为属性选择器、结构伪类选择器、伪元素选择器。
1.属性选择器
css3属性选择器用[ ]表示,它的权重为10。

<style>
        /!--选择type属性是text的input标签--/
        input[type="text"] {
            color: cadetblue;
            outline: none;
        }
        /!--选择有value属性的input标签--/
        input[value] {
            outline: none;
            color: cornflowerblue;
            font-size: 12px;
        }
       /!-- 选择class属性中以icon开头的div标签--/
        div[class^="icon"] {
            color: cyan;
        }
       /!-- 选择class属性中以data结尾的section标签--/
        section[class$="data"] {
            color: darkorchid;
        }
        /!-- 选择class属性中含有icon的section标签--/
        section[class*="icon"] {
           font-size: 19px;
        }
    </style>
</head>
<body>
    <div class="xx">
        <h2>型芯</h2>
        <div></div>
        <div></div>
        <div></div>
    </div>
        <!-- 1. 利用属性选择器就可以不用借助于类或者id选择器 -->
    <input type="text" value="请输入用户名"> 
    <input type="text"> -->
    <!-- 2. 属性选择器还可以选择属性=值的某些元素 重点务必掌握的 -->
    <input type="text" name="" id="">
    <input type="password" name="" id="">
    <!-- 3. 属性选择器可以选择属性值开头的某些元素 -->
    <div class="icon1">小图标1</div>
    <div class="icon2">小图标2</div>
    <div class="icon3">小图标3</div>
    <div class="icon4">小图标4</div>
    <div>我是打酱油的</div>
    <!-- 4. 属性选择器可以选择属性值结尾的某些元素 -->
    <section class="icon1-data">语文</section>
    <section class="icon2-data">数学</section>
    <section class="icon3-ico">英语</section> 
</body>

2.结构伪类选择器
根据文档结构来选择元素,常用于父级选择器里面的子元素,用符号" : " 表示,它的权重为10。

<style>
        /* 选中 类xx  下的div  第二个孩子元素   =》 行 
            必须得有一个  共同父元素
         
        */
        /* 这个是从 h2 开始数得 */
        .xx div:nth-child(n+1) {    
            color: #00f;
        }
        /* 这个是从 div还是数得/
      
        .xx div:nth-of-type(2) {
            color: #f00;
        }
       
    </style>
</head>
<body>
    <div class="xx">
        <h2>型芯</h2>
        <div></div>
        <div></div>
        <div></div>
    </div>
        <!-- 1. 利用属性选择器就可以不用借助于类或者id选择器 -->
    <input type="text" value="请输入用户名"> 
    <input type="text"> -->
    <!-- 2. 属性选择器还可以选择属性=值的某些元素 重点务必掌握的 -->
    <input type="text" name="" id="">
    <input type="password" name="" id="">
    <!-- 3. 属性选择器可以选择属性值开头的某些元素 -->
    <div class="icon1">小图标1</div>
    <div class="icon2">小图标2</div>
    <div class="icon3">小图标3</div>
    <div class="icon4">小图标4</div>
    <div>我是打酱油的</div>
    <!-- 4. 属性选择器可以选择属性值结尾的某些元素 -->
    <section class="icon1-data">语文</section>
    <section class="icon2-data">数学</section>
    <section class="icon3-ico">英语</section> 
</body>

1.nth-child(n) (括号里面的字母只能为n,n可以为数字或表达式)

  • ul li: frist-child : ul中的第一个孩子
  • ul li : nth-child (n) ul中的第n个孩子
  • even 偶数,odd 奇数

n如果是公式,则从0开始计算,但是第0个元素或者超出了元素的个数会被忽略

2.nth-of-type (n)(括号里面的字母只能为n,n可以为数字或表达式)

  • E : first-of-type 指定类型E的第一个孩子
  • E:last-of-type 指定类型E的最后一个孩子
  • E:nth-of-type(n) 指定类型E的第n个孩子

上述两种的区别:
nth-child 会把同一父元素下的所有子元素都排列序号,先找到第n个孩子,再看是否与E匹配。
nth-of-type对父元素指定子元素进行排列序号,先去匹配E,再根据E找第n个孩子。

3.伪元素选择器
在元素的后面或者前面插入内容,它的权重为1.

  • ::before 在元素内部的前面插入内容
  • ::after 在元素内部的后面插入内容
  • 伪元素选择器中必须有一个content=" "属性
<style> 
        div {
            width: 100px;
            height: 40px;
            background-color: yellow;
            /* border: 1px solid yellow; */
        }
        div::after {
            content: "hxx";
            width:50px;
            height:100%;
            display: inline-block;
            background-color: darkseagreen;
            /* float: right; */
        }
    </style>
</head>
<body>
    <div>
      我是
    </div>
</body>

css使用伪元素清除浮动
1.父级添加after伪元素

.clearfix:after {
         content:" ";
         display: block;
         height: 0;
         clear: both;
         visibility: hidden;
         }

2.父级添加双伪元素

.clearfix: before, .clearfix: after {
         content: " ";
         display: table;让两个盒子在一行
         }
 .clearfix: after {
         clear: both;
         }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值