为什么first-child无效,在其他文件单独测试没问题,这是什么原因


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            padding: 0px;
            margin: 0px;
            box-sizing: border-box;
        }

        ol {
            list-style: none;
            display: flex;
            flex-wrap: nowrap;
            background-color: aqua;
            
        }

        .one a{
            white-space: nowrap;
        }

       ol:first-child{
            flex-grow: 1;
            flex-wrap: wrap;
            background-color: blueviolet;
        }

        body {
            display: flex;
            justify-content: center;
        }

        main {
            display: flex;
            width: 80vw;
           
            background-color: antiquewhite;
        }
        div:first-child{
            background-color: red;
        }
    </style>
</head>

<body>
    <main>
        <a href="">logo</a>
        <ol class="one">
            <li><a href="">奔驰</a></li>
            <li><a href="">奥迪</a></li>
            <li><a href="">雷克萨斯</a></li>
            <li><a href="">宝马</a></li>
        </ol>
        <ol>
            <li><a href="">购买</a></li>
            <li><a href="">注册</a></li>
        </ol>
        <div>1</div>
        <div>1</div>
        <div>1</div>
        <div>1</div>
    </main>
</body>

</html>

你把a标签删除试下

这样写能行就怪了

  <div class="boxName">
         <div>1</div>
        <div>1</div>
        <div>1</div>
        <div>1</div>
  </div>
<style>
  .boxName div:first-child{
    xxxxxxxxxxxxxxxxx
  }
</style>
  • 你可以参考下这篇文章:有关 :first-child的理解
  • 除此之外, 这篇博客: nth选择器详解中的 1. :first-child 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 代码:

    .box1 p:first-child{
    	background-color: blue;
    }
    

    结果:
    在这里插入图片描述
    last-child与它相反,选择的是最后一个