Pure CSS Star Rating

In this example below you will see how to do a Pure CSS Star Rating with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by İlkem Erol, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright İlkem Erol ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Pure CSS Star Rating</title>
  
  
  <link rel='stylesheet prefetch' href='https://www.w3schools.com/w3css/4/w3.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div class="w3-container w3-center">
  <p style="background-color: black; color: white;"><strong>When you click the star, page do not go to top.</strong></p>
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis   egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
  <section>
    <div class="stars">
      <input class="star star-5" id="star-5" value="5" type="radio" name="star"/>
      <label class="star star-5" for="star-5"></label>
      <input class="star star-4" id="star-4" value="4" type="radio" name="star"/>
      <label class="star star-4" for="star-4"></label>
      <input class="star star-3" id="star-3" value="3" type="radio" name="star"/>
      <label class="star star-3" for="star-3"></label>
      <input class="star star-2" id="star-2" value="2" type="radio" name="star"/>
      <label class="star star-2" for="star-2"></label>
      <input class="star star-1" id="star-1" value="1" type="radio" name="star"/>
      <label class="star star-1" for="star-1"></label>

      <input id="sendForm" class="w3-button w3-black w3-round" type="submit" value="Submit"/>
      <p id="select-star">NoN-Selected!</p>
    </div>
  </section>
</div>
  <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/mychoo/pure-css-star-rating-xjZppe */
h3 {
    color:darkgrey;
    margin-left:20px;
    margin-top:20px;
}

div.stars {
  width: 270px;
  display: inline-block;
}

input.star { display: none; }

label.star {
  float: right;
  padding: 10px;
  font-size: 36px;
  color: #444;
  transition: all .2s;
}

input.star:checked ~ label.star:before {
  content: '\2605';
  color: #FD4;
  transition: all .25s;
}

input.star-5:checked ~ label.star:before {
  color: #FE7;
  text-shadow: 0 0 20px #952;
}

input.star-1:checked ~ label.star:before { color: #F62; }

label.star:hover { transform: rotate(215deg) scale(1.3); }

label.star:before {
  content: '\2605';
}

#select-star {
  font-size: 30px;
}


/*Downloaded from https://www.codeseek.co/mychoo/pure-css-star-rating-xjZppe */
$('#sendForm').click(function(){
  $('#select-star').text($("input[type=radio]:checked").val() + ' star selected!');
});

Comments