Turning CSS grid into clickable tabs with simple jQuery

Clickable Tabs are a great way to display information. It’s interactive and more engaging. I would like to create clickable tabs using CSS grid with simple jQuery in this post.

In this example, we will have a stacked block layout for mobile screens, and three column clickable tabs for larger screens. I’m going to use three highest mountains in the world as a sample content.

Let’s start by creating the HTML markups. Each mountain will have an image, title, button and text content.

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
<h1>Highest Mountains in the World</h1>
<h3>Click '+' for the effect</h3>
<h4>Change the window size for responsive layout</h4>
<div class="grid_items">
  <div class="grid_item tab active">
    <img src="https://webmadewell.com/wp-content/uploads/2018/04/img-bg-grid-tab-everest.jpg" alt="Mount Everest"></img>
    <div class="title">Mount Everest
      <button><span>more info</span></button>
    </div>
    <div class="content">
      <p>Mount Everest, known in Nepali as Sagarmāthā and in Tibetan as Chomolungma, is Earth's highest mountain above sea level, located in the Mahalangur Himal sub-range of the Himalayas. The international border between China (Tibet Autonomous Region) and Nepal (Province No. 1) runs across its summit point.</p>
      <p>The current official elevation of 8,848 m (29,029 ft), recognised by China and Nepal, was established by a 1955 Indian survey and subsequently confirmed by a Chinese survey in 1975.[1] In 2005, China remeasured the rock height of the mountain, with a result of 8844.43 m.</p>
    </div>
  </div>
  <div class="grid_item tab">
    <img src="https://webmadewell.com/wp-content/uploads/2018/04/img-bg-grid-tab-k2.jpg" alt="K2"></img>
    <div class="title">K2
      <button><span>more info</span></button>
    </div>
    <div class="content">
      <p>K2 is known as the Savage Mountain due to the extreme difficulty of ascent. It has the second-highest fatality rate among the eight thousanders. With around 300 successful summits and 77 fatalities, about one person dies on the mountain for every four who reach the summit.</p>
      <p>It is more difficult and hazardous to reach the peak of K2 from the Chinese side, so it is usually climbed from the Pakistani side. Unlike Annapurna, the mountain with the highest fatality-to-summit rate (191 summits and 61 fatalities), or the other eight thousanders, K2 has never been climbed during winter.</p>
    </div>
  </div>
  <div class="grid_item tab">
    <img src="https://webmadewell.com/wp-content/uploads/2018/04/img-bg-grid-tab-kangchenjunga.jpg" alt="Kangchenjunga"></img>
    <div class="title">Kangchenjunga
      <button><span>more info</span></button>
    </div>
    <div class="content">
      <p>Mount Kangchenjunga lies about 125 km (78 mi) east-south-east of Mount Everest. It is the second highest mountain of the Himalayas. Three of the five peaks – Main, Central and South – are on the border between North Sikkim and Nepal. Two peaks are in Nepal's Taplejung District.</p>
      <p>Kangchenjunga Main is the highest mountain in India, and the easternmost of the mountains higher than 8,000 m (26,000 ft). It is called Five Treasures of Snow after its five high peaks, and has always been worshipped by the people of Darjeeling and Sikkim.</p>
    </div>
  </div>
</div>

Let’s give the HTML content CSS rules for mobile screen view first.

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
body {
  font-family: &quot;Montserrat&quot;, sans-serif;
  line-height: 1.375em;
}
h1 {
  text-align: center;
  color: Teal;
  padding-top: 2.5%;
}
h4 {
  text-align: center;
  color:grey;  
}
div {
  box-sizing: border-box;
}
.grid_items {
  margin: 7.5% 5%;
  color: #ffffff;
  background-color: Teal;
}
.grid_item {
  position: relative;
}
.grid_item img {
  width: 100%;
}
.grid_item &gt; div {
  padding: 20px 30px;
}
.grid_item &gt; .title {
  position: absolute;
  top: 0;
  padding-top: 52.5%;
  width: 100%;
  font-size: 1.5em;
  font-weight: bold;
}
.grid_item button {
  display: none;
  cursor: pointer;
}

We have a stacked block layout for smaller screens. The layout should be displayed like below.

CSS grid tabs mobile view

Let’s set up CSS @media Rules for screens wider than 981 pixels.

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
@media only screen and (min-width: 981px) {
  .grid_items {
    display: grid;
    grid-template-columns: 33.333333% 33.333333% 33.333333%;
    grid-column-gap: 0;
  }
  .grid_item button {
    display: block;
    float: right;
    width: 36px;
    height: 36px;
    margin-top: -6px;
    background-color: #ffffff;
    border: none;
  }
  .grid_item button &gt; span {
    display: none;
  }
  .grid_item button::before {
    content: &quot;+&quot;;
    font-size: 3em;
    line-height: 0.625em;
    color: Teal;
  }
  .grid_item.active button::before {
    content: &quot;-&quot;;
  }
  .grid_item &gt; .content {
    transform: translatex(-2000px);
    opacity: 0;
    width: 300%;
    transition: transform 0.3s ease-in-out 0s, opacity 0.3s ease-in-out 0.3s;
  }
  .grid_item:nth-child(3n + 2) &gt; .content {
    margin-left: -100%;
  }
  .grid_item:nth-child(3n) &gt; .content {
    margin-left: -200%;
  }
  .grid_item.active {
    z-index: 1;
  }
  .grid_item.active &gt; .content {
    transform: translatey(0);
    opacity: 1;
  }
  .grid_item img {
    opacity: 0.5;
    transition: all 0.3s ease-in-out;
  }
  .grid_item.active img {
    opacity: 1;
    transform: translatey(-20%);
  }
  .grid_item &gt; .title {
    transition: margin 0.3s ease-in-out;
  }
  .grid_item.active &gt; .title {
    margin-top: 10%;
  }
}

Lastly, let’s add a very simple jQuery script to initiate the tabs in action.

1
2
3
4
$(&quot;button&quot;).click(function() {
  $(this).parents(&quot;.grid_item&quot;).toggleClass(&quot;active&quot;);
  $(this).parents(&quot;.grid_item&quot;).siblings(&quot;.grid_item&quot;).removeClass(&quot;active&quot;);
});

With the CSS and jQuery in place, the layout should look like below.

CSS grid tabs desktop view

You will be able to see the tabs in action when you click the buttons next to titles.
You can also play it in here at my CodePen page.

See the Pen Adding tabs into CSS grid using simple jQuery by WebMadeWell (@webmadewell) on CodePen.32161