This code creates a star rating system using HTML radio buttons and CSS. It allows users to rate items by clicking on radio buttons represented as stars. The CSS code manipulates the appearance of the stars based on user interactions, providing visual feedback. It’s helpful for implementing user-friendly rating systems on websites.
You can use this code on your website’s product or content rating sections. It offers a visually appealing star-based rating system that enhances user engagement and provides clear feedback. This code’s simplicity makes it easy to implement and customize, improving user interaction and feedback collection.
How to Create Star Rating Using Radio Buttons in HTML & CSS
1. Begin by creating the HTML structure for the star rating system in your feedback form. Inside a div with a class of “rate,” you’ll place a set of radio input elements and their corresponding labels. Each input represents a star with a unique ID, and the labels provide a visual representation of the rating value.
<div class="rate"> <input type="radio" id="star5" name="rate" value="5" /> <label for="star5" title="text">5 stars</label> <input type="radio" id="star4" name="rate" value="4" /> <label for="star4" title="text">4 stars</label> <input type="radio" id="star3" name="rate" value="3" /> <label for="star3" title="text">3 stars</label> <input type="radio" id="star2" name="rate" value="2" /> <label for="star2" title="text">2 stars</label> <input type="radio" id="star1" name="rate" value="1" /> <label for="star1" title="text">1 star</label> </div>
2. Now, let’s style the rating system using CSS. The provided CSS code manipulates the appearance of the stars based on user interactions. It hides the radio buttons and uses labels to display star icons. The color of the stars changes when the user hovers over.
*{ margin: 0; padding: 0; } .rate { float: left; height: 46px; padding: 0 10px; } .rate:not(:checked) > input { position:absolute; top:-9999px; } .rate:not(:checked) > label { float:right; width:1em; overflow:hidden; white-space:nowrap; cursor:pointer; font-size:30px; color:#ccc; } .rate:not(:checked) > label:before { content: '★ '; } .rate > input:checked ~ label { color: #ffc700; } .rate:not(:checked) > label:hover, .rate:not(:checked) > label:hover ~ label { color: #deb217; } .rate > input:checked + label:hover, .rate > input:checked + label:hover ~ label, .rate > input:checked ~ label:hover, .rate > input:checked ~ label:hover ~ label, .rate > label:hover ~ input:checked ~ label { color: #c59b08; } /* Modified from: https://github.com/mukulkant/Star-rating-using-pure-css */
That’s all! hopefully, you have successfully created a Star Rating with Radio Buttons. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.