モーダルウィンドウはポップアップに近く、ポップアップウィンドウはポップアップしている最中でも他のウィンドウを開くことが出来ますが、モーダルウィンドウは何らかの操作が完了するまで他の画面に遷移することが出来ない為、注意喚起などに役立ちますが、近年では、ユーザーの操作性が損なわれる可能性があることから、このモーダルウィンドウを使わないことが多くなっています。
今回は、このHTMLとCSSのコピペでできるモーダルウィンドウのデザイン2種についてご紹介します。
標準的なモーダル
ボタンをプッシュするとモーダルが表示される標準的な仕様です
HTMLとCSSのみで作っています。
コードを表示する
HTML
<center>
<a href="#modal-01" class="modal-button">
モーダルを表示
</a>
</center>
<div class="modal-wrapper" id="modal-01">
<a href="#!" class="modal-overlay"></a>
<div class="modal-window">
<div class="modal-content">
<p class="modal_title">モーダルの中身</p>
<p>
ああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああ
</p>
<p>
ああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああ
</p>
</div>
<a href="#!" class="modal-close"><i class="far fa-times-circle"></i></a>
</div>
</div>
CSS
.modal-wrapper {
z-index: 999;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
padding: 40px 10px;
text-align: center;
}
.modal-button {
color: #4f96f6;
background-color: #eeeeee;
font-weight: bold;
text-align: center;
cursor :pointer;
transition: all 0.3s;
display: block;
margin-top: 40px;
margin-bottom: 1px;
padding: 12px 2px;
max-width:300px;
text-decoration: none;
}
.modal-button:active {
/*ボタンを押したとき*/
-webkit-transform: translateY(2px);
transform: translateY(2px);/*下に動く*/
}
/*アイコンを表示*/
.modal-button:after {
font-family: "Font Awesome 5 Free";
content: "\f2d0";
padding-left: 8px;
}
/*ラベルホバー時*/
.modal-button:hover {
color: #FFFFFF;
background-color: #4f96f6;
transition: .6s;
}
.modal-wrapper:not(:target) {
opacity: 0;
visibility: hidden;
transition: opacity .3s, visibility .3s;
}
.modal-wrapper:target {
opacity: 1;
visibility: visible;
transition: opacity .4s, visibility .4s;
}
.modal-wrapper::after {
display: inline-block;
height: 100%;
margin-left: -.05em;
vertical-align: middle;
content: ""
}
.modal-wrapper .modal-window {
box-sizing: border-box;
display: inline-block;
z-index: 20;
position: relative;
width: 70%;
max-width: 600px;
padding: 10px 30px 25px;
border-radius: 2px;
background: #fff;
box-shadow: 0 0 30px rgba(0, 0, 0, .6);
vertical-align: middle
}
.modal-wrapper .modal-window .modal-content {
max-height: 80vh;
overflow-y: auto;
text-align: left
}
.modal_title {
font-size: 1.5em;
position: relative;
overflow: hidden;
padding: 0;
}
.modal_title::before,
.modal_title::after{
content: "";
position: absolute;
bottom: 0;
}
/* h2 プライマリカラー*/
.modal_title:before{
border-bottom: 4px solid #6bb6ff;
width: 100%;
}
/* h2 セカンダリカラー*/
.modal_title:after{
border-bottom: 4px solid #c8e4ff;
width: 100%;
}
.modal-content p {
margin: 10px 0 0 0;
}
.modal-overlay {
z-index: 10;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, .8)
}
.modal-wrapper .modal-close {
z-index: 20;
position: absolute;
top: 5px;
right: 5px;
width: 35px;
color: #95979c !important;
font-size: 30px;
font-weight: 700;
line-height: 35px;
text-align: center;
text-decoration: none;
text-indent: 0
}
.modal-wrapper .modal-close:hover {
color: #2b2e38 !important
}
上から落ちてくるモーダル
ボタンをプッシュすると上からモーダルが落ちてくるように表示されるです。
モーダルウィンドウの表示位置はサイトによって指定してお使いください。
コードを表示する
HTML
<center>
<a href="#modal-01" class="modal-button">
モーダルを表示
</a>
</center>
<div class="modal-wrapper" id="modal-01">
<a href="#!" class="modal-overlay"></a>
<div class="modal-window">
<div class="modal-content">
<p class="modal_title">モーダルの中身</p>
<p>
ああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああ
</p>
<p>
ああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああああ
</p>
</div>
<a href="#!" class="modal-close"><i class="far fa-times-circle"></i></a>
</div>
</div>
CSS
/*--------------------------------------
モーダル表示上から_002
--------------------------------------*/
.modal_wrap input {
display: none;
}
.modal_overlay {
display: flex;
justify-content: center;
overflow: auto;
position: fixed;
top: 0;
left: 0;
z-index: 9999;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
opacity: 0;
transition: opacity 0.5s, transform 0s 0.5s;
transform: scale(0);
}
.modal_trigger {
position: absolute;
width: 100%;
height: 100%;
}
.modal_content {
align-self: flex-start;
width: 60%;
padding: 30px 30px 15px;
box-sizing: border-box;
background: #fff;
line-height: 1.4em;
top: 20%;
transform: translateY(-60%);
transition: 0.5s;
}
.modal_content p {
padding-top: 0;
}
.close_button {
position: absolute;
top: 14px;
right: 16px;
font-size: 24px;
cursor: pointer;
}
.modal_wrap input:checked ~ .modal_overlay {
opacity: 1;
transform: scale(1);
transition: opacity 0.5s;
}
.modal_wrap input:checked ~ .modal_overlay .modal_content {
transform: translateY(20px);
}
.open_button {
color: #4f96f6;
background-color: #eeeeee;
font-weight: bold;
text-align: center;
cursor :pointer;
transition: all 0.3s;
display: block;
margin-top: 40px;
margin-bottom: 1px;
padding: 12px 2px;
max-width:300px;
text-decoration: none;
}
.open-button:active {
/*ボタンを押したとき*/
-webkit-transform: translateY(2px);
transform: translateY(2px);/*下に動く*/
}
/*アイコンを表示*/
.open-button:after {
font-family: "Font Awesome 5 Free";
content: "\f2d0";
padding-left: 8px;
}
/*ラベルホバー時*/
.open-button:hover {
color: #FFFFFF;
background-color: #4f96f6;
transition: .6s;
}
.modal_title2 {
font-size: 1.5em;
position: relative;
overflow: hidden;
padding-bottom: 10px;
margin-top:0;
margin-bottom: 0;
}
.modal_title2::before,
.modal_title2::after{
content: "";
position: absolute;
bottom: 0;
}
/* h2 プライマリカラー*/
.modal_title2:before{
border-bottom: 4px solid #6bb6ff;
width: 100%;
}
/* h2 セカンダリカラー*/
.modal_title2:after{
border-bottom: 4px solid #c8e4ff;
width: 100%;
}