How To Create a Simple Star Rating – No Javascipt

Hello guys in this tutorial we will create a Simple Star Rating system using HTML & CSS

First we need to create two files index.html and style.css then we need to do code for it.

Step:1

Add below code inside index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Star Rating</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta https-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="row-outer">
      <div class="container">
        <div class="star-rating">   
          <input type="radio" name="rating" id="rating-5">
          <label for="rating-5"></label>
          <input type="radio" name="rating" id="rating-4">
          <label for="rating-4"></label>
          <input type="radio" name="rating" id="rating-3">
          <label for="rating-3"></label>
          <input type="radio" name="rating" id="rating-2">
          <label for="rating-2"></label>
          <input type="radio" name="rating" id="rating-1">
          <label for="rating-1"></label>
        </div>
      </div>
    </div>
  </body>
</html>

Step:2

Then we need to add code for style.css which code i provide in below screen.

* {
  padding: 0;
  margin: 0;
  font-family: 'IBM Plex Sans', sans-serif;
}
body {
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f1f2f3;
    height: 100vh;
}
.container {
    width: 95%;
    max-width: 1160px;
    margin: auto;
}
.star-rating {
    display: flex;
    justify-content: center;
    width: 100%;
    overflow: hidden;
    flex-direction: row-reverse;
    position: relative;
}
.star-rating > input {
    display: none;
}
.star-rating > label {
    margin-top: auto;
    cursor: pointer;
    width: 40px;
    height: 40px;
    background: url(star-disable.png)no-repeat center / 75%;
    transition: 0.5s;
}
.star-rating > input:not(:checked) ~ label:hover,
.star-rating > input:not(:checked) ~ label:hover ~ label {
    background: url(star-active.png)no-repeat center / 75%;
}

.star-rating > input:checked ~ label,
.star-rating > input:checked ~ label ~ label {
    background: url(star-active.png)no-repeat center / 75%;
}

Output:

Leave a Comment