Skip to content

Instantly share code, notes, and snippets.

@yaakaito
Last active January 19, 2024 16:23
Show Gist options
  • Save yaakaito/6ec591eaec9c11e1c1633a5afa1aabdf to your computer and use it in GitHub Desktop.
Save yaakaito/6ec591eaec9c11e1c1633a5afa1aabdf to your computer and use it in GitHub Desktop.
:has() でリストが空のときにセクションごと消す

:has() が 2023 年末にすべてのブラウザでサポートされて、現実的に利用できるようになった。

「渡したセレクターにマッチする子要素を持つ親要素であるか」を判定できるので、例えばリストが空のときにセクションごと消すことができる。

<style>
section:has(ul:empty) {
  display: none;
}
</style>
<section class="section">
  <h1>section1</h1>
  <ul>
    <li>item1</li>
    <li>item2</li>
  </ul>
</section>
<section class="section">
  <h1>section2</h1>
  <ul>
  </ul>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment