If you want to create a custom star rating system for your website or application, you can easily achieve it using HTML, CSS, Font Awesome, and JavaScript. In this tutorial, we will show you how to create a star rating system using Font Awesome and JavaScript.
Font Awesome is a popular icon library that provides a vast collection of scalable vector icons that can be customized easily using CSS. In this tutorial, we will use Font Awesome to create the stars for our rating system.
Step 1: Set up the HTML
The first step is to set up the HTML markup. In this example, we will use a container div with a heading and a star-rating div that contains five Font Awesome star icons.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Custom Star Rating System</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h2>Give Me Rating.</h2>
<div class="star-rating">
<span class="fa fa-star star"></span>
<span class="fa fa-star star"></span>
<span class="fa fa-star star"></span>
<span class="fa fa-star star"></span>
<span class="fa fa-star star"></span>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Step 2: Style the Rating System with CSS
After setting up the HTML markup, the next step is to style the rating system using CSS. In this example, we will use a custom CSS file to style the star-rating div and the star icons. The CSS file will also define the styles for the active and inactive states of the stars.
.star-rating {
font-size: 30px;
}
.star {
color: grey;
cursor: pointer;
}
.star.active {
color: #ff9900;
}
Step 3: Create the JavaScript Functionality
The final step is to add the JavaScript functionality to the star-rating system. In this example, we will use JavaScript to add an event listener to each star icon that listens for a click event. When a user clicks on a star icon, the script will add the "active" class to that star and all the stars before it.
// Get all the star elements
const stars = document.querySelectorAll(".star");
// Loop through each star and add an event listener
stars.forEach((star, index) => {
star.addEventListener("click", () => {
// Remove the active class from all stars
stars.forEach((s, i) => {
if (i <= index) {
s.classList.add("active");
} else {
s.classList.remove("active");
}
});
});
To conclude, using Font Awesome and JavaScript can make it easy to create a dynamic and interactive star rating system for a website. By adding event listeners to the star elements and manipulating their classes with JavaScript, we can create a system that allows users to easily rate and provide feedback. Additionally, by customizing the styles of the stars with CSS, we can make the rating system fit seamlessly with the overall design of the website. Overall, the combination of Font Awesome, CSS, and JavaScript provides a powerful toolset for creating engaging and interactive user experiences.