グラデーション背景と半透明オーバーレイ付きモーダルウインドウの作り方【HTML/CSS/JavaScript・サンプルコード付き】
html/css/js
グラデーション背景と半透明オーバーレイ付きモーダルウインドウの作り方
HTML・CSS・JavaScriptを使って、グラデーション背景と半透明オーバーレイを組み合わせたモーダルウインドウを作る方法を紹介します。このサンプルでは、ボタンをクリックすると半透明のオーバーレイを表示し、グラデーション背景のモーダルウインドウを中央に表示できます。お知らせ表示、確認画面、キャンペーン案内など、デザイン性のあるモーダルUIを作りたい場合に利用できます。
コードについて
本記事のコードはサンプルコードです。ご利用前に必ず動作確認を行ってください。
免責事項
本コードの利用により発生した損害について、当サイトは一切の責任を負いません。
デモ
グラデーション背景付きモーダルウインドウ
半透明オーバーレイとグラデーション背景を組み合わせたモーダルです。
お知らせ
グラデーション背景と半透明オーバーレイを組み合わせた、デザイン性のあるモーダルウインドウです。
コードをコピーして使おう!
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コードを指定 -->
<meta charset="UTF-8">
<!-- スマホ表示に対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>グラデーション背景と半透明オーバーレイ付きモーダルウインドウ</title>
<!-- CSSファイルを読み込み -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- デモ全体を囲むエリア -->
<div class="gradient-overlay-modal-window_wrap">
<!-- 見出し -->
<div class="gradient-overlay-modal-window_title">
グラデーション背景付きモーダルウインドウ
</div>
<!-- 説明文 -->
<p class="gradient-overlay-modal-window_text">
半透明オーバーレイとグラデーション背景を組み合わせたモーダルです。
</p>
<!-- モーダルを開くボタン -->
<button class="gradient-overlay-modal-window_open" type="button" data-modal-open>
モーダルを開く
</button>
</div>
<!-- 半透明オーバーレイ -->
<div class="gradient-overlay-modal-window_overlay" data-modal-overlay>
<!-- モーダル本体 -->
<div class="gradient-overlay-modal-window_modal" role="dialog" aria-modal="true">
<!-- 見出し -->
<div class="gradient-overlay-modal-window_modal-title">
お知らせ
</div>
<!-- 説明文 -->
<p class="gradient-overlay-modal-window_modal-text">
グラデーション背景と半透明オーバーレイを組み合わせた、デザイン性のあるモーダルウインドウです。
</p>
<!-- 閉じるボタン -->
<button class="gradient-overlay-modal-window_close" type="button" data-modal-close>
閉じる
</button>
</div>
</div>
<!-- JavaScriptファイルを読み込み -->
<script src="script.js"></script>
</body>
</html>
/* ページ全体の設定 */
body{
margin:0;
font-family:sans-serif;
background:#ffffff;
}
/* デモ全体を囲むエリア */
.gradient-overlay-modal-window_wrap{
max-width:700px;
margin:40px auto;
padding:32px;
border:1px solid #e5e7eb;
border-radius:18px;
background:#f8fafc;
text-align:center;
}
/* タイトル */
.gradient-overlay-modal-window_title{
font-size:22px;
font-weight:700;
margin-bottom:12px;
}
/* 説明文 */
.gradient-overlay-modal-window_text{
margin-bottom:24px;
color:#4b5563;
}
/* モーダルを開くボタン */
.gradient-overlay-modal-window_open{
padding:12px 24px;
border:none;
border-radius:8px;
background:#0b6bff;
color:#ffffff;
font-weight:700;
cursor:pointer;
}
/* 半透明オーバーレイ */
.gradient-overlay-modal-window_overlay{
display:none;
position:fixed;
inset:0;
z-index:9999;
background:rgba(15,23,42,.45);
align-items:center;
justify-content:center;
padding:20px;
}
/* モーダル表示時 */
.gradient-overlay-modal-window_overlay.is-active{
display:flex;
}
/* モーダル本体 */
.gradient-overlay-modal-window_modal{
width:100%;
max-width:520px;
padding:32px;
border-radius:18px;
color:#ffffff;
background:linear-gradient(135deg,#2563eb,#7c3aed);
box-sizing:border-box;
text-align:left;
box-shadow:0 20px 40px rgba(0,0,0,.25);
animation:gradient-overlay-modal-window_show .25s ease;
}
/* モーダルの見出し */
.gradient-overlay-modal-window_modal-title{
font-size:24px;
font-weight:700;
margin-bottom:12px;
}
/* モーダル内の説明文 */
.gradient-overlay-modal-window_modal-text{
margin-bottom:24px;
line-height:1.8;
opacity:.9;
}
/* 閉じるボタン */
.gradient-overlay-modal-window_close{
padding:10px 20px;
border:1px solid rgba(255,255,255,.4);
border-radius:8px;
background:rgba(255,255,255,.18);
color:#ffffff;
font-weight:700;
cursor:pointer;
}
/* モーダル表示アニメーション */
@keyframes gradient-overlay-modal-window_show{
from{
opacity:0;
transform:translateY(12px);
}
to{
opacity:1;
transform:translateY(0);
}
}
// モーダルを開くボタンを取得
const modalOpen=document.querySelector('[data-modal-open]');
// 半透明オーバーレイを取得
const modalOverlay=document.querySelector('[data-modal-overlay]');
// モーダルを閉じるボタンを取得
const modalClose=document.querySelector('[data-modal-close]');
// モーダルを開くボタンをクリックしたらモーダルを表示
modalOpen.addEventListener('click',function(){
modalOverlay.classList.add('is-active');
});
// 閉じるボタンをクリックしたらモーダルを閉じる
modalClose.addEventListener('click',function(){
modalOverlay.classList.remove('is-active');
});
// 背景部分をクリックしたらモーダルを閉じる
modalOverlay.addEventListener('click',function(event){
if(event.target===modalOverlay){
modalOverlay.classList.remove('is-active');
}
});
ファイル構成と設置方法
このサンプルでは、HTML・CSS・JavaScriptをそれぞれ別ファイルに分けて使用します。index.html、style.css、script.js を同じフォルダに配置してください。
sample/
├── index.html
├── style.css
└── script.js
index.html をブラウザで開くと、半透明オーバーレイとグラデーション背景を組み合わせたモーダルウインドウを確認できます。
グラデーション背景と半透明オーバーレイ付きモーダルウインドウの作り方【HTML/CSS/JavaScript・サンプルコード付き】
コメント