Star Rating Interaction Using HTML CSS and JS With Free Source Code
Introduction
Hello coders, welcome to another new project. I hope you all are doing well. In our today’s project we’ve developed a star rating interaction with the help of frontend technologies html ,css and javascript. We’ve developed the structure of our project using html and then applied CSS to give some styling. We used JS in our HTML file for functionality.
This project is about how we rate some apps or website’s on the basis of their service. You’ll learn to build this type of tiny functions in this project that you can use later in your website and you can also customize it.
For every organization or brand feedbacks and ratings helps them to improve their services. This function they use in their website so that they can get user’s response on their services.
let’s understand our code.
index.html
This code creates a star rating system and makes an HTML structure with interactive functionality from JavaScript. The HTML part defines the layout, and in JavaScript, there is handling of user interactions— users are allowed to select a rating by clicking the stars.
HTML Section:
The HTML starts with the definition <!DOCTYPE html>, which declares that this is a document following the HTML5 specifications. Inside the <head> are the meta tags, defining what should occur. This sets the character encoding as UTF-8 and sets up the viewport for responsive design. The <title> is for the page title, and the <link> is to link to an external CSS file, style.css, to style this category page.
The star rating system is enclosed within a <div> having the class rating: contained in the <body>. It includes five <button> with class rating-button: that is part of a star rating system, it is a stand-in for one star. Nestled within each button is an SVG element and that it elements that represent as a star. The viewBox attributes enclosed in SVG to set a coordinate system and the path element is what describes the shape of the star. The fill=”currentColor” attribute allows CSS to fill the star with a color, which makes it possible that the color can change through the course.
JavaScript Section:
The JavaScript makes this static HTML interactive. It creates a Rating class that enables managing the behavior of star buttons. The constructor inside the class attaches a click event listener to ratingEl, the entire rating component. On clicking any star, the onClick method identifies the clicked star and updates the appearance of all stars. It appends the rating-button–active class to stars up through the clicked star and leaves others unactivated.
This script makes multiple rating systems on the same page work independently of one another, thus forming modular and reusable code in various Web projects. This pairing of HTML and JavaScript provides a powerful, user-friendly star rating system that is dynamic.
Star Rating Interaction
style.css
This CSS code styles a star rating system with a focus on interactive and visual effects.
The * selector makes sure padding and borders are included in the total width and height of the element by applying the box-sizing: border-box property globally. This greatly simplifies the math used to calculate layouts. The :root selector sets CSS variables for standard theming throughout the document. The –background-color makes the background of the page a dark color, #151515. The variables –on-color and –off-color are used for active and non-active star controls.
The body selector removes all the default margins and centers the content vertically and horizontally with Flexbox. It also ensures a minimum page width of 320px and fills the viewport height, ensuring responsiveness in the rating system and that it’s centered. The .rating class styles the container for the star buttons. It removes default list styles and aligns stars horizontally, with a slight gap.
The .rating-button class styles each star button by removing the default button appearance and outline. It sets the cursor to a pointer to indicate clickability and removes the default padding and borders. Lastly, –transition-delay is used for animation timing. Finally, it uses the –transition-delay variable for animation timing. The .rating–animatable .rating-button selector applies a smooth transform transition effect to the buttons, which enhances the interactive experience with buttons.
The .rating-button:active and the .rating-button–active:active rules change the scale of the button when pressed to provide visual feedback. The class .star styles the SVG stars with their initial scale and color. Active stars, indicated by .rating-button–active .star, have a bigger scale and rotate while changing colors due to the transitions defined for a dynamic user experience.
* {
box-sizing: border-box;
}
:root {
--background-color: #151515;
--on-color: #ffc107;
--off-color: #666;
background-color: var(--background-color);
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.rating {
list-style: none;
margin: 0;
padding: 0;
display: flex;
align-items: center;
gap: 0 4px;
height: 40px;
}
.rating-button {
appearance: none;
background: none;
outline: none;
border: none;
padding: 0;
height: 100%;
cursor: pointer;
--transition-delay: 0s;
}
.rating--animatable .rating-button {
transition: 0.4s cubic-bezier(0.23, 1, 0.32, 1) transform;
}
.rating-button:active {
transform: scale(0.8);
}
.rating-button--active:active {
transform: scale(1.1);
}
.star {
transform: scale(0.5) rotate(0deg);
height: 100%;
color: var(--off-color);
}
.rating--animatable .star {
transition: 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) all;
transition-delay: var(--transition-delay);
}
.rating-button--active .star {
transform: scale(0.9) rotate(360deg);
color: var(--on-color);
}