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.
While this method is semantic and accessible, you might consider using a pre-existing plugin for tabbed data.
+
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.
+
+
The HTML
+
The skeleton for this component is fairly basic - we just need the following structure:
+
+
Parent element for each tab item
+
Default radio input
+
Label linked to corresponding input
+
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>
+
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).
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):
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!
+
\ No newline at end of file
--
cgit v1.2.3-54-g00ecf