• We just launched and are currently in beta. Join us as we build and grow the community.

Step-by-Step Guide to Creating a Carousel with HTML, CSS, and JavaScript

GeNiuS

Pro Gamer
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
192
Likes
73
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, I will guide you through the process of creating a Carousel, also known as an Image Slider, using HTML, CSS, and JavaScript. Carousels are widely used in modern web design to showcase images, content, or products in an interactive and visually appealing way.

The primary goal of this tutorial is to provide you with valuable insights and practical ideas for building an image slider that can be customized and integrated into your own projects. Whether you're designing a portfolio, an e-commerce website, or any application that benefits from dynamic content display, this feature can enhance user engagement and overall aesthetics.

By the end of this tutorial, you'll have a fully functional carousel that you can adapt to suit your future web development needs.

What is Carousel?

The Carousel I am referring to is an image slider commonly used in web applications. This feature is designed to display a series of images, allowing users to navigate between slides seamlessly. With intuitive controls, users can move to the previous or next image slide, enhancing their browsing experience.

Image sliders are a popular element in modern web design, often used in portfolios, product showcases, or landing pages to present visual content in an engaging and interactive way. By implementing this feature, you can create an eye-catching and functional component that improves the overall appeal of your web application.

Let’s learn how to build a carousel step-by-step, ensuring it’s both user-friendly and visually appealing for your projects.

Step 1: Creating the Interface

To get started, let’s design the sample web application interface by creating the main HTML file. Name the file index.html

. This file will include the HTML elements required for implementing the carousel feature. Refer to the script provided below for details.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  6. <title

    >

    Simple Carousel using HTML, CSS, and JS</

    title

    >
  7. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  8. <script

    src

    =

    "https://code.jquery.com/jquery-3.7.1.min.js"

    crossorigin=

    "anonymous"

    ></

    script

    >
  9. </

    head

    >
  10. <body

    >
  11. <div

    id

    =

    "main-wrapper"

    >
  12. <h2

    id

    =

    "page-title"

    >

    Simple Carousel using HTML, CSS, and JS</

    h2

    >

  13. <div

    class

    =

    "carousel-container"

    >
  14. <!-- Carousel Items -->
  15. <div

    class

    =

    "carousel"

    >
  16. <div

    class

    =

    "carousel-item active"

    >
  17. <img

    src

    =

    "images/1.jpg"

    alt

    =

    "Sample Image 1"

    >
  18. </

    div

    >
  19. <div

    class

    =

    "carousel-item"

    >
  20. <img

    src

    =

    "images/2.jpg"

    alt

    =

    "Sample Image 2"

    >
  21. </

    div

    >
  22. <div

    class

    =

    "carousel-item"

    >
  23. <img

    src

    =

    "images/3.jpg"

    alt

    =

    "Sample Image 3"

    >
  24. </

    div

    >
  25. </

    div

    >
  26. <!-- Carousel Buttons -->
  27. <div

    class

    =

    "carousel_actions"

    >
  28. <button

    class

    =

    "carousel-btn carousel-prev-button"

    aria-label

    =

    "Previous Slide"

    ><</

    button

    >
  29. <button

    class

    =

    "carousel-btn carousel-next-button"

    aria-label

    =

    "Next Slide"

    >

    ></

    button

    >
  30. </

    div

    >
  31. </

    div

    >
  32. </

    div

    >
  33. <script

    src

    =

    "script.js"

    ></

    script

    >
  34. </

    body

    >
  35. </

    html

    >

Step 2: Creating the CSS

Next, we’ll create the CSS file for the application. Save the file with the name style.css

. This file will include the styles and designs for various elements of the application, including the components of the carousel.

