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

CSS HTML
Update 2018.02.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;
	}
}

copypet.jp

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

More Info

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