コピペでできる!cssとhtmlのみで作るモーダルウィンドウ

CSS HTML
 2018.02.09
 2018.07.09

ちょっとした情報なら、ページ移管させずにモーダルウィンドウで表現するのもアリですよね。
cssとhtmlのみで作れるモーダルウィンドウです。

browser:  65 11 20 11 

cssとhtmlのみで作るモーダルウィンドウ

ボタンを押すと中心からモーダルウィンドウが開く

ニャン易度
<div class="cp_modal02">
<input type="checkbox" id="cp_modal" />
<h1>h1テキスト</h1>
<p>テキスト</p>
<label for="cp_modal" class="cp_modal_btn"></label>
<label for="cp_modal" class="cp_modal_bg"></label>
<div class="cp_modal_cont">
<label for="cp_modal" class="cp_close"><i class="fa fa-times" aria-hidden="true"></i></label>
<header>
<h2>h2テキスト</h2>
</header>
<article class="cp_content">
<p>テキスト</p>
<p class="citation">テキスト</p>
</article>
<footer><label for="cp_modal" class="cp_btn cp_cl">閉じる</label></footer>
</div>
</div>
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
*, *::after, *::before {
box-sizing: inherit;
}
*, *:before, *:after {
box-sizing: border-box;
outline: none;
}
.cp_modal02 {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 100vh;
font-size: 16px;
font-weight: 300;
line-height: 1.5;
color: #444444;
}
.cp_modal02:before {
position: absolute;
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.7);
z-index: -1;
}
.cp_modal02 .cp_content p {
margin: 0;
line-height: 1.5;
text-indent: 1em;
}
.cp_modal02 p.citation {
text-align: right;
}
.cp_modal02 p.citation::before {
content: '\2500'
}
.cp_modal02 p:not(.citation) {
margin-bottom: 1.5rem;
}
.cp_modal02 a {
text-decoration: none;
}
.cp_modal02 label {
cursor: pointer;
}
.cp_modal02 .cp_modal_btn {
position: relative;
display: table-cell;
width: 100px;
height: 100px;
background-color: #00bcd4;
box-shadow: 0 0 40px rgba(0, 0, 0, 0.3);
border-radius: 50%;
text-align: center;
transition: box-shadow 250ms ease;
color: #ffffff;
}
.cp_modal02 .cp_modal_btn::before {
position: absolute;
top:50%;
left: 20%;
font-family: 'FontAwesome';
content: '\f0a6' ' よむ';
font-size: 1em;
line-height: 0;
display: inline-block;
width: 4em;
color: #ffffff;
}
.cp_modal02 .cp_modal_btn:hover {
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}
.cp_modal02 .cp_modal_bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
z-index: 10;
visibility: hidden;
transition: background-color 250ms linear;
}
.cp_modal02 .cp_modal_cont {
position: absolute;
top: 45%;
left: 50%;
width: 50%;
height: 50%;
overflow-y: scroll;
margin-top: -18%;
margin-left: -25%;
padding: 30px;
background-color: white;
border-radius: 4px;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
transform: scale(0);
transition: transform 250ms ease;
visibility: hidden;
z-index: 20;
}
.cp_modal02 .cp_modal_cont .cp_close {
position: relative;
float: right;
font-size: 18px;
transition: transform 500ms ease;
z-index: 11;
}
.cp_modal02 .cp_modal_cont .cp_close:hover {
color: #da3c41;
transform: rotate(180deg);
}
.cp_modal02 .cp_modal_cont header {
position: relative;
display: block;
border-bottom: 1px solid #00bcd4;
margin-bottom: 1.5em;
}
.cp_modal02 .cp_modal_cont header h2 {
margin: 0 0 10px;
padding: 0;
font-size: 28px;
}
.cp_modal02 .cp_modal_cont article {
position: relative;
display: block;
margin: 0;
padding: 0;
font-size: 16px;
line-height: 1.75;
}
.cp_modal02 .cp_modal_cont footer {
position: relative;
display: flex;
align-items: center;
justify-content: flex-end;
width: 100%;
margin: 0;
padding: 10px 0 0;
}
.cp_modal02 .cp_modal_cont footer .cp_btn {
position: relative;
padding: 10px 30px;
border-radius: 3px;
font-size: 14px;
font-weight: 400;
color: white;
text-transform: uppercase;
overflow: hidden;
}
.cp_modal02 .cp_modal_cont footer .cp_btn:before {
position: absolute;
content: '';
top: 0;
left: 0;
width: 0;
height: 100%;
background-color: rgba(88, 24, 26, 0.3);
transition: width 250ms ease;
z-index: 0;
}
.cp_modal02 .cp_modal_cont footer .cp_btn:hover:before {
width: 100%;
}
.cp_modal02 .cp_modal_cont footer .cp_btn.cp_cl {
background-color: #da3c41;
}
.cp_modal02 #cp_modal {
display: none;
}
.cp_modal02 #cp_modal:checked ~ .cp_modal_bg {
visibility: visible;
background-color: #000000;
opacity: 0.7;
transition: background-color 250ms linear;
}
.cp_modal02 #cp_modal:checked ~ .cp_modal_cont {
visibility: visible;
transform: scale(1);
transition: transform 250ms ease;
z-index: 111;
}
@media only screen and (max-width: 480px) {
.cp_modal02 .cp_modal_cont{
top: 30%;
left: 30%;
width: 90%;
}
}

モーダルウィンドウの中身がスライド

ニャン易度
<div class="cp_modal05">
<label for="cp_controller" class="cp_btn">open modal</label>
<div class="cp_modal">
<input type="checkbox" id="cp_controller" class="cp_open" hidden>
<div class="cp_modal_wrap">
<label for="cp_controller" class="cp_overlay"></label>
<div class="cp_modal_cont">
<label for="cp_controller" class="cp_close"></label>
<input type="radio" name="nav" id="cp_slide-1" class="cp_radio" checked hidden/>
<input type="radio" name="nav" id="cp_slide-2" class="cp_radio" hidden/>
<input type="radio" name="nav" id="cp_slide-3" class="cp_radio" hidden/>
<div class="cp_modal_slide content-1">
<div class="cp_modal_item">
<h2>テキスト - Slide 1</h2>
<p>テキスト</p>
<div class="cp_nav">
<label for="cp_slide-2" class="cp_next"></label>
</div>
</div>
</div>
<div class="cp_modal_slide content-2">
<div class="cp_modal_item">
<h2>テキスト - Slide 2</h2>
<p>テキスト</p>
<div class="cp_nav">
<label for="cp_slide-1" class="cp_prev"></label>
<label for="cp_slide-3" class="cp_next"></label>
</div>
</div>
</div>
<div class="cp_modal_slide content-3">
<div class="cp_modal_item">
<h2>テキスト - Slide 3</h2>
<p>テキスト</p>
<p class="citation">テキスト</p>
<div class="cp_nav">
<label for="cp_slide-2" class="cp_prev"></label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
.cp_modal05 {
font-size: 1rem;
box-sizing: border-box;
width: 100%;
height: 100vh;
display: flex;
margin: 0;
padding: 0;
text-align: center;
color: #333333;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
}
.cp_modal05 #cp_controller {
position: absolute;
left: -999em;
opacity: 0;
}
.cp_modal05 .cp_open:checked ~ .cp_modal_wrap {
display: block;
}
.cp_modal05 .cp_open:checked ~ .cp_modal_wrap:before,
.cp_modal05 .cp_open:checked ~ .cp_modal_wrap .cp_overlay {
display: block;
}
.cp_modal05 .cp_close {
font-size: 1.5em;
position: absolute;
z-index: 100;
top: 0;
right: 0.5em;
cursor: pointer;
}
.cp_modal05 .cp_close::before {
font-family: 'FontAwesome';
content: '\f00d';
}
.cp_modal05 .cp_close:hover::before {
color: #00bcd4;
}
.cp_modal05 .cp_modal_wrap {
display: none;
}
.cp_modal05 .cp_modal_wrap:before {
position: fixed;
z-index: 101;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: none;
content: '';
background: rgba(27,37,56,0.8);
}
.cp_modal05 .cp_overlay {
position: fixed;
z-index: 102;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: none;
}
.cp_modal05 .cp_modal_cont {
position: absolute;
z-index: 103;
top: 50%;
left: 50%;
overflow: hidden;
width: 80%;
max-width: 90%;
max-height: 90%;
padding: 15px 0;
transform: translate(-50%, -50%);
text-align: center;
border-radius: 0.25em;
background: #ffffff;
}
.cp_modal05 .cp_modal_cont p {
margin: 0;
line-height: 1.8;
text-indent: 1em;
text-align: left;
padding: 0 2.5em;
}
.cp_modal05 .cp_modal_cont p.citation {
text-align: right;
}
.cp_modal05 .cp_modal_cont p.citation::before {
content: '\2500'
}
.cp_modal05 .cp_modal_cont p:not(.citation) {
margin-bottom: 1.5rem;
}
.cp_modal05 .cp_modal_item {
position: relative;
margin: 0 1em;
padding: 3em 0 1em 0;
}
.cp_modal05 .cp_modal_slide {
position: absolute;
display: flex;
width: 100%;
transition: all 0.4s ease-in;
}
.cp_modal05 .cp_slide-2 {
left: 100%;
}
.cp_modal05 .cp_slide-3 {
left: 200%;
}
.cp_modal05 #cp_slide-1:checked ~ .content-1 {
position: relative;
left: 0;
overflow: auto;
}
.cp_modal05 #cp_slide-1:checked ~ .content-2 {
left: 100%;
}
.cp_modal05 #cp_slide-2:checked ~ .content-1 {
left: -100%;
}
.cp_modal05 #cp_slide-2:checked ~ .content-2 {
position: relative;
left: 0;
overflow: auto;
}
.cp_modal05 #cp_slide-2:checked ~ .content-3 {
left: 100%;
}
.cp_modal05 #cp_slide-3:checked ~ .content-1 {
left: -200%;
}
.cp_modal05 #cp_slide-3:checked ~ .content-2 {
left: -100%;
}
.cp_modal05 #cp_slide-3:checked ~ .content-3 {
position: relative;
left: 0;
overflow: auto;
}
.cp_modal05 .cp_nav {
position: absolute;
top: 50%;
width: 100%;
}
.cp_modal05 .cp_nav label {
line-height: 0.5em;
position: absolute;
display: inline-block;
width: 10px;
height: 10px;
padding: 0.5em;
cursor: pointer;
color: #ffffff;
border-radius: 0.3em;
background: #00bcd4;
}
.cp_modal05 .cp_nav label.cp_prev {
left: 0;
}
.cp_modal05 .cp_nav label.cp_prev::before {
font-family: 'FontAwesome';
content: '\f100';
}
.cp_modal05 .cp_nav label.cp_next {
right: 0;
}
.cp_modal05 .cp_nav label.cp_next::before {
font-family: 'FontAwesome';
content: '\f101';
}
.cp_modal05 .cp_btn {
position: relative;
display: inline-block;
min-width: 60px;
padding: 8px 15px;
cursor: pointer;
text-align: center;
text-decoration: none;
border: #da3c41 solid 1px;
border-radius: 3px;
background: #ffffff;
}
@media only screen and (max-width: 767px) {
.cp_modal05 .cp_modal_cont {
width: 100%;
max-width: 100%;
border-radius: 0;
}
}