Below, I have provided the CSS script to style the carousel and other elements of the application. Feel free to customize the design according to the requirements of your project.

  1. html,

    body {
  2. margin

    :

    unset;
  3. padding

    :

    unset;
  4. width

    :

    100%

    ;
  5. max-width

    :

    100%

    ;
  6. height

    :

    100%

    ;
  7. max-height

    :

    100%

    ;
  8. }

  9. body {
  10. display

    :

    flex;
  11. flex-flow

    :

    column;
  12. align-items

    :

    center

    ;
  13. justify-content

    :

    center

    ;
  14. overflow

    :

    auto

    ;
  15. background-image

    :

    linear-gradient(

    to top

    ,

    #cfd9df

    0%

    ,

    #e2ebf0

    100%

    )

    ;
  16. }

  17. #main-wrapper

    {
  18. display

    :

    block

    ;
  19. width

    :

    100%

    ;
  20. max-width

    :

    500px

    ;
  21. padding

    :

    15px

    ;
  22. }
  23. #page-title

    {
  24. text-align

    :

    center

    ;
  25. font-weight

    :

    bolder

    ;
  26. }
  27. .carousel-container{
  28. position

    :

    relative

    ;
  29. width

    :

    100%

    ;
  30. max-width

    :

    500px

    ;
  31. }

  32. .carousel{
  33. position

    :

    relative

    ;
  34. width

    :

    100%

    ;
  35. height

    :

    300px

    ;
  36. overflow

    :

    hidden

    ;
  37. }

  38. .carousel-item{
  39. position

    :

    absolute

    ;
  40. width

    :

    100%

    ;
  41. height

    :

    100%

    ;
  42. display

    :

    none

    ;
  43. }
  44. .carousel-item

    .active{
  45. display

    :

    block

    ;
  46. }
  47. .carousel-item

    .carousel-prev-transition-from,
  48. .carousel-item

    .carousel-prev-transition-to,
  49. .carousel-item

    .carousel-next-transition-from,
  50. .carousel-item

    .carousel-next-transition-to
  51. {
  52. display

    :

    block

    ;
  53. transition

    :

    all .5s

    ease-in-out;
  54. }
  55. .carousel-item

    .carousel-prev-transition-to{
  56. transform

    :

    translateX(

    -100%

    )

    ;
  57. }

  58. .carousel-item

    .carousel-next-transition-to{
  59. transform

    :

    translateX(

    100%

    )

    ;
  60. }
  61. .carousel-item

    img {
  62. width

    :

    100%

    ;
  63. height

    :

    100%

    ;
  64. object-fit

    :

    cover;
  65. object-position

    :

    center

    center

    ;
  66. }

  67. .carousel_actions

    {
  68. position

    :

    absolute

    ;
  69. display

    :

    flex;
  70. width

    :

    100%

    ;
  71. height

    :

    100%

    ;
  72. justify-content

    :

    space-between;
  73. top

    :

    0

    ;
  74. }

  75. .carousel-btn

    {
  76. height

    :

    100%

    ;
  77. width

    :

    50px

    ;
  78. font-size

    :

    20px

    ;
  79. font-weight

    :

    900

    ;
  80. color

    :

    #fff

    ;
  81. background

    :

    #00000008

    ;
  82. border

    :

    unset;
  83. cursor

    :

    pointer

    ;
  84. }

  85. .carousel-btn

    :

    hover

    {
  86. background

    :

    #0000001f

    ;
  87. }

Step 3: Creating the JavaScript

Finally, let’s create the main JavaScript (JS) file for the application. Save the new file as script.js

