CSS Star Rating Radio Buttons

CSS Star Rating Radio Buttons
Code Snippet:5 star rating with css and html
Author: allenhe
Published: January 21, 2024
Last Updated: January 22, 2024
Downloads: 1,574
License: MIT
Edit Code online: View on CodePen
Read More

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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X