Skip to content

用CSS实现根据元素数量控制样式 #29

Description

@OPY-bbt
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
        ul li:only-child {
            color: red;
        }

        ul li:first-child:nth-last-child(2),
        ul li:first-child:nth-last-child(2) ~ li {
            color: green;
        }

        ul li:first-child:nth-last-child(3),
        ul li:first-child:nth-last-child(3) ~ li {
            color: blue;
        }
        
    </style>
  </head>
  <body>
    <ul>
      <li>1.1</li>
    </ul>

    <ul>
      <li>2.1</li>
      <li>2.2</li>
    </ul>

    <ul>
      <li>3.1</li>
      <li>3.2</li>
      <li>3.3</li>
    </ul>
  </body>
</html>

refer

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions