DEV Community

Cover image for How To Create a Simple Star Rating – No JavaScript
Stackfindover
Stackfindover

Posted on • Updated on

How To Create a Simple Star Rating – No JavaScript

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

Animated Stars With Realistic Moon
Advanced Animated Submenu

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

Star Rating 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 http-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>
Enter fullscreen mode Exit fullscreen mode

Disable Right Click Using JavaScript

Star Rating Step:2

Then we need to add code for style.css which code I provide in the 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%;
}
Enter fullscreen mode Exit fullscreen mode

Star Rating Video Output:

Star Rating CodePen Output:

Top comments (0)