CSS Only Tabs UI

Category: CSS & CSS3 | August 2, 2021
Author:mrwolferinc
Views Total:123 views
Official Page:Go to website
Last Update:August 2, 2021
License:MIT

Preview:

CSS Only Tabs UI

Description:

A minimal, clean tabs UI component built with CSS and radio buttons.

How to use it:

1. The HTML structure for the tabs UI.

<div class="container">
  <!-- Tabs -->
  <input type="radio" id="tab-link-1" name="tabset" checked>
  <label for="tab-link-1">Tab 1</label>
  <input type="radio" id="tab-link-2" name="tabset">
  <label for="tab-link-2">Tab 2</label>
  <input type="radio" id="tab-link-3" name="tabset">
  <label for="tab-link-3">Tab 3</label>
  <!-- Tab content -->
  <div class="tab-content">
    <section class="tab-panel" id="tab-1">
      <h2>Tab 1</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut tempor, velit et tempor molestie, urna purus fringilla neque, nec condimentum purus nibh eu tortor. Curabitur non maximus mi. Suspendisse cursus libero nisi, ac imperdiet purus malesuada vel. Cras maximus in quam vel sagittis. Sed ut cursus turpis. Mauris a ipsum congue, sagittis risus commodo, tincidunt mi. Sed mi ante, viverra et iaculis sollicitudin, malesuada vitae turpis. Proin nisl neque, semper a nisl et, dictum feugiat nulla.</p>
      <p>Praesent sit amet molestie nunc. In nec tellus cursus, malesuada urna quis, tristique nisl. Vivamus iaculis tempus libero. Mauris nec elit semper, volutpat dolor eu, gravida mauris. Maecenas sed ullamcorper justo. Cras sit amet massa augue. Nam posuere nec justo at auctor.</p>
    </section>
    <section class="tab-panel" id="tab-2">
      <h2>Tab 2</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec accumsan elit, et eleifend odio. Integer sed mi vel enim dapibus congue. Proin consequat augue sed tellus egestas gravida. Curabitur vel nibh sit amet metus semper rutrum. Etiam augue ex, aliquam sed convallis vitae, vulputate non diam. Aenean tempus diam in cursus interdum. Mauris tincidunt accumsan leo, eget mattis ex mattis eget.</p>
      <p>Aliquam ligula erat, tristique vitae orci eu, tristique semper velit. Suspendisse scelerisque et nisl quis convallis. Maecenas at libero ipsum. Quisque elementum metus id mauris pretium, bibendum auctor libero tincidunt. Cras dolor diam, faucibus vel massa eu, vestibulum bibendum lorem. Cras vel massa vitae ipsum dapibus imperdiet vel eu magna. Morbi ac augue non nibh ullamcorper.</p>
    </section>
    <section class="tab-panel" id="tab-3">
      <h2>Tab 3</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ultricies risus vel nibh malesuada, ut ultricies massa sagittis. Proin vestibulum bibendum velit, in congue neque eleifend eget. Vivamus quis turpis non odio tempus dapibus quis et ligula. Mauris molestie tincidunt tellus, sed interdum est egestas sed. Sed id ligula neque. Sed consequat venenatis metus a faucibus. Morbi mauris nulla, finibus at laoreet a, interdum sit amet dui. Cras convallis purus arcu, ut feugiat odio mattis vitae. Aliquam fringilla justo quis sagittis lobortis.</p>
      <p>Quisque suscipit pulvinar nunc, at auctor turpis. Nullam ex turpis, fringilla ut blandit vel, laoreet id turpis. Etiam velit ex, laoreet et felis eu, placerat tincidunt sem. Praesent ut ex ipsum. Suspendisse potenti. Quisque imperdiet lorem sit amet varius condimentum. Quisque id rhoncus nibh, vel semper velit. Class aptent taciti.</p>
    </section>
  </div>
</div>

2. The core CSS styles.

.container > input[type="radio"] {
  position: absolute;
  left: -200vw;
}

.container > label {
  position: relative;
  display: inline-block;
  padding: 15px 15px 25px;
  border: 1px solid transparent;
  border-bottom: 0;
  cursor: pointer;
  font-weight: 600;
}

.container > label::after {
  content: "";
  position: absolute;
  left: 15px;
  bottom: 10px;
  width: 22px;
  height: 4px;
  background: #8d8d8d;
}

.container > label:hover, .container > input:focus + label {
  color: #06c;
}

.container > label:hover::after, .container > input:focus + label::after, .container > input:checked + label::after {
  background: #06c;
}

.container > input:checked + label {
  border-color: #ccc;
  border-bottom: 1px solid #fff;
  margin-bottom: -1px;
}

.tab-content {
  border-top: 1px solid #ccc;
}

.tab-content section.tab-panel {
  padding: 30px 0;
}

3. Show & hide content based on the check state.

.container section.tab-panel {
  display: none;
}

.container > input:first-child:checked ~ .tab-content > section.tab-panel:first-child, .container > input:nth-child(3):checked ~ .tab-content > section.tab-panel:nth-child(2), .container > input:nth-child(5):checked ~ .tab-content > section.tab-panel:nth-child(3) {
  display: block;
}

You Might Be Interested In:


Leave a Reply