[パーツ]タブ シンプルなタブ切り替え(horizontal)

CSS HTML
 2018.02.19
 2018.03.27

シンプルなタブ切り替え(horizontal)

ページ内で情報を整理する時に使えるタブ切り替え。
横にタブが移動するシンプルなタブ切り替えです。

browser:  65 11 20 10 
ニャン易度 

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;
	}
}

copypet.jp

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

More Info

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