JSFiddle - React, Tailwind, and code Playground

by Prathameshsb

HTML

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <div class="rating">
    <input type="radio" id="star5" name="rating" value="5">
    <label class="rating-star-label" for="star5">5</label>
    <input type="radio" id="star4" name="rating" value="4">
    <label class="rating-star-label" for="star4">4</label>
    <input type="radio" id="star3" name="rating" value="3">
    <label class="rating-star-label" for="star3">3</label>
    <input type="radio" id="star2" name="rating" value="2">
    <label class="rating-star-label" for="star2">2</label>
    <input type="radio" id="star1" name="rating" value="1">
    <label class="rating-star-label" for="star1">1</label>
    <div class="rating-stars">
      <label class="rating-star" for="star5"></label>
      <label class="rating-star" for="star4"></label>
      <label class="rating-star" for="star3"></label>
      <label class="rating-star" for="star2"></label>
      <label class="rating-star" for="star1"></label>
    </div>
  </div>
</body>
</html>

CSS

.rating {
  display: inline-block;
  unicode-bidi: bidi-override;
  color: #777777;
  font-size: 25px;
  height: 25px;
  width: 125px;
  margin: 0 auto;
  position: relative;
  cursor: pointer;
}

.rating-star {
  display: inline-block;
  position: relative;
  width: 25px;
  height: 25px;
  margin-right: 2px;
  transition: color 0.3s ease;
}

.rating-star:before {
  content: '\2605';
  position: absolute;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.rating:hover .rating-star:before,
.rating:hover .rating-star-label {
  opacity: 1;
}

.rating:hover .rating-star-label {
  color: #FFD700;
}

.rating input[type="radio"] {
  display: none;
}

.rating input[type="radio"]:checked ~ .rating-star:before {
  opacity: 1;
}

.rating .rating-star-label {
  color: #ccc;
  font-size: 25px;
  position: absolute;
  top: 0;
  left: 0;
  line-height: 25px;
}

JavaScript

var ratingStars = document.querySelectorAll('.rating-star');

ratingStars.forEach(function(star) {
  star.addEventListener('click', function() {
    var rating = this.getAttribute('for');
    alert('You rated ' + rating + ' stars!');
  });
});