This code provides a fully interactive, component-based Star Rating Display widget built using HTML, CSS, and JavaScript. It is designed to let users visually select a rating from one to five stars, providing real-time feedback through dynamic color changes, scaling animations, and numerical score updates.
The HTML structure acts as the foundation, setting up a container card that holds five individual star symbols (each with a data-value attribute from 1 to 5), a numeric display for the score, and an instructional text label. The CSS styles this structure using modern layout techniques like Flexbox, CSS variables for customizable theming, and smooth transitions that make the stars enlarge and change color when hovered over or activated.
The JavaScript brings the widget to life by adding event listeners to each star. When a user moves their mouse over a star (mouseenter), the script dynamically highlights that star and all preceding stars, simultaneously updating the numerical text display (e.g., "4.0"). If the user moves the cursor away (mouseleave), the display reverts to the last permanently saved rating. Clicking a star (click) locks in that specific value as the selectedRating, updating both the visual active states and the numerical output so it persists until a new rating is chosen.
To use this code in your project, create three separate files in the same directory: save the HTML code as index.html, the CSS code as styles.css, and the JavaScript code as scripts.js. Ensure you define the necessary CSS variables (such as --card-bg, --star-active, --text-primary, etc.) in a parent stylesheet or your styles.css file so the styles render correctly. Finally, open the index.html file in any web browser, and you will have a functional, responsive star-rating widget ready for user input.