[CSS] CSS nesting

Browser support: https://caniuse.com/?search=css%20nesting

 

Example 1:

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      ul {
        border: 3px black solid;
        li {
          background-color: red;
        }
      }
    </style>
  </head>
  <body>
    <h2>CSS Nesting (CSS嵌套)</h2>
    <ul>
      <li>111</li>
      <li>222</li>
      <li>333</li>
    </ul>
    <ol>
      <li>aaa</li>
      <li>bbb</li>
      <li>ccc</li>
    </ol>
  </body>
</html> 
 
Example 2: with :hover
<!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>
      ul {
        border: 3px black solid;
        &:hover {
          border: 3px yellow solid;
        }
        li {
          background-color: red;
        }
      }
    </style>
  </head>
  <body>
    <h2>CSS Nesting (CSS嵌套)</h2>
    <ul>
      <li>111</li>
      <li>222</li>
      <li>333</li>
    </ul>
    <ol>
      <li>aaa</li>
      <li>bbb</li>
      <li>ccc</li>
    </ol>
  </body>
</html>

 

posted @ 2025-05-11 15:02  Zhentiw  阅读(11)  评论(0)    收藏  举报