ボタンを押すと中心からバウンドしながら画面が現れるモーダルウィンドウ

ニャン易度
<div class="cp_modal04">
<div class="cp_modal">
<input id="cp_trigger" type="checkbox" />
<label for="cp_trigger">Launch Modal</label>
<div class="cp_overlay" role="dialog">
<div class="cp_wrap">
<label for="cp_trigger">✖</label>
<h2>h2テキスト</h2>
<p>テキスト</p>
<p class="citation">テキスト</p>
<label for="cp_trigger" class="cp_btn">閉じる</label>
</div>
</div>
</div>
</div>
.cp_modal04 {
position: fixed;
top: 0;
left: 0;
display: -webkit-box;
display: flex;
-webkit-box-sizing: border-box;
box-sizing: border-box;
font-size: 1rem;
width: 100%;
height: 100vh;
margin: 0;
padding: 0;
background: #cccccc;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
}
.cp_modal04 h2 {
font-size: 1.6em;
line-height: 1.2;
margin: 1.414em 0 0.5em;
}
.cp_modal04 rt,.cp_modal04 rp {
font-size: 30%;
}
.cp_modal04 .cp_modal {
display: inline-block;
padding: 1em;
}
@media (min-width: 43.75em) {
.cp_modal04 .cp_modal {
padding: 1.5em;
}
}
.cp_modal04 .cp_modal > label {
font-weight: bold;
display: inline-block;
padding: 0.75em 1.5em;
cursor: pointer;
-webkit-transition: all 0.55s;
transition: all 0.55s;
color: #ffffff;
border: 1px solid #00bcd4;
border-radius: 0.2em;
background: #00bcd4;
text-shadow: 0px 1px 1px #333333;
}
.cp_modal04 .cp_modal > label:hover {
-webkit-transform: scale(0.97);
transform: scale(0.97);
}
.cp_modal04 .cp_overlay {
position: fixed;
z-index: 600;
top: 0;
right: 0;
bottom: 0;
left: 0;
text-align: center;
background: rgba(27,37,56,1);
text-shadow: none;
}
.cp_modal04 .cp_wrap {
position: relative;
overflow-y: auto;
width: 90%;
height: 80%;
margin: 0 auto;
padding: 1em 1em;
background: rgba(27,37,56,1);
}
@media (min-width: 50em) {
.cp_modal04 .cp_wrap {
padding: 1.75em;
}
}
@media (min-height: 37.5em) {
.cp_modal04 .cp_wrap {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
}
.cp_modal04 .cp_wrap label {
line-height: 1.5;
position: absolute;
top: 0.5em;
right: 0.5em;
display: inline-block;
width: 1.5em;
height: 1.5em;
cursor: pointer;
color: #ffffff;
border-radius: 50%;
background: #00bcd4;
}
.cp_modal04 .cp_wrap label.cp_btn {
position: relative;
overflow: hidden;
width: 4em;
padding: 10px 30px;
color: #ffffff;
border-radius: 3px;
}
.cp_modal04 .cp_wrap h2 {
margin-bottom: 1em;
text-transform: uppercase;
color: #ffffff;
}
.cp_modal04 .cp_wrap p {
text-align: justify;
color: #ffffff;
margin: 0;
line-height: 2.3;
text-indent: 1em;
}
.cp_modal04 p.citation {
text-align: right;
}
.cp_modal04 p.citation::before {
content: '\2500'
}
.cp_modal04 p:not(.citation) {
margin-bottom: 1.5rem;
}
.cp_modal04 .cp_modal input:focus ~ label {
-webkit-transform: scale(0.97);
transform: scale(0.97);
}
.cp_modal04 input {
position: absolute;
top: -1000px;
}
.cp_modal04 .cp_overlay {
z-index: -100;
-webkit-transition: all 0.75s cubic-bezier(0.68, -0.55, 0.265, 1.55);
transition: all 0.75s cubic-bezier(0.68, -0.55, 0.265, 1.55);
-webkit-transform: scale(0.5);
transform: scale(0.5);
opacity: 0;
}
.cp_modal04 input:checked ~ .cp_overlay {
z-index: 800;
-webkit-transform: scale(1);
transform: scale(1);
opacity: 1;
}

copypet.jp

CSS3などで新たに追加された要素・装飾方法など、日々コードを書いていないと忘れてしまったり、ささっとプロトタイプを作る時などちょっとしたことに時間をかけている暇はない。そんな時に「あ〜、あれストックしときゃよかったなぁ」って困った自分用のストックブログです。カスタマイズなどがしやすいよう、昨今のweb制作に取り入れられる一般的なコードを中心に掲載しています。

More Info

こんな記事はいかがですか?