A star rating system is a simple and easy way for users to rate content such as products, articles, or services. It enhances the user experience by providing a visual indicator of quality or satisfaction. In this guide, we’ll show you how to create a rating system. Dynamically score wires using HTML, CSS, and JavaScript.
Prerequisites
Before you start Make sure you have a basic understanding of:
- HTML: To create the structure of a web page.
- CSS: for styling elements.
- JavaScript: To add interactivity to the star rating system.
Approach
Follow these steps to create a star rating system.
- HTML: Create layouts using div and span elements.
- CSS: Style stars and output text for a visually appealing interface.
- JavaScript: Added functionality to dynamically record and display user ratings.
Step-by-Step Implementation
HTML Code
The HTML structure consists of a string represented by the span element and a section to display the selected score.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Star Rating</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="rating-container">
<span class="star" onclick="gfg(1)">★</span>
<span class="star" onclick="gfg(2)">★</span>
<span class="star" onclick="gfg(3)">★</span>
<span class="star" onclick="gfg(4)">★</span>
<span class="star" onclick="gfg(5)">★</span>
</div>
<div id="output">Rating is: 0/5</div>
<script src="script.js"></script>
</body>
</html>
CSS Code
Format stars and output text to make it look beautiful.
/* style.css */
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.rating-container {
display: flex;
justify-content: center;
gap: 10px;
}
.star {
font-size: 2rem;
cursor: pointer;
color: lightgray;
transition: color 0.2s;
}
.star.one, .star.two, .star.three, .star.four, .star.five {
color: gold;
}
#output {
margin-top: 20px;
font-size: 1.2rem;
color: #333;
}
JavaScript Code
Use JavaScript to handle click events and dynamically update star characters and rating output.
// script.js
// Access the star elements and output container
let stars = document.getElementsByClassName("star");
let output = document.getElementById("output");
// Function to update the rating
function gfg(n) {
remove();
for (let i = 0; i < n; i++) {
stars[i].className = `star ${['one', 'two', 'three', 'four', 'five'][n - 1]}`;
}
output.innerText = `Rating is: ${n}/5`;
}
// Function to remove existing styles
function remove() {
for (let i = 0; i < stars.length; i++) {
stars[i].className = "star";
}
}
How It Works
- HTML Structure:
- Five
span
elements represent the stars. - The
onclick
attribute calls thegfg()
function to handle the rating.
2. CSS Styling:
- Dynamic classes (
one
,two
, etc.) are added to change the color of the stars when clicked. - The
.star
class styles the default stars.
3. JavaScript Functionality:
- The
gfg()
function updates the class of each star up to the selected rating. - The
remove()
function resets the star styles before applying the new rating.
Conclusion
Creating a star rating system with HTML, CSS, and JavaScript is a great way to enhance your web development skills. By following this guide, you’ll be able to implement a dynamic, user-friendly feature on your website. Start coding and make your projects shine!