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?

[スニペット]HTML&CSS&JSポップアップ

Last updated at Posted at 2025-04-01
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ポップアップ表示</title>
  <link rel="stylesheet" href="style.css" />
</head>
<body>
  <button id="openPopup">ポップアップを開く</button>

  <div id="popupOverlay" class="overlay">
    <div class="popup">
      <h2>ポップアップ画面</h2>
      <p>これは簡単なポップアップ画面です。</p>
      <button id="closePopup">閉じる</button>
    </div>
  </div>

  <script src="script.js"></script>
</body>
</html>
body {
  font-family: Arial, sans-serif;
}

button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
}

/* オーバーレイ */
.overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  justify-content: center;
  align-items: center;
}

/* ポップアップウィンドウ */
.popup {
  background: white;
  padding: 20px;
  border-radius: 8px;
  width: 300px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}

h2 {
  margin-top: 0;
}
const openPopupButton = document.getElementById('openPopup');
const closePopupButton = document.getElementById('closePopup');
const popupOverlay = document.getElementById('popupOverlay');

openPopupButton.addEventListener('click', () => {
  popupOverlay.style.display = 'flex';
});

closePopupButton.addEventListener('click', () => {
  popupOverlay.style.display = 'none';
});

ポップアップに装飾を付ける

body {
  font-family: 'Helvetica Neue', sans-serif;
  background: #f5f5f5;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.open-btn {
  padding: 12px 24px;
  background: #007bff;
  color: white;
  font-size: 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0, 123, 255, 0.2);
  transition: background 0.3s ease;
}

.open-btn:hover {
  background: #0056b3;
}

.overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.6);
  justify-content: center;
  align-items: center;
  animation: fadeIn 0.3s ease forwards;
}

.popup {
  background: white;
  padding: 30px;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  position: relative;
  max-width: 90%;
  width: 400px;
  animation: slideUp 0.3s ease;
  text-align: center;
}

h2 {
  margin-top: 0;
  font-size: 24px;
  color: #333;
}

p {
  color: #555;
  margin-bottom: 0;
}

.close-btn {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 28px;
  color: #888;
  cursor: pointer;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #333;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from {
    transform: translateY(30px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
<div id="popupOverlay" class="overlay">
  <div class="popup">
    <div class="popup-header">
      <h2>お知らせ</h2>
    </div>
    <div class="popup-body">
      <p>これはスタイリッシュなポップアップ画面です。ご確認ください。</p>
    </div>
    <button id="closePopup" class="close-btn">&times;</button>
  </div>
</div>
.popup {
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  position: relative;
  max-width: 90%;
  width: 400px;
  overflow: hidden; /* ← ヘッダーの角丸を維持するために追加 */
  animation: slideUp 0.3s ease;
}

/* ヘッダー部分に色をつける */
.popup-header {
  background: #007bff;
  color: white;
  padding: 16px;
  text-align: center;
}

/* 本文部分を分離 */
.popup-body {
  padding: 24px;
  text-align: center;
  color: #555;
}

/* 既存の h2 スタイルは不要になる場合も */
.popup-header h2 {
  margin: 0;
  font-size: 20px;
}

/* close ボタンは上部の右端に置く */
.close-btn {
  position: absolute;
  top: 10px;
  right: 16px;
  background: none;
  border: none;
  font-size: 28px;
  color: #fff;
  cursor: pointer;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #ddd;
}
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?