1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
---
layout: post
title: "Tabbed Content Without JavaScript"
date: 2019-01-28
---
Creating tabs is a fairly trivial and common practice in web design, but many times it requires JavaScript to properly implement. Fortunately it *is* possible to create tabbed content with only using CSS.
![Tabbed elements with only CSS](/public/images/tabbed-content.png)
[Live CodePen Example](https://codepen.io/bradleytaunt/pen/abjmayw)
---
<div class="message">
<p><strong>Sidenote:</strong></p>
<p>While this method is semantic and accessible, you might consider using a pre-existing plugin for tabbed data.</p>
<p>This component tends to feel a little "stiff" compared to more fleshed out variations available. This pure CSS version is better suited as a fallback for when users have disabled JavaScript.</p>
</div>
## The HTML
The skeleton for this component is fairly basic - we just need the following structure:
1. Parent element for each tab item
2. Default radio input
3. Label linked to corresponding input
4. Inner content associated with each tab item
---
<!-- Simple main container for all elements -->
<div class="tabs">
<!-- Parent container holding for individual tab item -->
<div class="tab-item">
<!-- Default radio input -->
<input class="tab-input" type="radio" name="tabs" id="tab-1">
<!-- Label connected to radio input via `id` and `for` attributes -->
<label class="tab-label" for="tab-1">Tab 1</label>
<!-- Full inner content of current tab item -->
<div class="tab-content">Content goes here</div>
</div>
</div>
Full HTML for reference:
<div class="tabs">
<div class="tab-item">
<input class="tab-input" type="radio" name="tabs" id="tab-1">
<label class="tab-label" for="tab-1">Tab 1</label>
<div class="tab-content">Content goes here</div>
</div>
<div class="tab-item">
<input class="tab-input" type="radio" name="tabs" id="tab-2">
<label class="tab-label" for="tab-2">Tab 2</label>
<div class="tab-content">Content goes here</div>
</div>
<div class="tab-item">
<input class="tab-input" type="radio" name="tabs" id="tab-3">
<label class="tab-label" for="tab-3">Tab 3</label>
<div class="tab-content">Content goes here</div>
</div>
</div>
## The CSS
First, we need to set each `input`, `label` and inner content into their own parent containers:
/* Main parent that holds all contents */
.tabs {
height: 100%;
min-height: 250px;
position: relative;
}
/* Each tab items (includes heading & content) */
.tab-item {
display: inline;
}
Next, we will hide the default `radio` input and design our labels to resemble a basic web tab element. The `z-index` property on the label is important for how we will be stacking our content on the z-axis (labels above inner content for example).
/* Hide the default radio inputs */
.tab-input {
position: absolute;
visibility: hidden;
}
/* The main tab headings */
.tab-label {
background: white;
box-shadow: inset 0 -4px 4px rgba(0,0,0,0.02);
color: lightgrey;
cursor: pointer;
display: inline-block;
font-weight: 600;
margin: 0 5px 0 0;
padding: 10px 20px;
position: relative;
text-align: center;
z-index: 0;
}
The main inner content of each tab needs to have an `absolute` position set as it's default, since the one currently selected will switch to `relative` on mobile (more on that in a moment):
/* The inner tab content */
.tab-content {
background: white;
bottom: 0;
box-shadow: 0 6px 8px rgba(0,0,0,0.02);
left: 0;
overflow: scroll;
padding: 20px;
position: absolute;
right: 0;
top: 50px;
z-index: 0;
}
The final step is just telling the browser to style both the `label` and inner content of the currently selected radio `input`:
/* Style the currently selected tab label */
.tab-input:checked + .tab-label {
border: 1px solid #eee;
border-bottom: 0;
box-shadow: 0 -6px 8px rgba(0,0,0,0.02);
color: #268bd2;
z-index: 2;
}
/* Show the currently selected tab content */
.tab-input:checked ~ .tab-content {
border: 1px solid #eee;
z-index: 1;
}
It's as simple as that! For reference, here is the entire CSS file for easier access:
/* Main parent that holds all contents */
.tabs {
height: 100%;
min-height: 250px;
position: relative;
}
/* Each tab items (includes heading & content) */
.tab-item {
display: inline;
}
/* Hide the default radio inputs */
.tab-input {
position: absolute;
visibility: hidden;
}
/* The main tab headings */
.tab-label {
background: white;
box-shadow: inset 0 -4px 4px rgba(0,0,0,0.02);
color: lightgrey;
cursor: pointer;
display: inline-block;
font-weight: 600;
margin: 0 5px 0 0;
padding: 10px 20px;
position: relative;
text-align: center;
z-index: 0;
}
/* The inner tab content */
.tab-content {
background: white;
bottom: 0;
box-shadow: 0 6px 8px rgba(0,0,0,0.02);
left: 0;
overflow: scroll;
padding: 20px;
position: absolute;
right: 0;
top: 50px;
z-index: 0;
}
/* Style the currently selected tab label */
.tab-input:checked + .tab-label {
border: 1px solid #eee;
border-bottom: 0;
box-shadow: 0 -6px 8px rgba(0,0,0,0.02);
color: #268bd2;
z-index: 2;
}
/* Show the currently selected tab content */
.tab-input:checked ~ .tab-content {
border: 1px solid #eee;
z-index: 1;
}
## Don't forget about mobile
With only a few extra lines of CSS we can ensure that our custom tabs will stack on top of each other and look solid on mobile devices:
@media(max-width:38em) {
.tab-label {
display: block;
width: 100%;
}
.tab-content {
display: none;
}
.tab-input:checked ~ .tab-content {
bottom: auto;
display: block;
position: relative;
top: auto;
}
}
## One minor caveat
Even though I'm a pretty big fan of implementing tabs this way, there is a small drawback:
The `height` of the inner content doesn't grow dynamically since it defaults as `absolute`, so a `min-height` or `height` value is required on the parent element. This could become a problem in certain situations where you don't have the luxury of setting a static height.
Other than that, enjoy building some JavaScript-free tabs!
|