0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CSSだけでポップアップを作る

Posted at

CSSだけでポップアップを簡単に作れたのでメモ。
ezgif.com-gif-maker (2).gif

仕組みを簡単に

まずポップアップのセクションを作成しておき、CSSでvisibility: hidden;として見えない状態にしておきます。
ボタンのaタグにhref="#popup"を設定し、ポップアップセクションにもid="popup"を仕込んでおきます。
ポップアップの方に
popup:target { visibility: visible; }
を設定すると、ボタンが押された時にポップアップが表示されるようになります。

コード

index.html
  <section class="tours">
    <!-- ボタン -->
    <a href="#popup" class="btn-green">More details</a>
  </section>
  <!-- ポップアップ -->
  <section class="popup" id="popup">
    <div class="popup__container">
      <h2>Lorem ipsum, dolor sit amet consectetur adipisicing elit.</h2>
      <p class="popup__text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Modi ab culpa laborum rem, voluptate repudiandae sunt qui porro nam quia quae fuga cupiditate repellendus quas asperiores autem officia et recusandae facilis aliquam nisi corporis voluptates impedit. Sed, eius illum nesciunt suscipit illo quas, recusandae id dolorem labore obcaecati nulla. Fugiat?</p>
      <!-- 閉じるボタン -->
      <a href="#tours" class="popup__close">&times;</a>
    </div>
  </section>
style.css
.popup {
  width: 100vw;
  height: 100vh;
  background: rgba(0,0,0,.8);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  transition: all .2s;

  visibility: hidden;
}
.popup__container {
  width: 70%;
  height: 60%;
  background: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 5rem;
}
.popup__container h2 {
  margin-bottom: 2rem;
}
.popup:target {
  visibility: visible;
}
.popup__close {
  position: absolute;
  top: 2rem;
  right: 2rem;
  color: black;
  text-decoration: none;
  font-size: 5rem;
  line-height: 1rem;
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?