. This file will include the necessary code to make the carousel feature fully functional. Refer to the script provided below for implementation details.

  1. $(

    document)

    .ready

    (

    function

    (

    )

    {

  2. $(

    '.carousel-container'

    )

    .each

    (

    function

    (

    )

    {
  3. var

    carousel =

    $(

    this

    )
  4. // Carousel items
  5. var

    items =

    carousel.find

    (

    ".carousel .carousel-item"

    )

    ;
  6. // Carousel Next Slide Button
  7. var

    next_btn =

    carousel.find

    (

    ".carousel_actions .carousel-next-button"

    )
  8. // Carousel Previous Slide Button
  9. var

    prev_btn =

    carousel.find

    (

    ".carousel_actions .carousel-prev-button"

    )

  10. // Event Listner when Next Slide Button has been triggerd
  11. next_btn.click

    (

    function

    (

    e)

    {
  12. e.preventDefault

    (

    )

    ;
  13. // current active slide
  14. var

    currentSlide =

    carousel.find

    (

    ".carousel .carousel-item.active"

    )
  15. var

    currentSlideIndx =

    currentSlide.index

    (

    )

    ;
  16. // Identifying next slide index
  17. var

    nextSlideIndx =

    currentSlideIndx +

    1

    ;
  18. if

    (

    nextSlideIndx ==

    items.length

    )
  19. nextSlideIndx =

    0

    ;

  20. // Next Slide to Show
  21. var

    nextSlide =

    $(

    items[

    nextSlideIndx]

    )

    ;

  22. // Animate Slide Transition from current active to the next slide
  23. currentSlide.addClass

    (

    "carousel-next-transition-from"

    )
  24. nextSlide.addClass

    (

    "carousel-next-transition-to"

    )
  25. setTimeout(

    function

    (

    )

    {
  26. nextSlide.css

    (

    "transform"

    ,

    "translateX(0%)"

    )
  27. currentSlide.css

    (

    "transform"

    ,

    "translateX(-100%)"

    )
  28. setTimeout(

    function

    (

    )

    {
  29. currentSlide.removeClass

    (

    "carousel-next-transition-from"

    )
  30. currentSlide.removeClass

    (

    "active"

    )
  31. nextSlide.removeClass

    (

    "carousel-next-transition-to"

    )
  32. nextSlide.addClass

    (

    "active"

    )
  33. currentSlide.removeAttr

    (

    "style"

    )
  34. nextSlide.removeAttr

    (

    "style"

    )
  35. }
  36. ,

    1000

    )
  37. }

    ,

    10

    )
  38. }

    )

  39. // Event Listner when Next Slide Button has been triggerd
  40. prev_btn.click

    (

    function

    (

    e)

    {
  41. e.preventDefault

    (

    )

    ;
  42. // current active slide
  43. var

    currentSlide =

    carousel.find

    (

    ".carousel .carousel-item.active"

    )
  44. var

    currentSlideIndx =

    currentSlide.index

    (

    )

    ;
  45. // Identifying previous slide index
  46. var

    prevSlideIndx =

    currentSlideIndx -

    1

    ;
  47. if

    (

    prevSlideIndx <

    0

    )
  48. prevSlideIndx =

    (

    items.length

    -

    1

    )

    ;

  49. var

    prevSlide =

    $(

    items[

    prevSlideIndx]

    )

    ;

  50. // Animate Slide Transition from current active to the next slide
  51. currentSlide.addClass

    (

    "carousel-prev-transition-from"

    )
  52. prevSlide.addClass

    (

    "carousel-prev-transition-to"

    )
  53. setTimeout(

    function

    (

    )

    {
  54. prevSlide.css

    (

    "transform"

    ,

    "translateX(0%)"

    )
  55. currentSlide.css

    (

    "transform"

    ,

    "translateX(100%)"

    )
  56. setTimeout(

    function

    (

    )

    {
  57. currentSlide.removeClass

    (

    "carousel-prev-transition-from"

    )
  58. currentSlide.removeClass

    (

    "active"

    )
  59. prevSlide.removeClass

    (

    "carousel-prev-transition-to"

    )
  60. prevSlide.addClass

    (

    "active"

    )
  61. currentSlide.removeAttr

    (

    "style"

    )
  62. prevSlide.removeAttr

    (

    "style"

    )
  63. }
  64. ,

    1000

    )
  65. }

    ,

    10

    )
  66. }

    )
  67. }

    )

  68. }

    )

And there you have it! Now you test the simple application with carousel that we created on your end and see if it works as we wanted.

Here's a screenshot of the application using the source code that I provided on this tutorial.

html-css-js-simple-carousel.jpg


I have also provided the complete source code zip file on this article and it is free to download. The download button is located below this article content. Feel free to download and modify the codes the way you wanted.

I hope this Step-by-Step Guide to Creating a Carousel with HTML, CSS, and JavaScript will help you and you'll find something useful from the source code for your future web application projects.

Explore more on this website for more Tutorials, Free Source Code, and Articles covering various programming languages.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,632

323,159

323,168

Top