代码之家  ›  专栏  ›  技术社区  ›  Frankey

:not()CSS伪类未按预期工作

  •  0
  • Frankey  · 技术社区  · 10 月前

    一个简单的,为什么

    :not(.theme) small{
      font-size: 40px;
    }
    
    :not(.theme *) small{
      text-decoration: underline;
    }
    
    *:not(.theme *) small{
      font-weight: bold;
    }
    

    正在对所有人生效 small html元素? 我的假设是,只有 小的 不是类父级的后代的元素 theme 受到影响。

    同样适用于

    :not(.theme *) .small-class{
      text-transform: capitalize;
    }
    

    影响所有小班元素。

    body {
      background: #6e28d9;
      padding: 0 24px;
      color: white; /* Change my color to yellow */
      margin: 0;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      gap:20px;
    }
    
    .theme small {
      color: black;
    }
    
    :not(.theme) small{
      font-size: 40px;
    }
    
    :not(.theme *) small{
      text-decoration: underline;
    }
    
    *:not(.theme *) small{
      font-weight: bold;
    }
    
    small:not(.theme *){
      letter-spacing: 10px;
    }
    
    :not(.theme *) .small-class{
      text-transform: capitalize;
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="src/style.css">
      </head>
      <body>
    
        <small>pure small</small>
    
       <div class="theme">
           <small>small with theme parent</small>
       </div>
    
        <div class="theme">
           <small class="small-class">small-class with theme parent</small>
       </div>
    
       <div>
           <small class="small-class">small-class with theme parent</small>
       </div>
    
      </body>
    </html>

    事实上,有人可能对如何在 小的 不是任何类父级的继承项的元素 主题

    这是渲染结果的一小部分 Rendered result

    1 回复  |  直到 10 月前
        1
  •  1
  •   Ry- Vincenzo Alcamo    10 月前

    我的假设是,只有不是类父级后代的小元素 theme 受到影响。

    如果这是你想要的,那就去做

    small:not(.theme *) {
      /* … */
    }
    

    <small> 不是a的后代的元素 .theme .

    :not(.theme *) small 匹配全部 small 匹配的任何元素的后代元素 :not(.theme *) ,这是所有的,因为根 <html> 元素是必然的 :不是(.theme*) .