CSS选择器注意点

only-child与only-of-type

:only-child是一个 CSS 伪选择器,它匹配一个元素,该元素满足:

  • 该元素是其父级唯一孩子;
  • 该元素父级没有其他任何子元素;

:only-of-type是一个 CSS 伪选择器,它匹配一个元素,该元素满足:

  • 该元素是其父级的孩子;
  • 该父级没有其他与该元素相同类型的孩子;
  • 该父级可以有其他类型的孩子;
<!DOCTYPE html>
<html lang="en">
<!-- only-child与only-of-type比较  -->
<head>
    <meta charset="UTF-8">
    <title> only-child与only-of-type比较 </title>
    <style>
        .only-child p{
            background: green;
            color: #fff;
            padding: 10px
        }
        .only-child p:only-child{
            background: orange;
        }

        .only-of-type p{
            background: green;
            color: #fff;
            padding: 10px
        }
        .only-of-type p:only-of-type{
            background: orange;
        }
    </style>
</head>
<body>
    <h3>only-child演示</h3>
    <div class="only-child">
        <p>111111111</p>
        <p>111111111</p>
        <p>111111111</p>
    </div>
    <hr>

    <div class="only-child">
        <p>11111111</p>
    </div>
    <hr>

    <div class="only-child">
        <p>11111111</p>
        <a>22222222</a>
    </div>
    <hr>

    <h3>only-of-type演示</h3>
    <div class="only-of-type">
        <p>111111111</p>
        <p>111111111</p>
        <p>111111111</p>
    </div>
    <hr>

    <div class="only-of-type">
        <p>11111111</p>
    </div>
    <hr>

    <div class="only-of-type">
        <p>11111111</p>
        <a>22222222</a>
    </div>
    <hr>
    <h3>end</h3>
    <div></div>
</body>
</html>

8cec8b36710598d6f0701c2ff0b8a2ca1f0.jpg

first-child与first-of-type

:first-child是一个 CSS 伪选择器,它匹配一个元素,该元素满足:

  • 该元素是其父级的第一个孩子;

first-of-type是一个 CSS 伪选择器,它匹配一个元素,该元素满足:

  • 该元素是其父级的孩子;
  • 该元素在同类型的孩子中排在第一个;

显然,

:last-child 与 :last-of-type 

:nth-child(n) 与 :nth-of-type(n)

:nth-last-child(n) 与 :nth-last-of-type(n)

也是如此

<!DOCTYPE html>
<html lang="en">
<!-- first-child与first-of-type比较  -->
<head>
    <meta charset="UTF-8">
    <title>first-child与first-of-type比较</title>
    <style>
        .first-child p{
            background: green;
            color: #fff;
            padding: 10px
        }
        .first-child span{
            background: green;
            color: #fff;
            padding: 10px
        }
        .first-child p:first-child{
            background: orange;
        }
        .first-child span:first-child{
            background: orange;
        }

        .first-of-type p{
            background: green;
            color: #fff;
            padding: 10px
        }
        .first-of-type span{
            background: green;
            color: #fff;
            padding: 10px
        }
        .first-of-type p:first-of-type{
            background: orange;
        }
        .first-of-type span:first-of-type{
            background: orange;
        }
    </style>
</head>
<body>
    <h3>first-child演示</h3>
    <div class="first-child">
        <p>div中的第一个p元素</p>
        <span>div中第一个span元素</span>
        <p>div中第二个p元素</p>
        <span>div中第二个span元素</span>
    </div>
    <hr>

    <h3>first-of-type演示</h3>
    <div class="first-of-type">
        <p>div中的第一个p元素</p>
        <span>div中第一个span元素</span>
        <p>div中第二个p元素</p>
        <span>div中第二个span元素</span>
    </div>
    <hr>
    <h3>end</h3>
</body>
</html>

2330ce36ddfc4c89f0227126804455a46cb.jpg

in-range与out-of-range

:in-range伪类选择器,用于对元素绑定的值在指定范围限制内时具有范围限制的元素进行样式设置。换句话说,当它匹配元素的value属性值在其指定的范围限制内时,可以设置这些匹配元素的样式。

:out-of-range伪类选择器,用来指定当元素的有效值被限定在一段范围之内(使用min和max属性来指定范围),但实际输入值在该范围之外时使用的样式。

注意: :in-range伪类选择器和:out-of-range伪类选择器都是只作用于能指定区间值的元素;无法选择任何其他没有数据范围限制或不是表单控件元素的元素。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>in-range与out-of-range比较</title>
    <style>
        input {
            width: 100px;
            height: 40px;
            font-size: 1.4em;
        }

        input:in-range {
            background-color: lightgreen;
            color: green;
        }

        input:out-of-range {
            background-color: salmon;
            color: white;
        }

    </style>
</head>
<body>
    <div class="container">
        <p>值在1和10以内是,样式为绿色;但只要值在1和10之外,样式将是红色的。</p>
        <input id="range" type="number" min="1" max="10" value="12">
        <label for="range"></label>
    </div>
</body>
</html>

109379faf1836c4e5b1ea8c4a3f0494d471.jpg

4065def9448ff879487bce73e94146e3b7b.jpg

转载于:https://my.oschina.net/gain/blog/3079883

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值