Sliding Tabs Css

Tutorial kali ini akan membahas bagaimana membuat Sliding Tabs hanya dengan menggunakan css tanpa framework css ataupun javascript.

Langsung saja ke kode dibawah.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title> Sliding Tabs CSS | MedanInCode</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="tabs">
            <input type="radio" id="tab-1" name="tabs" checked />
            <label class="tab" for="tab-1">Primary<span class="notification">2</span></label>
            <input type="radio" id="tab-2" name="tabs" />
            <label class="tab" for="tab-2">Promotions</label>
            <input type="radio" id="tab-3" name="tabs" />
            <label class="tab" for="tab-3">Social</label>
            <span class="active"></span>
        </div>
    </div>
</body>
</html>

style.css

:root {
  --primary-color: #dc3545;
  --secondary-color: #e6eef9;
  --bg-color: #2e2e2e;
  --tab-height: 50px;
  --tab-width: 200px;
}

body {
  background-color: var(--bg-color);
}

input[type=radio] {
  display: none;
}

.container {
  position: fixed; 
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.tabs {
  display: flex;
  position: relative;
  padding: 0.5rem;
  border-radius: 2rem;
  background-color: #fff;
}
/*selector for all element under .tabs*/
.tabs * {
  z-index: 2;
}

.tab {
  display: flex;
  align-items: center;
  justify-content: center;
  height: var(--tab-height);
  width: var(--tab-width);
  font-size: 1.25rem;
  font-weight: 500;
  border-radius: 2rem;
  cursor: pointer;
  transition: color 0.15s ease-in;
}

.notification {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  margin-left: 0.75rem;
  border-radius: 50%;
  background-color: var(--secondary-color);
  transition: 0.25s ease-in;
}

input[type=radio]:checked + label {
  color: var(--primary-color);
}
input[type=radio]:checked + label > .notification {
  background-color: var(--primary-color);
  color: #fff;
}

input[id=tab-1]:checked ~ .active {
  transform: translateX(0);
}

input[id=tab-2]:checked ~ .active {
  transform: translateX(100%);
}

input[id=tab-3]:checked ~ .active {
  transform: translateX(200%);
}

.active {
  position: absolute;
  display: flex;
  height: var(--tab-height);
  width: var(--tab-width);
  background-color: var(--secondary-color);
  border-radius: 2rem;
  transition: 0.25s ease;
  z-index: 1;
}

@media (max-width: 700px) {
  .tabs {
    transform: scale(0.6);
  }
}

Kita menggunakan tambahan div dengan class active untuk menghadirkan effect slide,kalau hanya effect warna background teman-teman hanya perlu menambah background color.

Jika ada yang perlu ditanyakan tentang selector css atau fungsi class di style.css bisa tanyakan di kolom komentar dibawah.