LoginSignup
5
4

More than 5 years have passed since last update.

cssで特定の要素以外を非表示にする

Last updated at Posted at 2018-11-09

概要

以下のようなHTMLで

<div class="main">
  <div class="deactive1"></div>
  <div class="deactive2"></div>
  <div class="deactive3"></div>
  <div class="deactive4"></div>
  <div class="deactive5"></div>
  <!-- 上の全ての要素を非表示にしたい -->
  <div id="app">
    vueとかで作ったアプリケーション
  </div>
</div>

.main内の#app以外の要素を非表示にしたいが、classもidもめちゃめちゃでセレクターで指定できない時があります。

そこで、not擬似クラスを使用して#app以外の要素にdisplay: noneを適用するcssを書いてみます

実装

.main>*:not(#app) {
    display: none;
}

これで以下のような感じになるはずです!:relaxed:

2.png

まとめ

意外にググっても出てこなかったのでまとめてみました。

SSIなどでHTMLがサーバーで自動挿入されるような場面で使えるのではないでしょうか?(実際に僕はそうでした:thinking:)

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4