コピペでできる!CSSとhtmlだけのシンプルなタブ切り替え2種

CSS HTML
 2018.02.19
 2018.03.22

ページ内の情報を整理する時などによく使うタブ切り替え。
CSSとHTMLだけで作るシンプルなタブ切り替えを用意しました。
横並びのタブと縦並びのタブです。
カスタマイズのベースとしてお使いいただければと思います。

browser:  65 11 20 10 

CSSとhtmlだけのシンプルなタブ切り替え[2種]

横にタブが移動するシンプルなタブ

ニャン易度

First Tab

First Tab text

Second Tab

Second Tab text

Third Tab

Third Tab text

Force Tab

Force Tab text

<div class="cp_tab">
	<input type="radio" name="cp_tab" id="tab1_1" aria-controls="first_tab01" checked>
	<label for="tab1_1">First Tab</label>
	<input type="radio" name="cp_tab" id="tab1_2" aria-controls="second_tab01">
	<label for="tab1_2">Second Tab</label>
	<input type="radio" name="cp_tab" id="tab1_3" aria-controls="third_tab01">
	<label for="tab1_3">Third Tab</label>
	<input type="radio" name="cp_tab" id="tab1_4" aria-controls="force_tab01">
	<label for="tab1_4">Force Tab</label>
	<div class="cp_tabpanels">
		<div id="first_tab01" class="cp_tabpanel">
		<h2>First Tab</h2>
		<p>First Tab text</p>
		</div>
		<div id="second_tab01" class="cp_tabpanel">
		<h2>Second Tab</h2>
		<p>Second Tab text</p>
		</div>
		<div id="third_tab01" class="cp_tabpanel">
		<h2>Third Tab</h2>
		<p>Third Tab text</p>
		</div>
		<div id="force_tab01" class="cp_tabpanel">
		<h2>Force Tab</h2>
		<p>Force Tab text</p>
		</div>
	</div>
</div>
.cp_tab *, .cp_tab *:before, .cp_tab *:after {
	-webkit-box-sizing: border-box;
	        box-sizing: border-box;
}
.cp_tab {
	margin: 1em auto;
}
.cp_tab > input[type='radio'] {
	margin: 0;
	padding: 0;
	border: none;
	border-radius: 0;
	outline: none;
	background: none;
	-webkit-appearance: none;
	        appearance: none;
	display: none;
}
.cp_tab .cp_tabpanel {
	display: none;
}
.cp_tab > input:first-child:checked ~ .cp_tabpanels > .cp_tabpanel:first-child,
.cp_tab > input:nth-child(3):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(2),
.cp_tab > input:nth-child(5):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(3),
.cp_tab > input:nth-child(7):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(4),
.cp_tab > input:nth-child(9):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(5),
.cp_tab > input:nth-child(11):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(6) {
	display: block;
}
.cp_tab > label {
	position: relative;
	display: inline-block;
	padding: 15px;
	cursor: pointer;
	border: 1px solid transparent;
	border-bottom: 0;
}
.cp_tab > label:hover,
.cp_tab > input:focus + label {
	color: #0066cc;
}
.cp_tab > input:checked + label {
	margin-bottom: -1px;
	border-color: #cccccc;
	border-bottom: 1px solid #ffffff;/*背景色と同じ*/
	border-radius: 6px 6px 0 0;
}
.cp_tab .cp_tabpanel {
	padding: 0.5em 1em;
	border-top: 1px solid #cccccc;
}
@media (max-width: 480px) {
	.cp_tab {
		width: 100%;
		font-size: 0.8em;
	}
	.cp_tab label {
		padding: 0.5em;
	}
}

縦にタブが移動するシンプルなタブ

ニャン易度

First Tab

First Tab text

Second Tab

Second Tab text

Third Tab

Third Tab text

Force Tab

Force Tab text

<div class="cp_tab">
	<input type="radio" name="cp_tab" id="tab2_1" aria-controls="first_tab02" checked>
	<label for="tab2_1">First Tab</label>
	<input type="radio" name="cp_tab" id="tab2_2" aria-controls="second_tab02">
	<label for="tab2_2">Second Tab</label>
	<input type="radio" name="cp_tab" id="tab2_3" aria-controls="third_tab02">
	<label for="tab2_3">Third Tab</label>
	<input type="radio" name="cp_tab" id="tab2_4" aria-controls="force_tab02">
	<label for="tab2_4">Force Tab</label>
	<div class="cp_tabpanels">
		<div class="cp_tabpanel">
		<h2>First Tab</h2>
		<p>First Tab text</p>
		</div>
		<div class="cp_tabpanel">
		<h2>Second Tab</h2>
		<p>Second Tab text</p>
		</div>
		<div class="cp_tabpanel">
		<h2>Third Tab</h2>
		<p>Third Tab text</p>
		</div>
		<div class="cp_tabpanel">
		<h2>Force Tab</h2>
		<p>Force Tab text</p>
		</div>
	</div>
</div>
.cp_tab *, .cp_tab *:before, .cp_tab *:after {
	-webkit-box-sizing: border-box;
	        box-sizing: border-box;
}
.cp_tab {
	margin: 1em auto;
	position: relative;
}
.cp_tab input[type='radio'] {
	margin: 0;
	padding: 0;
	border: none;
	border-radius: 0;
	outline: none;
	background: none;
	-webkit-appearance: none;
	        appearance: none;
	display: none;
}
.cp_tab .cp_tabpanel {
	display: none;
}
.cp_tab > input:first-child:checked ~ .cp_tabpanels > .cp_tabpanel:first-child,
.cp_tab > input:nth-child(3):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(2),
.cp_tab > input:nth-child(5):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(3),
.cp_tab > input:nth-child(7):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(4),
.cp_tab > input:nth-child(9):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(5),
.cp_tab > input:nth-child(11):checked ~ .cp_tabpanels > .cp_tabpanel:nth-child(6) {
	display: block;
}
.cp_tab .cp_tabpanels {
	position: absolute;
	top:0;
	left: 10em;
	display: block;
	height: 320px;
}
.cp_tab > input + label {
	position: relative;
	z-index: 100;
	top: -15px;
	left: 1px;
	display: block;
	width: 10em;
	margin-bottom: -20px;
	padding: 15px;
	cursor: pointer;
	border: 1px solid #cccccc;
	border-right: 0;
	border-radius: 6px 0 0 6px;
}
.cp_tab > input + label:last-of-type {
	margin-bottom: 100px;
}
.cp_tab > label:hover,
.cp_tab > input:focus + label {
	color: #0066cc;
}
.cp_tab > input:checked + label {
	border-color: #cccccc;
	border-right: 1px solid #ffffff;/*背景色と同じ*/
}
.cp_tab .cp_tabpanel {
	padding: 0.5em 1em;
	border-left: 1px solid #cccccc;
	min-height: 320px;
}
@media (max-width: 480px) {
	.cp_tab {
		width: 100%;
	}
	.cp_tab > input + label {
	font-size: 0.7em;
	}
	.cp_tab .cp_tabpanels {
	left: 7em;
	min-width: 7em;
	}
}

エフェクトなどがついたものが欲しい!

copypet.jp

コピペでできる!CSSとhtmlだけの切り替えエフェクトがグッとくるタブ切り替え5種 | copypet.jp|パーツで探す、web制作に使えるコピペサイト。

ページ内の情報を整理する時などによく使うタブ切り替え。 CSSとHTMLだけで作る切り替えエフェクトがグッとくるタブ切り替えを用意しました。…

copypet.jp

記事を見る

copypet.jp

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

More Info

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