[CSS]レスポンシブ対応、スクリプト無し、完成度も非常に高いタブコンテンツを実装するテクニック
Post on:2014年6月30日
sponsorsr
スクリプトは一切使用せず、デスクトップ時はタブコンテンツ、スマフォ・タブレット時はタブをスケールダウンに変更してコンテンツを表示するスタイルシートのテクニックを紹介します。
単にレスポンシブ対応のタブというだけでなく、コンテンツの切替やタブのホバーエフェクトなど、非常に完成度の高いタブコンテンツに仕上がっています。

デモのアニメーション
実際の動作は下記ページで確認できます。

No JS: Tabs that scale down to menu
簡単にレビューを。
まずはデスクトップでの表示、タブは5つセットされており、タブのホバーやコンテンツ切替時の繊細なエフェクトが気持ちいいです。

タブの仕組みは、最上部のラジオボタンでタブを制御しており、各タブと連動しています。

タブの仕組み
スマフォ・タブレット時はタブが、スケールダウンに変更します。

幅780pxで表示
濃いレッドのバーをタップすると、タブのアイテムがスケールダウンで表示されます。

スケールダウンを開いたところ
上部に注目です、スケールダウンはラジオボタンの下にあるチェックボックスで制御しています。

スケールダウンの仕組み
実装はこんな感じになります。
HTML
まずは上部のコントローラー、このラジオボタンで各タブの動きを制御します。
デザインを変更して利用したり、タブのみにしたい場合は非表示にするのも有りですね。
<!-- TAB CONTROLLERS -->
<input id="panel-1-ctrl"
class="panel-radios" type="radio" name="tab-radios" checked>
<input id="panel-2-ctrl"
class="panel-radios" type="radio" name="tab-radios">
<input id="panel-3-ctrl"
class="panel-radios" type="radio" name="tab-radios">
<input id="panel-4-ctrl"
class="panel-radios" type="radio" name="tab-radios">
<input id="panel-5-ctrl"
class="panel-radios" type="radio" name="tab-radios">
<input id="nav-ctrl"
class="panel-radios" type="checkbox" name="nav-checkbox">
各タブはlabel要素をリストで配置し、コンテンツはsection要素で配置します。中身があるとコードが長くなるので省略しています。
<header id="introduction">
<h1>No JS: Tabs That Scale Down to Menu</h1>
</header>
<!-- TABS LIST -->
<ul id="tabs-list">
<!-- MENU TOGGLE -->
<label id="open-nav-label" for="nav-ctrl"></label>
<li id="li-for-panel-1">
<label class="panel-label"
for="panel-1-ctrl">タブ 1</label>
</li><!--INLINE-BLOCK FIX
--><li id="li-for-panel-2">
<label class="panel-label"
for="panel-2-ctrl">タブ 2</label>
</li><!--INLINE-BLOCK FIX
--><li id="li-for-panel-3">
<label class="panel-label"
for="panel-3-ctrl">タブ 3</label>
</li><!--INLINE-BLOCK FIX
--><li id="li-for-panel-4">
<label class="panel-label"
for="panel-4-ctrl">タブ 4</label>
</li><!--INLINE-BLOCK FIX
--><li id="li-for-panel-5">
<label class="panel-label"
for="panel-5-ctrl">タブ 5</label>
</li>
<label id="close-nav-label" for="nav-ctrl">Close</label>
</ul>
<!-- THE PANELS -->
<article id="panels">
<div class="container">
<section id="panel-1">
<main>
<p>コンテンツ 1</p>
</main>
</section>
<section id="panel-2">
<main>
<p>コンテンツ 2</p>
</main>
</section>
<section id="panel-3">
<main>
<p>コンテンツ 3</p>
</main>
</section>
<section id="panel-4">
<main>
<p>コンテンツ 4</p>
</main>
</section>
<section id="panel-5">
<main>
<p>コンテンツ 5</p>
</main>
</section>
</div>
</article>
CSS
スタイルシートはブラウザベンダや各デザイン部分があるので長いですが、このままコピペで利用できます。
@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,400|Inconsolata);
ul#tabs-list {
list-style: none;
text-align: center;
border-bottom: 1px solid #dfdfdf;
margin: 0;
padding: 0;
}
label.panel-label {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
display: block;
width: 100%;
color: #bdc3c7;
cursor: pointer;
background-color: #ecf0f1;
-webkit-transition-property: border-top, background-color, color;
transition-property: border-top, background-color, color;
-webkit-transition-duration: 200ms;
transition-duration: 200ms;
}
label.panel-label:hover {
color: #c0392b;
}
#panels {
background-color: white;
}
#panels .container {
margin: 0 auto;
width: 90%;
}
#panels section header label.panel-label {
padding: 12px 24px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#panels section main {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
max-height: 0;
opacity: 0;
-webkit-transition: opacity 600ms;
transition: opacity 600ms;
overflow-y: hidden;
}
#panel-1-ctrl:checked ~ #panels #panel-1 main {
max-height: initial;
opacity: 1;
padding: 48px 24px;
}
#panel-2-ctrl:checked ~ #panels #panel-2 main {
max-height: initial;
opacity: 1;
padding: 48px 24px;
}
#panel-3-ctrl:checked ~ #panels #panel-3 main {
max-height: initial;
opacity: 1;
padding: 48px 24px;
}
#panel-4-ctrl:checked ~ #panels #panel-4 main {
max-height: initial;
opacity: 1;
padding: 48px 24px;
}
#panel-5-ctrl:checked ~ #panels #panel-5 main {
max-height: initial;
opacity: 1;
padding: 48px 24px;
}
@media all and (max-width: 767px) {
#nav-ctrl:checked ~ #tabs-list #li-for-panel-1 {
max-height: 46px;
opacity: 1;
}
#nav-ctrl:checked ~ #tabs-list #li-for-panel-2 {
max-height: 46px;
opacity: 1;
}
#nav-ctrl:checked ~ #tabs-list #li-for-panel-3 {
max-height: 46px;
opacity: 1;
}
#nav-ctrl:checked ~ #tabs-list #li-for-panel-4 {
max-height: 46px;
opacity: 1;
}
#nav-ctrl:checked ~ #tabs-list #li-for-panel-5 {
max-height: 46px;
opacity: 1;
}
#open-nav-label {
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
cursor: pointer;
}
#nav-ctrl:checked ~ #tabs-list #open-nav-label {
display: none;
}
#close-nav-label {
display: block;
max-height: 0;
overflow-y: hidden;
background-color: #444444;
color: #ecf0f1;
padding: 0px;
-webkit-transition: max-height 200ms;
transition: max-height 200ms;
cursor: pointer;
text-transform: uppercase;
font-size: 12px;
line-height: 22px;
letter-spacing: 1px;
}
#nav-ctrl:checked ~ #tabs-list #close-nav-label {
max-height: 36px;
opacity: 1;
padding: 12px 24px;
}
#tabs-list {
position: relative;
}
#tabs-list label.panel-label {
padding: 12px 0;
}
#tabs-list #li-for-panel-1 {
max-height: 0;
overflow-y: hidden;
-webkit-transition: max-height 200ms;
transition: max-height 200ms;
}
#tabs-list #li-for-panel-2 {
max-height: 0;
overflow-y: hidden;
-webkit-transition: max-height 200ms;
transition: max-height 200ms;
}
#tabs-list #li-for-panel-3 {
max-height: 0;
overflow-y: hidden;
-webkit-transition: max-height 200ms;
transition: max-height 200ms;
}
#tabs-list #li-for-panel-4 {
max-height: 0;
overflow-y: hidden;
-webkit-transition: max-height 200ms;
transition: max-height 200ms;
}
#tabs-list #li-for-panel-5 {
max-height: 0;
overflow-y: hidden;
-webkit-transition: max-height 200ms;
transition: max-height 200ms;
}
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 {
max-height: 46px;
opacity: 1;
}
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 label.panel-label {
background-color: white;
color: #c0392b;
background-color: #c0392b;
color: white;
}
#panel-2-ctrl:checked ~ #tabs-list #li-for-panel-2 {
max-height: 46px;
opacity: 1;
}
#panel-2-ctrl:checked ~ #tabs-list #li-for-panel-2 label.panel-label {
background-color: white;
color: #c0392b;
background-color: #c0392b;
color: white;
}
#panel-3-ctrl:checked ~ #tabs-list #li-for-panel-3 {
max-height: 46px;
opacity: 1;
}
#panel-3-ctrl:checked ~ #tabs-list #li-for-panel-3 label.panel-label {
background-color: white;
color: #c0392b;
background-color: #c0392b;
color: white;
}
#panel-4-ctrl:checked ~ #tabs-list #li-for-panel-4 {
max-height: 46px;
opacity: 1;
}
#panel-4-ctrl:checked ~ #tabs-list #li-for-panel-4 label.panel-label {
background-color: white;
color: #c0392b;
background-color: #c0392b;
color: white;
}
#panel-5-ctrl:checked ~ #tabs-list #li-for-panel-5 {
max-height: 46px;
opacity: 1;
}
#panel-5-ctrl:checked ~ #tabs-list #li-for-panel-5 label.panel-label {
background-color: white;
color: #c0392b;
background-color: #c0392b;
color: white;
}
#panels .container {
width: 100%;
}
#panels section header {
display: block;
}
}
@media all and (min-width: 768px) {
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 {
pointer-events: none;
cursor: default;
-webkit-transform: translate(0, 1px);
-ms-transform: translate(0, 1px);
transform: translate(0, 1px);
-webkit-box-shadow: none;
box-shadow: none;
border-top: none;
border-right: none;
}
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1:last-child {
border-right: 1px solid #dfdfdf;
}
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 + li {
border-left: 1px solid #dfdfdf;
}
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 label.panel-label {
background-color: white;
color: #c0392b;
border-top: 6px solid #c0392b;
padding-top: 26px;
}
#panel-2-ctrl:checked ~ #tabs-list #li-for-panel-2 {
pointer-events: none;
cursor: default;
-webkit-transform: translate(0, 1px);
-ms-transform: translate(0, 1px);
transform: translate(0, 1px);
-webkit-box-shadow: none;
box-shadow: none;
border-top: none;
border-right: none;
}
#panel-2-ctrl:checked ~ #tabs-list #li-for-panel-2:last-child {
border-right: 1px solid #dfdfdf;
}
#panel-2-ctrl:checked ~ #tabs-list #li-for-panel-2 + li {
border-left: 1px solid #dfdfdf;
}
#panel-2-ctrl:checked ~ #tabs-list #li-for-panel-2 label.panel-label {
background-color: white;
color: #c0392b;
border-top: 6px solid #c0392b;
padding-top: 26px;
}
#panel-3-ctrl:checked ~ #tabs-list #li-for-panel-3 {
pointer-events: none;
cursor: default;
-webkit-transform: translate(0, 1px);
-ms-transform: translate(0, 1px);
transform: translate(0, 1px);
-webkit-box-shadow: none;
box-shadow: none;
border-top: none;
border-right: none;
}
#panel-3-ctrl:checked ~ #tabs-list #li-for-panel-3:last-child {
border-right: 1px solid #dfdfdf;
}
#panel-3-ctrl:checked ~ #tabs-list #li-for-panel-3 + li {
border-left: 1px solid #dfdfdf;
}
#panel-3-ctrl:checked ~ #tabs-list #li-for-panel-3 label.panel-label {
background-color: white;
color: #c0392b;
border-top: 6px solid #c0392b;
padding-top: 26px;
}
#panel-4-ctrl:checked ~ #tabs-list #li-for-panel-4 {
pointer-events: none;
cursor: default;
-webkit-transform: translate(0, 1px);
-ms-transform: translate(0, 1px);
transform: translate(0, 1px);
-webkit-box-shadow: none;
box-shadow: none;
border-top: none;
border-right: none;
}
#panel-4-ctrl:checked ~ #tabs-list #li-for-panel-4:last-child {
border-right: 1px solid #dfdfdf;
}
#panel-4-ctrl:checked ~ #tabs-list #li-for-panel-4 + li {
border-left: 1px solid #dfdfdf;
}
#panel-4-ctrl:checked ~ #tabs-list #li-for-panel-4 label.panel-label {
background-color: white;
color: #c0392b;
border-top: 6px solid #c0392b;
padding-top: 26px;
}
#panel-5-ctrl:checked ~ #tabs-list #li-for-panel-5 {
pointer-events: none;
cursor: default;
-webkit-transform: translate(0, 1px);
-ms-transform: translate(0, 1px);
transform: translate(0, 1px);
-webkit-box-shadow: none;
box-shadow: none;
border-top: none;
border-right: none;
}
#panel-5-ctrl:checked ~ #tabs-list #li-for-panel-5:last-child {
border-right: 1px solid #dfdfdf;
}
#panel-5-ctrl:checked ~ #tabs-list #li-for-panel-5 + li {
border-left: 1px solid #dfdfdf;
}
#panel-5-ctrl:checked ~ #tabs-list #li-for-panel-5 label.panel-label {
background-color: white;
color: #c0392b;
border-top: 6px solid #c0392b;
padding-top: 26px;
}
ul#tabs-list {
text-align: center;
border-bottom: 1px solid #dfdfdf;
}
ul#tabs-list li {
display: inline-block;
text-align: center;
font-size: 0.875em;
width: 18%;
-webkit-box-shadow: 0px -2px 2px rgba(0, 0, 0, 0.05);
box-shadow: 0px -2px 2px rgba(0, 0, 0, 0.05);
border-top: 1px solid #dfdfdf;
border-right: 1px solid #dfdfdf;
-webkit-transition-property: border-top;
transition-property: border-top;
-webkit-transition-duration: 200ms;
transition-duration: 200ms;
}
ul#tabs-list li:hover {
border-top: none;
border-right: none;
}
ul#tabs-list li:hover:last-of-type {
border-right: 1px solid #dfdfdf;
}
ul#tabs-list li:hover + li {
border-left: 1px solid #dfdfdf;
}
ul#tabs-list li label.panel-label {
border-top: 0px solid #c0392b;
padding: 24px 0;
}
ul#tabs-list li label.panel-label:hover {
border-top-width: 6px;
padding-top: 25px;
}
#open-nav-label,
#close-nav-label {
display: none;
}
#nav-ctrl {
display: none;
}
}
@media all and (min-width: 900px) {
main {
width: 70%;
margin: 0 auto;
}
}
.panel-radios {
position: fixed;
left: 50%;
top: 10px;
width: 20px;
opacity: 0.5;
z-index: 99;
}
.panel-radios:nth-child(1) {
-webkit-transform: translateX(-50px);
-ms-transform: translateX(-50px);
transform: translateX(-50px);
}
.panel-radios:nth-child(2) {
-webkit-transform: translateX(-30px);
-ms-transform: translateX(-30px);
transform: translateX(-30px);
}
.panel-radios:nth-child(3) {
-webkit-transform: translateX(-10px);
-ms-transform: translateX(-10px);
transform: translateX(-10px);
}
.panel-radios:nth-child(4) {
-webkit-transform: translateX(10px);
-ms-transform: translateX(10px);
transform: translateX(10px);
}
.panel-radios:nth-child(5) {
-webkit-transform: translateX(30px);
-ms-transform: translateX(30px);
transform: translateX(30px);
}
.panel-radios:nth-child(6) {
top: 30px;
-webkit-transform: translateX(-10px);
-ms-transform: translateX(-10px);
transform: translateX(-10px);
display: block;
}
body {
background: #e74c3c;
color: #444444;
font-family: "Open Sans", "Helvetica Neue", Helvetica, sans-serif;
}
#introduction {
width: 90%;
margin: 0 auto;
padding: 48px 24px;
color: white;
}
#introduction h1 {
font-weight: 300;
text-align: center;
}
main h1 {
margin-top: 0;
font-weight: 300;
color: #c0392b;
}
main p {
line-height: 1.8;
}
main hr {
margin: 12px 0;
border-top: 1px solid #dfdfdf;
}
label.demo-label {
background-color: #c0392b;
color: white;
padding: 4px 8px;
border-radius: 2px;
cursor: pointer;
display: inline-block;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
label.demo-label:hover {
background-color: #e74c3c;
}
#demo-child-toggle {
display: none;
}
#demo-child-toggle:checked ~ #demo-parent #demo-child {
color: #c0392b;
font-weight: bold;
text-transform: uppercase;
}
#demo-parent {
margin-top: 8px;
}
code,
pre {
color: #c0392b;
font-family: Inconsolata, "Andale Mono", Andale, monowidth;
background-color: #ecf0f1;
border: 1px solid #dfdfdf;
border-radius: 2px;
}
code {
padding: 2px 6px;
}
pre {
padding: 12px;
line-height: 1.6;
}
pre strong {
color: #444444;
}
sponsors











