{"id":306971,"date":"2020-04-24T12:29:00","date_gmt":"2020-04-24T19:29:00","guid":{"rendered":"https:\/\/css-tricks.com\/?p=306971"},"modified":"2022-07-12T09:25:01","modified_gmt":"2022-07-12T16:25:01","slug":"how-to-make-a-css-only-carousel","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/how-to-make-a-css-only-carousel\/","title":{"rendered":"How to Make a CSS-Only Carousel"},"content":{"rendered":"\n

We mentioned a way to make a CSS-only carousel in a recent issue of the newsletter<\/a> and I thought that a more detailed write up would be interesting and capture some of my thoughts on making one.<\/p>\n\n\n\n\n\n\n\n

So, here\u2019s what we\u2019re making today:<\/p>\n\n\n\n

<div class=\"wrapper\">\n  <nav class=\"lil-nav\"><\/nav>\n  <div class=\"gallery\"><\/div>\n<\/div><\/code><\/pre>\n\n\n\n

Next, we can add images! For this little example, I checked out our list of sites with high quality images that you can use for free<\/a> and went with Unsplash<\/a>.<\/p>\n\n\n\n

After saving images with the CodePen asset manager, I started adding the URLs to the nav<\/code> element:<\/p>\n\n\n\n

<nav class=\"lil-nav\">\n  <a href=\"#image-1\">\n    <img class=\"lil-nav__img\" src=\"...\" alt=\"Yosemite\" \/>\n  <\/a>\n  <a href=\"#image-2\">\n    <img class=\"lil-nav__img\" src=\"...\" alt=\"Basketball hoop\" \/>\n  <\/a>\n  <!-- more images go here --> \n<\/nav><\/code><\/pre>\n\n\n\n

See that the href<\/code> to each of these links is pointing to an ID? That\u2019s because if we look at the demo again, we want to be able to click an image and then we want to it to hop to the larger version of that image in the gallery to the right.<\/p>\n\n\n\n

So, now we can start to add these images to the large gallery, too\u2026<\/p>\n\n\n\n

<div class=\"gallery\">\n  <img class=\"gallery__img\" id=\"image-1\" src=\"...\" alt=\"Yosemite\" \/>\n  <img class=\"gallery__img\" id=\"image-2\" src=\"...\" alt=\"Basketball hoop\" \/>\n  <!-- more images go here --> \n<\/div><\/code><\/pre>\n\n\n\n

Nifty. Next is the fun part: styling this bad boy. We can use a grid layout the parent .wrapper<\/code> and set some smart defaults for the img<\/code> element:<\/p>\n\n\n\n

img {\n  display: block;\n  max-width: 100%;\n}\n\n.wrapper {\n  display: grid;\n  grid-template-columns: 1fr 5fr;\n  grid-gap: 20px;\n}<\/code><\/pre>\n\n\n\n