How to create a review carousel using JavaScript ?
Last Updated :
18 Apr, 2025
In this article, we have created a review carousel using JavaScript. We have also used basic HTML and CSS for styling. The JavaScript carousel is a slideshow component that cycles through a set of images or content. It uses JavaScript to control the transitions and user interactions, providing an interactive and dynamic experience. Review Carousel is used to display the reviews.
Approach:
- In the body tag, create a nested div tag with a class name containing the reviewer image, name, and text.
- Add previous and next buttons to check previous and next reviews, respectively.
- For styling, add some CSS properties in the style tag like alignment, font size, padding, margin, etc.
- Create a function using JavaScript in the script tag to display only one review at a time.
Example: Create a review carousel using the above approach.
HTML Code: As in the first two steps, we will create a nested div tag and two buttons in the body tag.
Note: In the button tag, we have specified an attribute onclick. The onclick event attribute works when the user clicks the button. It will execute the function when the button is clicked.
CSS code: For styling, we have used CSS properties.
Note: We can also create another file for styling or we can add them in the same HTML file under the style tag.
Now, to display only one review at a time we will create some functions in JavaScript.
Carousel function: This function gets all the elements using the specified class name as an object with the help of the getElementsByClassName() method. These objects can be accessed by the index of the elements. This function receives a parameter, which is the index of the element it will display.
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<div class="review">
<div class="review__items">
<img src=
"https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" />
<h1>GeeksforGeeks</h1>
<p>
Let's learn to create a review
carousel using JavaScript.
</p>
</div>
<div class="review__items">
<img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210101144014/gfglogo.png" />
<h1>Geek Here</h1>
<p>
Very useful site to learn cool
stuff. Improve your skills
</p>
</div>
<div class="review__items">
<img src=
"https://media.geeksforgeeks.org/img-practice/courses/GfG_Logo.png" />
<h1>Hello there!</h1>
<p>
Have a nice day, Please visit
us again. Nice to meet you.
</p>
</div>
<div class="review__button">
<button id="prev" onclick="previousReview()">
PREV
</button>
<button id="next" onclick="nextReview()">
NEXT
</button>
</div>
</div>
</html>
CSS
/* filename: style.css */
.review {
background: rgb(145, 226, 188);
height: auto;
width: 270px;
border-radius: 15px;
margin: auto;
padding: 10px;
margin-top: 30px;
box-shadow: 5px 5px 5px #32917d;
align-items: center;
}
.review__items {
align-items: center;
justify-content: space-evenly;
width: 250px;
padding: 10px;
align-items: center;
}
.review__items img {
height: 250px;
width: 250px;
border-radius: 5px;
box-shadow: rgba(0, 0, 0, 0.35) 0px 4px 15px;
}
.review__items h1 {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 20px;
}
.review__items p {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 14px;
}
.review__button {
text-align: center;
padding: 10px;
}
.review__button button {
color: rgb(192, 229, 192);
background: green;
font-weight: bold;
}
.review__items {
display: none;
}
JavaScript
//Filename: script.js
let rev = 0;
carousel(rev);
function previousReview() {
rev = rev - 1;
carousel(rev);
}
function nextReview() {
rev = rev + 1;
carousel(rev);
}
function carousel(review) {
let reviews = document.getElementsByClassName("review__items");
if (review >= reviews.length) {
review = 0;
rev = 0;
}
if (review < 0) {
review = reviews.length - 1;
rev = reviews.length - 1;
}
for (let i = 0; i < reviews.length; i++) {
reviews[i].style.display = "none";
}
reviews[review].style.display = "block";
}
Output
Similar Reads
How to create a Responsive Carousel in React JS ? A carousel, often seen in web and mobile applications, is a user interface component used to display a set of content items such as images or text in a rotating manner. It allows users to view multiple items within a limited space by scrolling horizontally or vertically through the content. In this
2 min read
Create an Autoplay Carousel using HTML CSS and JavaScript An Autoplay Carousel is a dynamic UI element that automatically cycles through a series of images or content slides, providing a visually engaging way to display information. It enhances user experience by showcasing multiple items in a limited space without manual navigation. This can be implemente
2 min read
How to create image slider using HTML CSS and JavaScript ? An image slide, or slideshow, is a dynamic display of images that automatically transitions from one to the next, often with animations. To create an image slide, use HTML to structure the images, CSS for styling and animations, and JavaScript to control the timing and transitions between images.App
3 min read
How to Create a Quotes Slideshow with CSS and JavaScript? A quotes slideshow is a web component that displays a series of quotes in a sequential manner. Users can either view these quotes automatically or interactively through navigation controls. we'll explore how to create a quotes slideshow using HTML, CSS, and JavaScript.Approach Create a basic HTML do
4 min read
How to Create a Portfolio Website using HTML CSS and JavaScript ? A portfolio website is a website that represents you online on the web pages. It contains different sections like Introduction to Yourself, About You, Your Services, Your Contact Details, and Your Portfolio. We are going to build all of the sections with an interactive and attractive web design that
15+ min read
How to Create Image Gallery using JavaScript? An Image Gallery is a collection of images displayed in a grid or slideshow format on a webpage. To create an Image Gallery using JavaScript, you can dynamically load images, create HTML elements, and use CSS for styling. JavaScript can add interactivity, like transitions and navigation controls.Pro
3 min read