{"id":5062,"date":"2020-06-27T10:52:00","date_gmt":"2020-06-27T10:52:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=5062"},"modified":"2020-08-21T11:07:30","modified_gmt":"2020-08-21T11:07:30","slug":"tutorial-create-a-carousel-using-css-scroll-snap-and-javascript","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/tutorial-create-a-carousel-using-css-scroll-snap-and-javascript\/","title":{"rendered":"Tutorial: Create a Carousel using CSS Scroll Snap and JavaScript"},"content":{"rendered":"\n<p>This <strong>tutorial <\/strong>will help you create a <strong>responsive carousel<\/strong> using the <strong>CSS scroll-snap<\/strong> property and very little<strong> JavaScript<\/strong> for navigation. You can use it for products display, or as a gallery, or for your testimonials \u2013 like the example in this tutorial.<\/p>\n\n\n\n<p>So here is what we will be creating:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5063\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/tutorial-create-a-carousel-using-css-scroll-snap-and-javascript\/attachment\/example-testimonials-carousel\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Testimonials-Carousel.gif\" data-orig-size=\"600,327\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example-Testimonials-Carousel\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Testimonials-Carousel-300x164.gif\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Testimonials-Carousel.gif\" loading=\"lazy\" width=\"600\" height=\"327\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Testimonials-Carousel.gif\" alt=\"\" class=\"wp-image-5063\"\/><\/figure>\n\n\n\n<p>This is really just a horizontal scroll section but when you scroll, it \u201csnaps\u201d (auto-adjusts the position of the scroll container) to ensure it displays the full item \u2013 making it behave like a carousel. You can also use the arrows to display next and previous items, which requires JavaScript. You need to have some basic knowledge of HTML, CSS and JavaScript to follow along. On the way, you will learn about the CSS scroll-snap properties.<\/p>\n\n\n\n<p><strong>Let\u2019s get started.<\/strong><\/p>\n\n\n\n<h2>Setting up<\/h2>\n\n\n\n<p>Create a blank HTML document and name it&nbsp;<code>index.html<\/code>. Add the basic HTML skeleton. If you use Visual Studio Code, all you need to do is type \u201c!\u201d and hit enter. You will end up with this.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;!<\/code><code>DOCTYPE<\/code> <code>html&gt;<\/code><code>&lt;<\/code><code>html<\/code> <code>lang<\/code><code>=<\/code><code>\"en\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>head<\/code><code>&gt;<\/code><code>&lt;<\/code><code>meta<\/code> <code>charset<\/code><code>=<\/code><code>\"UTF-8\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>meta<\/code> <code>name<\/code><code>=<\/code><code>\"viewport\"<\/code> <code>content<\/code><code>=<\/code><code>\"width=device-width, initial-scale=1.0\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>title<\/code><code>&gt;Document&lt;\/<\/code><code>title<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>head<\/code><code>&gt;<\/code><code>&lt;<\/code><code>body<\/code><code>&gt;<\/code>&nbsp;<code>&lt;\/<\/code><code>body<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>html<\/code><code>&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The font used in this demo is \u2018Noto Sans\u2019. Let\u2019s embed this font from Google fonts. Add this CDN link below the title tag to embed regular and italic styles of this font.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;<\/code><code>link<\/code> <code>href<\/code><code>=<\/code><code>\"<a href=\"https:\/\/fonts.googleapis.com\/css2?family=Noto\">https:\/\/fonts.googleapis.com\/css2?family=Noto<\/a>+Sans:ital,wght@0,400;0,700;1,400&amp;display=swap\"<\/code> <code>rel<\/code><code>=<\/code><code>\"stylesheet\"<\/code><code>&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Create your stylesheet and name it&nbsp;<code>style.css<\/code>. Link the stylesheet to your HTML document below the Google fonts CDN link using<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;<\/code><code>link<\/code> <code>rel<\/code><code>=<\/code><code>\"stylesheet\"<\/code> <code>href<\/code><code>=<\/code><code>\"style.css\"<\/code><code>&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>I have downloaded three portrait images from&nbsp;<a href=\"https:\/\/www.pexels.com\/search\/portrait\/\" rel=\"noreferrer noopener\" target=\"_blank\">Pexels<\/a>&nbsp;and cropped them square for this demo. You can choose to include your own. Name them&nbsp;<code>testimonial1.jpg<\/code>,&nbsp;<code>testimonial2.jpg<\/code>&nbsp;and&nbsp;<code>testimonial3.jpg<\/code>.<\/p>\n\n\n\n<h3>Creating a basic scroller<\/h3>\n\n\n\n<p>Let\u2019s first create a simple horizontal scroller with three items with width same as that of the scroll container.<\/p>\n\n\n\n<h4>HTML<\/h4>\n\n\n\n<p>Add this markup within body tag in your HTML file:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"testimonials\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"scroller\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"item\"<\/code><code>&gt;<\/code><code>Item 1<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"item\"<\/code><code>&gt;<\/code><code>Item 2<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"item\"<\/code><code>&gt;<\/code><code>Item 3<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Here, the&nbsp;<code>testimonials<\/code>&nbsp;div acts as the wrapper, within which is the horizontal&nbsp;<code>scroller<\/code>, and then three item&nbsp;<code>divs<\/code>.<\/p>\n\n\n\n<h4>CSS<\/h4>\n\n\n\n<p>In&nbsp;<code>style.css<\/code>, begin with some common styles for all elements:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>* {<\/code><code>margin<\/code><code>: <\/code><code>0<\/code><code>;<\/code><code>padding<\/code><code>: <\/code><code>0<\/code><code>;<\/code><code>box-sizing<\/code><code>: <\/code><code>border-box<\/code><code>;<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Add these styles to the&nbsp;<code>body<\/code>&nbsp;element:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>body {<\/code><code>font-family<\/code><code>: <\/code><code>'Noto Sans'<\/code><code>, <\/code><code>sans-serif<\/code><code>;<\/code><code>font-size<\/code><code>: <\/code><code>1em<\/code><code>;<\/code><code>color<\/code><code>: <\/code><code>#4A5568<\/code><code>;<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Restrict the width of the&nbsp;<code>testimonials<\/code>&nbsp;wrapper to about 800px and center it.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>.testimonials {<\/code><code>max-width<\/code><code>: <\/code><code>800px<\/code><code>;<\/code><code>margin<\/code><code>: <\/code><code>auto<\/code><code>;<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Add these basics styles to see the scroller at work.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>.scroller {<\/code><code>overflow-x<\/code><code>: <\/code><code>scroll<\/code><code>;<\/code><code>display<\/code><code>: <\/code><code>flex<\/code><code>;<\/code><code>}<\/code><code>.item {<\/code><code>min-width<\/code><code>: <\/code><code>100%<\/code><code>;<\/code><code>min-height<\/code><code>: <\/code><code>200px<\/code><code>; <\/code><code>\/* To start with *\/<\/code><code>background-color<\/code><code>: <\/code><code>#EDF2F7<\/code><code>; <\/code><code>\/* For demo *\/<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>With this, you can see a horizontal scroll section like this.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-attachment-id=\"5064\" data-permalink=\"https:\/\/azoora.com\/blog\/code\/tutorial-create-a-carousel-using-css-scroll-snap-and-javascript\/attachment\/example-carousel-01\/#main\" data-orig-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Carousel-01.png\" data-orig-size=\"1024,266\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example-Carousel-01\" data-image-description=\"\" data-medium-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Carousel-01-300x78.png\" data-large-file=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Carousel-01-1024x266.png\" loading=\"lazy\" width=\"1024\" height=\"266\" src=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Carousel-01-1024x266.png\" alt=\"\" class=\"wp-image-5064\" srcset=\"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Carousel-01.png 1024w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Carousel-01-300x78.png 300w, https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Example-Carousel-01-768x200.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3>Scroll Snapping<\/h3>\n\n\n\n<p>With CSS scroll snap, it is possible to enforce the scroll position (smoothly moving to the enforced position) once the user stops scrolling. This works by applying two primary properties \u2013&nbsp;<code>scroll-snap-type<\/code>&nbsp;and&nbsp;<code>scroll-snap-align<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>scroll-snap-type<\/strong>&nbsp;property is applied on the&nbsp;<strong>parent<\/strong>&nbsp;container (In our case, it\u2019s the&nbsp;<code>scroller<\/code>&nbsp;div). It takes two arguments \u2013 the snap direction (&nbsp;<code>x | y | both<\/code>&nbsp;) and the snap behavior (&nbsp;<code>mandatory | proximity<\/code>&nbsp;). We need to use&nbsp;<code>x<\/code>&nbsp;for the direction because we are scrolling horizontally. For the behavior,&nbsp;<code>mandatory<\/code>&nbsp;means the browser has to snap to the position, while&nbsp;<code>proximity<\/code>&nbsp;is less strict. We will use&nbsp;<code>mandatory<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>scroll-snap-align<\/strong>&nbsp;property is applied to the child items. It determines the snap point. It takes one or two arguments for&nbsp;<code>x<\/code>&nbsp;or\/and&nbsp;<code>y<\/code>&nbsp;direction (&nbsp;<code>none | start | end | center<\/code>&nbsp;). In our case, this property can have any value except for&nbsp;<code>none<\/code>, because our items are 100% width of the parent container.&nbsp;<a href=\"https:\/\/webdesign.tutsplus.com\/tutorials\/how-to-scroll-snap-using-css--cms-30333\" rel=\"noreferrer noopener\" target=\"_blank\">You can dig deeper into the CSS Scroll Snap concept here<\/a>.<\/p>\n\n\n\n<p>Add these properties to the&nbsp;<code>scroller<\/code>&nbsp;and&nbsp;<code>item<\/code>&nbsp;divs.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>.scroller {<\/code><code>\/* Existing styles here *\/<\/code><code>scroll-snap-type: x mandatory;<\/code><code>}<\/code><code>.item {<\/code><code>\/* Existing styles here *\/<\/code><code>scroll-snap-align: <\/code><code>center<\/code><code>;<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>That\u2019s all you need for the magic! Now check your browser and scroll horizontally. You will notice that the browser strictly snaps to display a full item once you stop scrolling at any point. With this, the primary carousel behavior is achieved.<\/p>\n\n\n\n<h3>Adding content<\/h3>\n\n\n\n<p>Let\u2019s add some content to the items.<\/p>\n\n\n\n<h4>HTML<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"item\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>img<\/code> <code>src<\/code><code>=<\/code><code>\"testimonial1.jpg\"<\/code> <code>alt<\/code><code>=<\/code><code>\"Elise\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"card\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>p<\/code><code>&gt;<\/code><code>Eu rebum molestie per. No nostrud imperdiet abhorreant qui, hinc incorrupte vix ad. Vel id aliquip oblique. Primis feugait sit an, brute illud convenire duo ea. Graece tempor eripuit sed in.<\/code><code>&lt;\/<\/code><code>p<\/code><code>&gt;<\/code><code>&lt;<\/code><code>span<\/code><code>&gt;Elise&lt;\/<\/code><code>span<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"item\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>img<\/code> <code>src<\/code><code>=<\/code><code>\"testimonial2.jpg\"<\/code> <code>alt<\/code><code>=<\/code><code>\"John\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"card\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>p<\/code><code>&gt;<\/code><code>Vitae pericula maluisset ut mei, pro eleifend gubergren eu. Et his brute graeci. Affert ponderum ei vel, dolorum accumsan ea has. Sea oblique salutatus ei, simul lucilius pri et. Errem melius temporibus ut eos<\/code><code>&lt;\/<\/code><code>p<\/code><code>&gt;<\/code><code>&lt;<\/code><code>span<\/code><code>&gt;John&lt;\/<\/code><code>span<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"item\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>img<\/code> <code>src<\/code><code>=<\/code><code>\"testimonial3.jpg\"<\/code> <code>alt<\/code><code>=<\/code><code>\"Imani\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>div<\/code> <code>class<\/code><code>=<\/code><code>\"card\"<\/code><code>&gt;<\/code><code>&lt;<\/code><code>p<\/code><code>&gt;<\/code><code>Ne est virtute indoctum, quaeque vituperata cum ut. Te nisl quaeque pri. Vix ex autem latine, mel ne nobis scaevola, ei est dolor utinam commune. Pri unum doctus in, cu primis pertinax eos.<\/code><code>&lt;\/<\/code><code>p<\/code><code>&gt;<\/code><code>&lt;<\/code><code>span<\/code><code>&gt;Imani&lt;\/<\/code><code>span<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><code>&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4>CSS<\/h4>\n\n\n\n<p>Time to style it up.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>.testimonials {<\/code><code>\/* Existing styles here *\/<\/code><code>padding<\/code><code>: <\/code><code>15px<\/code><code>;<\/code><code>text-align<\/code><code>: <\/code><code>center<\/code><code>;<\/code><code>}<\/code><code>.item {<\/code><code>\/* Existing styles here *\/<\/code><code>background-color<\/code><code>: <\/code><code>white<\/code><code>; <\/code><code>\/* Change the value to white *\/<\/code><code>margin-bottom<\/code><code>: <\/code><code>10px<\/code><code>;<\/code><code>padding<\/code><code>: <\/code><code>0<\/code> <code>50px<\/code><code>;<\/code><code>}<\/code><code>.item img {<\/code><code>margin<\/code><code>: <\/code><code>15px<\/code> <code>auto<\/code> <code>-60px<\/code><code>;<\/code><code>width<\/code><code>: <\/code><code>120px<\/code><code>;<\/code><code>height<\/code><code>: <\/code><code>120px<\/code><code>;<\/code><code>border<\/code><code>: <\/code><code>solid<\/code> <code>4px<\/code> <code>#ffffff<\/code><code>;<\/code><code>border-radius<\/code><code>: <\/code><code>60px<\/code><code>;<\/code><code>box-shadow<\/code><code>: <\/code><code>0<\/code> <code>4px<\/code> <code>6px<\/code> <code>-1px<\/code> <code>rgba<\/code><code>(<\/code><code>0<\/code><code>, <\/code><code>0<\/code><code>, <\/code><code>0<\/code><code>, <\/code><code>0.1<\/code><code>);<\/code><code>z-index<\/code><code>: <\/code><code>2<\/code><code>;<\/code><code>}<\/code><code>.card {<\/code><code>background-color<\/code><code>: <\/code><code>rgb<\/code><code>(<\/code><code>237<\/code><code>, <\/code><code>242<\/code><code>, <\/code><code>247<\/code><code>);<\/code><code>padding<\/code><code>: <\/code><code>80px<\/code> <code>40px<\/code> <code>40px<\/code><code>;<\/code><code>border-radius<\/code><code>: <\/code><code>10px<\/code><code>;<\/code><code>box-shadow<\/code><code>: <\/code><code>0<\/code> <code>4px<\/code> <code>6px<\/code> <code>-1px<\/code> <code>rgba<\/code><code>(<\/code><code>0<\/code><code>, <\/code><code>0<\/code><code>, <\/code><code>0<\/code><code>, <\/code><code>0.1<\/code><code>);<\/code><code>z-index<\/code><code>: <\/code><code>1<\/code><code>;<\/code><code>}<\/code><code>.card p {<\/code><code>font-style<\/code><code>: <\/code><code>italic<\/code><code>;<\/code><code>line-height<\/code><code>: <\/code><code>1.6<\/code><code>;<\/code><code>}<\/code><code>.card span {<\/code><code>display<\/code><code>: <\/code><code>block<\/code><code>;<\/code><code>margin-top<\/code><code>: <\/code><code>20px<\/code><code>;<\/code><code>color<\/code><code>: <\/code><code>teal<\/code><code>;<\/code><code>font-weight<\/code><code>: <\/code><code>bold<\/code><code>;<\/code><code>letter-spacing<\/code><code>: <\/code><code>0.05em<\/code><code>;<\/code><code>text-transform<\/code><code>: <\/code><code>uppercase<\/code><code>;<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Our carousel has taken shape. The only thing pending now is the navigation functionality.<\/p>\n\n\n\n<h3>Adding navigation<\/h3>\n\n\n\n<p>In this demo, I have added simple arrows with&nbsp;<code>&lt;<\/code>&nbsp;and&nbsp;<code>&gt;<\/code>&nbsp;symbols for the navigation buttons. You can also use icons or images.<\/p>\n\n\n\n<h4>HTML<\/h4>\n\n\n\n<p>Add these two lines immediately after the&nbsp;<code>scroller<\/code>&nbsp;div, but within the&nbsp;<code>testimonial<\/code>&nbsp;div:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;<\/code><code>span<\/code> <code>class<\/code><code>=<\/code><code>\"btn prev\"<\/code><code>&gt;&amp;lt;&lt;\/<\/code><code>span<\/code><code>&gt;<\/code><code>&lt;<\/code><code>span<\/code> <code>class<\/code><code>=<\/code><code>\"btn next\"<\/code><code>&gt;&amp;gt;&lt;\/<\/code><code>span<\/code><code>&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4>CSS<\/h4>\n\n\n\n<p>Add these to your stylesheet for positioning and styling the navigation buttons:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>.testimonials {<\/code><code>\/* Existing styles here *\/<\/code><code>position<\/code><code>: <\/code><code>relative<\/code><code>;<\/code><code>}<\/code><code>.testimonials .btn {<\/code><code>position<\/code><code>: <\/code><code>absolute<\/code><code>;<\/code><code>top<\/code><code>: <\/code><code>50%<\/code><code>;<\/code><code>margin-top<\/code><code>: <\/code><code>20px<\/code><code>;<\/code><code>height<\/code><code>: <\/code><code>30px<\/code><code>;<\/code><code>width<\/code><code>: <\/code><code>30px<\/code><code>;<\/code><code>border-radius<\/code><code>: <\/code><code>15px<\/code><code>;<\/code><code>background-color<\/code><code>: <\/code><code>rgba<\/code><code>(<\/code><code>95<\/code><code>, <\/code><code>106<\/code><code>, <\/code><code>117<\/code><code>, <\/code><code>0.3<\/code><code>);<\/code><code>z-index<\/code><code>: <\/code><code>1<\/code><code>;<\/code><code>line-height<\/code><code>: <\/code><code>30px<\/code><code>;<\/code><code>text-align<\/code><code>: <\/code><code>center<\/code><code>;<\/code><code>color<\/code><code>: <\/code><code>white<\/code><code>;<\/code><code>font-weight<\/code><code>: <\/code><code>bold<\/code><code>;<\/code><code>}<\/code><code>.testimonials .btn:hover{<\/code><code>background-color<\/code><code>: <\/code><code>rgba<\/code><code>(<\/code><code>95<\/code><code>, <\/code><code>106<\/code><code>, <\/code><code>117<\/code><code>, <\/code><code>0.5<\/code><code>);<\/code><code>cursor<\/code><code>: <\/code><code>pointer<\/code><code>;<\/code><code>}<\/code><code>.testimonials .btn.next {<\/code><code>right<\/code><code>: <\/code><code>15px<\/code><code>;<\/code><code>}<\/code><code>.testimonials .btn.prev {<\/code><code>left<\/code><code>: <\/code><code>15px<\/code><code>;<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4>JavaScript<\/h4>\n\n\n\n<p>We need some <strong>JavaScript <\/strong>to make the buttons work. Add this script before closing the\u00a0<code>body<\/code>\u00a0tag.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>&lt;script&gt;<\/code><code>const testimonials = document.querySelector(<\/code><code>'.testimonials'<\/code><code>);<\/code><code>const scroller = testimonials.querySelector(<\/code><code>'.scroller'<\/code><code>);<\/code><code>const nextBtn = testimonials.querySelector(<\/code><code>'.btn.next'<\/code><code>);<\/code><code>const prevBtn = testimonials.querySelector(<\/code><code>'.btn.prev'<\/code><code>);<\/code><code>const itemWidth = testimonials.querySelector(<\/code><code>'.item'<\/code><code>).clientWidth;<\/code>&nbsp;<code>nextBtn.addEventListener(<\/code><code>'click'<\/code><code>, scrollToNextItem);<\/code><code>prevBtn.addEventListener(<\/code><code>'click'<\/code><code>, scrollToPrevItem);<\/code>&nbsp;<code>function<\/code> <code>scrollToNextItem() {<\/code><code>scroller.scrollBy({left: itemWidth, top: 0, behavior: <\/code><code>'smooth'<\/code><code>});<\/code><code>}<\/code><code>function<\/code> <code>scrollToPrevItem() {<\/code><code>scroller.scrollBy({left: -itemWidth, top: 0, behavior: <\/code><code>'smooth'<\/code><code>});<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In this code above, we have added \u2018click\u2019 event handlers to the&nbsp;<code>prev<\/code>&nbsp;and&nbsp;<code>next<\/code>&nbsp;buttons. The functionality of moving to the previous or next item is achieved using the&nbsp;<code>scrollBy<\/code>&nbsp;method. Check out&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/scrollBy\" rel=\"noreferrer noopener\" target=\"_blank\">the syntax and examples of&nbsp;<code>element.scrollBy<\/code>&nbsp;method<\/a>.<\/p>\n\n\n\n<p>The option&nbsp;<code>behavior: \u2018smooth\u2019<\/code>&nbsp;gives a smooth sliding effect. Please note that&nbsp;<a href=\"https:\/\/caniuse.com\/#feat=mdn-api_scrolltooptions_behavior\" rel=\"noreferrer noopener\" target=\"_blank\">this effect is not supported in IE 11 and a few older versions of other browsers<\/a>.<\/p>\n\n\n\n<h4>Making it loop<\/h4>\n\n\n\n<p>You can optionally make this carousel loop &#8211; clicking on \u2018next\u2019 button for the last item makes the carousel start from beginning and similar behavior for \u2018prev\u2019 button, by changing the methods in your script to this:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><tbody><tr><td><code>function<\/code> <code>scrollToNextItem() {<\/code><code>if<\/code><code>(scroller.scrollLeft &lt; (scroller.scrollWidth - itemWidth))<\/code><code>\/\/ The scroll position is not at the beginning of last item<\/code><code>scroller.scrollBy({left: itemWidth, top: 0, behavior:<\/code><code>'smooth'<\/code><code>});<\/code><code>else<\/code><code>\/\/ Last item reached. Go back to first item by setting scroll position to 0<\/code><code>scroller.scrollTo({left: 0, top: 0, behavior:<\/code><code>'smooth'<\/code><code>});<\/code><code>}<\/code><code>function<\/code> <code>scrollToPrevItem() {<\/code><code>if<\/code><code>(scroller.scrollLeft != 0)<\/code><code>\/\/ The scroll position is not at the beginning of first item<\/code><code>scroller.scrollBy({left: -itemWidth, top: 0, behavior:<\/code><code>'smooth'<\/code><code>});<\/code><code>else<\/code><code>\/\/ This is the first item. Go to last item by setting scroll position to scroller width<\/code><code>scroller.scrollTo({left: scroller.scrollWidth, top: 0, behavior:<\/code><code>'smooth'<\/code><code>});<\/code><code>}<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3>Making it fully responsive<\/h3>\n\n\n\n<p>What we have now is already responsive. Except that, for browser widths smaller than&nbsp;<code>480px<\/code>, the card becomes too narrow and long. So let\u2019s reduce some padding for the item and card for this width using media queries.<\/p>\n\n\n\n<p>Add this to your stylesheet:<\/p>\n\n\n\n<p>@media screen and (max-width: 480px) {<br>.item {<br>padding: 0 30px;<br>}<br>.card {<br>padding: 80px 30px 30px;<br>}<br>}<\/p>\n\n\n\n<p>This is it! You have successfully created a responsive carousel for your testimonials section with the least amount of JavaScript, and also learned about the new&nbsp;<code>scroll-snap<\/code>&nbsp;properties in CSS. You can modify this carousel to suit your needs. Try and explore using this carousel for images with varying width and watch how it behaves.<\/p>\n\n\n\n<p><strong>Download <\/strong>the <strong>complete source code<\/strong> for your use from <strong>below<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>Download<\/strong>: <a href=\"https:\/\/1stwebdesigner.com\/wp-content\/uploads\/2020\/08\/testimonials.zip\">Link<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will help you create a responsive carousel using the CSS scroll-snap property and very little JavaScript for navigation. You can use it for products display, or as a gallery, or for your testimonials \u2013 like the example in this tutorial. So here is what we will be creating: This is really just a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":5065,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,8,12,62],"tags":[25,56,94,146,116,87,110,217,16],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2020\/08\/Creating-Carousel-Using-CSS-Snap-Scroll-And-Javascript.png","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-1jE","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5062"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=5062"}],"version-history":[{"count":1,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5062\/revisions"}],"predecessor-version":[{"id":5066,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/5062\/revisions\/5066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/5065"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=5062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=5062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=5062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}