CSS3 nth-child 当在div中指定class的时候失效 求解

查看:181
本文介绍了CSS3 nth-child 当在div中指定class的时候失效 求解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .aa{background:blue;color:#fff;}
        .aa:nth-child(1){background:red;}
    </style>
</head>
<body>
    <div>1</div>
    <div class="aa">1</div>
    <div class="aa">1</div>
    <div class="aa">1</div>
    <div>1</div>
</body>
</html>

如上所示 我想要的是 第一个.aa变成红色 然而当所有标签都是div的时候 nth-child first-child都失效了
这个怎么破?

解决方案

类名 === 'aa' && 是body的第一个child

这样显然一个符合的节点都木有呀。

你可以改成这样:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .aa{background:blue;color:#fff;}
        .aa:nth-child(1){background:red;}
    </style>
</head>
<body>
    <div>1</div>
    <div>    
        <div class="aa">1</div>
        <div class="aa">1</div>
        <div class="aa">1</div>
    </div>
    <div>1</div>
</body>
</html>

这篇关于CSS3 nth-child 当在div中指定class的时候失效 求解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