Dynamic Tab Panel using only HTML and CSS


This tutorial will guide you with easy code to create your own compact and lightweight version of Tabbed panel which work perfectly well. If you don't know JavaScript not a problem all you need is HTML and CSS know how and you are good to go.

This tutorial is a very short and comprehensive. In this tutorial we will Make a Tab-Content switching system with only html and css. We won't be using any JavaScript in this case. However if you are looking something similar to do with Angular JS you can check out this tutorial here: Dynamic Tab Switch Control with AngularJS.

In this tutorials as we mention there is no JavaScript used, so it is quick easy and lightweight. So for a start let us make the tabs and content section to map them. To make the tabs we are going to use the radio buttons and labels.


<!doctype html>
<html>
	<head>
		<title>Tabs Demo</title>
		<link rel="stylesheet" href="tabs.css" >
	</head>
	<body>
		<input type="radio" name="tabs" id="tab1" checked="checked">
		<label for="tab1">Hello World</label>
		<input type="radio" name="tabs" id="tab2">
		<label for="tab2">Hello World 2</label>
		<input type="radio" name="tabs" id="tab3">
		<label for="tab3">Hello World 3</label>
		<input type="radio" name="tabs" id="tab4">
		<label for="tab4">Hello World 4</label>
		<input type="radio" name="tabs" id="tab5">
		<label for="tab5">Hello World 5</label>
		<div id="tab-content1" class="tab-content">
			<h2>Tab Content 1</h2>
			<p></p>
		</div>
		<div id="tab-content2" class="tab-content">
			<h2>Tab Content 2</h2>
			<p></p>
		</div>
		<div id="tab-content3" class="tab-content">
			<h2>Tab Content 3</h2>
			<p></p>
		</div>
		<div id="tab-content4" class="tab-content">
			<h2>Tab Content 4</h2>
			<p></p>
		</div>
		<div id="tab-content5" class="tab-content">
			<h2>Tab Content 5</h2>
			<p></p>
		</div>
	</body>
</html>

As you can see we have used radio and label, this way when we hide the radio the label will act as the Tab however underlying radio button will remain in operation. We have assigned the Id to each radio so that we can bind this id to a specific tab content div which we use for displaying content for the currently active tab.

The Stage is set all we need is a little design and CSS as logic to work our tabs and content switching. We use following CSS.


body{
	background:#9accc8;
	padding:25px;
	width:90%;
	margin:0 auto;
	font-family:arial;
}



input[name=tabs]{
	display:none;
}

input[name=tabs] + label{
	padding:5px;
	background:#00A886;
	border-radius:5px;
	color:white;
	
}

input[name=tabs]:checked + label{
	font-weight:bold;
	border:solid 2px #fff;
}

.tab-content{
	display:none;
}

#tab1:checked ~ #tab-content1, #tab2:checked ~ #tab-content2,  #tab3:checked ~ #tab-content3, #tab4:checked ~ #tab-content4, #tab5:checked ~ #tab-content5 {
	display:block;
}

The body is nothing but style to the body, then We have hidden the radio buttons so that Labels can take the spot, added some colors to label, and we are using ":checked" property for checking which tab to display active and which content to show, at last hidden all the content bodies and mapped the radio button to the corresponding content body so that when one radio is checked the corresponding body is displayed.

You can check out the live demo here: Dynamic Tabs using CSS Demo