How to make a carousel using only HTML and CSS (no JavaScript required!)

Published

Recently, I've been working on a site that uses a CMS that's a bit limiting. I can add my own HTML and CSS to the site, but JavaScript is off-limits.

The designs I'm working from included a carousel. I had some ideas of how I could make that work using CSS animations and the transform property, but that would leave me with a carousel that scrolled automatically and didn't allow for user input which wasn't really what I was looking for. After some thinking, I eneded up with a solution that uses absolute positioning and the :target pseudo-selector to change the z-index and opacity of our carousel items to cycle through them. It looks something like this:

Let's build one!

The structure

The structure of our carousel goes something like this: We have a main div.carousel-wrapper that gives our carousel its size. Inside our wrapper, we have span.hidden-target elements with unique IDs that act as targets for our carousel items controls, and div.carousel-item elements that hold the content of each of our carousel items.

Each of our div.carousel-item elements have some content within them, and two links, a.arrow-prev and a.arrow-next, which we use to cycle between the carousel items.

Because our individual carousel items will be position: absolute (so we can stack them on top of eachother), we have to set the div.carousel-wrapper's height manually, and it makes sense to do this inline. We're going to try to offload as much of our CSS to our external stylesheet, but some of the items we'll have to write inline in order to make our carousel reusable and scalable.

I'm also using inline CSS to set the background image of two of our div.carousel-item elements to make them a little more vibrant, but we'll leave that out below so that our markup is more readable.

1<!--Here's our main wrapper.
2Since our carousel items get their size from their parent,
3we have to specify its height.-->
4<div class="carousel-wrapper" style="height: 400px;">
5  <!--The carousel uses regular links to cycle through each item.
6  The links actually target these display: none; spans so our page doesn't
7  jump like it normally would when using jump links.-->
8  <span id="target-item-1"></span>
9  <span id="target-item-2"></span>
10  <span id="target-item-3"></span>
11  <!--Here are our carousel items.
12  Each has a 'carousel-item' class, which we use for shared styling
13  and an item-# class, which we use to control its opacity
14  depending on which target-item-# is currently targeted-->
15  <div class="carousel-item item-1">
16    <!--We can add any content in here, just make sure that
17    your .carousel-wrapper is big enough to hold all the content.-->
18    <h2>Item 1</h2>
19    <p>Content goes here.</p>
20    <!--Here are the links that control the carousel! Make sure
21    the href of each one is pointing to the right target-item-#
22    so that the carousel cycles in sequence.-->
23    <a class="arrow arrow-prev" href="#target-item-3"></a>
24    <a class="arrow arrow-next" href="#target-item-2"></a>
25  </div>
26  <!--And here are a couple more carousel items so that
27  we have some content to scroll to. Notice the 'light' class?
28  Royal blue is a pretty dark background color, so we'll add a CSS
29  rule to make the text white if a carousel item has this class-->
30  <div class="carousel-item item-2 light" style="background-color: royalblue;">
31    <h2>Item 2</h2>
32    <p>Content goes here.</p>
33    <a class="arrow arrow-prev" href="#target-item-1"></a>
34    <a class="arrow arrow-next" href="#target-item-3"></a>
35  </div>
36  <div class="carousel-item item-3">
37    <h2>Item 3</h2>
38    <p>Content goes here.</p>
39    <a class="arrow arrow-prev" href="#target-item-2"></a>
40    <a class="arrow arrow-next" href="#target-item-1"></a>
41  </div>
42</div>
43

That's it for our HTML. It's surprisingly light. The CSS (SCSS, in this case) is where the magic happens.

The styles

1/_ Here's where our carousel begins, with the main wrapper being
2relatively positioned, so that our absolutely positioned items are
3in the right place. _/
4.carousel-wrapper {
5position: relative;
6
7/_ Our absolutely positioned carousel items span the width and
8height of its parent. We're making them transparent by default so
9that they fade in when we cycle through them using the arrow links. _/
10.carousel-item {
11position: absolute;
12top: 0;
13bottom: 0;
14left: 0;
15right: 0;
16padding: 25px 50px;
17opacity: 0;
18transition: all 0.5s ease-in-out;
19
20    /* Did you notice the 50px left, right padding up above? It's so
21    we can position our arrow links! Each one will be 50px wide. Also,
22    I'm using empty links with a background image so that the links
23    look like arrows. Make sure you swap out that URL with an actual
24    URL so that your arrow links aren't just transparent rectangles. */
25    .arrow {
26    position: absolute;
27    top: 0;
28    display: block;
29    width: 50px;
30    height: 100%;
31    -webkit-tap-highlight-color: rgba(0,0,0,0);
32    background: url("/carousel-arrow-dark.png") 50% 50% / 20px no-repeat;
33
34      /* Let's put our arrow to go back on the left. */
35      &.arrow-prev {
36        left: 0;
37      }
38
39      /* And our arrow to go forward on the right. Since I'm using
40      the same arrow image for both my arrows, I'm rotating this one by
41      180 degrees so that it points in the right direction */
42      &.arrow-next {
43        right: 0;
44        -webkit-transform: rotate(180deg);
45                transform: rotate(180deg);
46      }
47    }
48
49    /* I really like how these carousel items look on a dark image
50    background, so if a .carousel-item div has the class 'light',
51    we'll make its text color white, and use a white arrow instad of
52    a dark gray one. Again, make sure this arrow image exists somewhere */
53    &.light {
54      color: white;
55
56      .arrow {
57        background: url("/carousel-arrow-light.png") 50% 50% / 20px no-repeat;
58      }
59    }
60
61    /* Let's use using some media queries to resize the arrows
62    on smaller devices.*/
63    @media (max-width: 480px) {
64      .arrow, &.light .arrow {
65        background-size: 10px;
66        background-position: 10px 50%;
67      }
68    }
69
70}
71
72/_ Let's set our jump link targets display: none; so that we're not
73making the browser jump to the top of the carousel whenever a user
74clicks on one of our arrow links. This attribute selector will target
75any element whose id starts with 'target-item'. _/
76[id^="target-item"] {
77display: none;
78}
79
80/_ So, up above we made all our carousel items transparent, which means
81that on page-load, we'd have a big empty box where our carousel should be.
82Let's set our first item's opacity to 1 so that it displays instead. Also,
83we're setting its z-index to 2, so that it's positioned on top of the
84other carousel items. _/
85.item-1 {
86z-index: 2;
87opacity: 1;
88}
89
90/_ But we don't want the first item to ALAWYS be opacity: 1; otherwise
91it would peek through when cycling between items two and above. _/
92\*:target ~ .item-1 {
93opacity: 0;
94}
95
96/_ ...but if #target-item-1 is targeted, well we do want the first item
97to show up, so we're selecting it with the ~ sibling selector and
98setting its opacity to 1 again :-) _/
99#target-item-1:target ~ .item-1 {
100opacity: 1;
101}
102
103/_ If any other target-item-# is targeted, let's select it using the sibling
104selector, make it fade in, and place it on top of the pile using z-index: 3.
105Here's where you'd add more target items if your carousel has more than three
106items. It might be worth adding like 10 items right off the bat. _/
107#target-item-2:target ~ .item-2, #target-item-3:target ~ .item-3 {
108z-index: 3;
109opacity: 1;
110}
111}
112

And that's it! You have a carousel that's fully functional and is 100% HTML and CSS! We only made a carousel with three items, but you can keep adding items, just make sure you add more target items, and you link up your arrow links correctly.