爱编程的设计师 2022-03-17 19:57 采纳率: 94.4%
浏览 18
已结题

nth-child指定元素的问题,和指定的不一致

理论上应该是第六个变为黄色 但是实际是第二十黄色 这个是什么情况,下面的nth-of-type正常
.mather .son span:nth-child(6){
        background-color: yellow;
    }
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="">

    <style>
        
        *{
            margin: 0;
            padding: 0;
        }
        a{
            text-decoration: none;
        }
        ul,ol{
            list-style: none;
        }
        body{
            width: 700px;
            margin: auto;
            margin-top: 50px;
        }
        .father{
            height: 500px;
            width: 300px;
            background-color: aquamarine;
            margin: auto;
            float: left;
        }
        .mather{
            height: 500px;
            width: 380px;
            background-color: aquamarine;
            margin: auto;
            float:right;
        }
        .one,.two,.three,.four{
            height: 25px;
            width: 25px;
            background-color: brown;
            margin-bottom: 5px;
            /* float: left; */
        }
        .father .son .three{
            background-color: blue;
        }
        .father .oldSon div+div{
            background-color: yellow;
        }
        .father .daughter div:nth-child(odd){
            background-color: red;
        }
        .mather .son span:nth-child(6){
            background-color: yellow;
        }
        /* .mather .son span:nth-of-type(6){
            background-color: yellow;
        } */
        .sonSpan{
            display: inline-block;
            height: 25px;
            width: 25px;
            background-color: brown;
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="child son">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
        </div>
        <div class="child oldSon">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
        </div>
        <div class="child daughter">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
        </div>
        <div class="child oldDaughter">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
        </div>
    </div>
    <div class="mather">
        <div class="child son">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
            <span class="sonSpan A">A</span>
            <span class="sonSpan B">B</span>
            <span class="sonSpan C">C</span>
            <span class="sonSpan D">D</span>
            <span class="sonSpan E">E</span>
            <div class="sonSpan F">F</div>
            <span class="sonSpan G">G</span>
            <span class="sonSpan H">H</span>
            <span class="sonSpan H">I</span>
            <span class="sonSpan H">G</span>
            <span class="sonSpan H">K</span>
            <span class="sonSpan H">M</span>
        </div>
        <div class="child oldSon">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
        </div>
        <div class="child daughter">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
        </div>
        <div class="child oldDaughter">
            <div class="one"></div>
            <div class="two"></div>
            <div class="three"></div>
            <div class="four"></div>
        </div>
    </div>
</body>
</html>

  • 写回答

2条回答 默认 最新

  • 你好!机器人 2022-03-17 21:22
    关注

    nth-child这个指的是子级的第几个元素

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月26日
  • 已采纳回答 3月18日
  • 创建了问题 3月17日

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