Admin Web Developer

Pure CSS Star Rating System With Color Change

6 min read

Pure CSS Star Rating System With Color Change

“Pure CSS Star Rating System With Color Change”.

Recently I have to work with a rating system in one of my projects and I need to create a design for it. So, I have spent a lot of time on google for designs but personally, I did not like it because I need a design which uses a pure CSS and HTML.

After so much time spending on Google, I come up with a solution. So,

Here I have created a pure CSS five-star rating System with different colors. In this example, I have used HTML radio inputs and SCSS.

Step 1: Create a star shape

HTML:

<span class="star"></span>
<span class="star"></span>

SCSS:

CSS

//color
$lightgray: #D3D3D3;

//mixin 
@mixin transition($transition-property, $transition-time, $method) {
  -webkit-transition: $transition-property $transition-time $method;
  -moz-transition: $transition-property $transition-time $method;
  -ms-transition: $transition-property $transition-time $method;
  -o-transition: $transition-property $transition-time $method;
  transition: $transition-property $transition-time $method;
}
@mixin transform($transforms) {
  transform: $transforms;
  -moz-transform: $transforms;
  -ms-transform: $transforms;
  -webkit-transform: $transforms;
}

//mixin star shape
@mixin star-shape($width: $width, $background-color: transparent, $z-index: 0) {
  @include transform(rotate(35deg));
  @include transition(all, .3s, linear);
  border-bottom: $width * .7 solid $background-color;
  border-left: $width solid transparent;
  border-right: $width solid transparent;
  display: inline-block;
  height: 0;
  margin-bottom: $width / 2;
  margin-top: $width / 2;
  position: relative;
  width: 0;
  margin-right: .4em;
  &amp;:before,
  &amp;:after {
    @include transition(all, .3s, linear);
    content: '';
    display: block;
    height: 0;
    position: absolute;
    width: 0;
    z-index: $z-index - 1;
  }
  &amp;:before {
    @include transform(rotate(-35deg));
    border-bottom: $width * .8 solid $background-color;
    border-left: $width * .3 solid transparent;
    border-right: $width * .3 solid transparent;
    left: -$width * .65;
    top: -$width * .45;
  }
  &amp;:after {
    @include transform(rotate(-70deg));
    border-bottom: $width * .7 solid $background-color;
    border-left: $width solid transparent;
    border-right: $width solid transparent;
    left: -$width * 1.05;
    top: $width * .03;
  }
}
.star-rating {
  text-align: center;
 }
.star {
  @include star-shape(1.8em, $lightgray);
}
//color
$lightgray: #D3D3D3;

//mixin 
@mixin transition($transition-property, $transition-time, $method) {
  -webkit-transition: $transition-property $transition-time $method;
  -moz-transition: $transition-property $transition-time $method;
  -ms-transition: $transition-property $transition-time $method;
  -o-transition: $transition-property $transition-time $method;
  transition: $transition-property $transition-time $method;
}
@mixin transform($transforms) {
  transform: $transforms;
  -moz-transform: $transforms;
  -ms-transform: $transforms;
  -webkit-transform: $transforms;
}

//mixin star shape
@mixin star-shape($width: $width, $background-color: transparent, $z-index: 0) {
  @include transform(rotate(35deg));
  @include transition(all, .3s, linear);
  border-bottom: $width * .7 solid $background-color;
  border-left: $width solid transparent;
  border-right: $width solid transparent;
  display: inline-block;
  height: 0;
  margin-bottom: $width / 2;
  margin-top: $width / 2;
  position: relative;
  width: 0;
  margin-right: .4em;
  &amp;:before,
  &amp;:after {
    @include transition(all, .3s, linear);
    content: '';
    display: block;
    height: 0;
    position: absolute;
    width: 0;
    z-index: $z-index - 1;
  }
  &amp;:before {
    @include transform(rotate(-35deg));
    border-bottom: $width * .8 solid $background-color;
    border-left: $width * .3 solid transparent;
    border-right: $width * .3 solid transparent;
    left: -$width * .65;
    top: -$width * .45;
  }
  &amp;:after {
    @include transform(rotate(-70deg));
    border-bottom: $width * .7 solid $background-color;
    border-left: $width solid transparent;
    border-right: $width solid transparent;
    left: -$width * 1.05;
    top: $width * .03;
  }
}
.star-rating {
  text-align: center;
 }
.star {
  @include star-shape(1.8em, $lightgray);
}

Codepen

Step 2 : Create star rating using radio buttons

HTML:

<div class="star-rating"> 
  <h3>Star rating</h3>
  <div class="rating">
    <input type="radio" id="star5" name="rating" value="5" class="radio-1" />
    <label for="star5" class="star star-1" title="Excellent"></label>
    <input type="radio" id="star4" name="rating" value="4" class="radio-2" />
    <label for="star4" class="star star-2" title="Great"></label>
    <input type="radio" id="star3" name="rating" value="3" class="radio-3" />
    <label for="star3" class="star star-3" title="Average"></label>
    <input type="radio" id="star2" name="rating" value="2" class="radio-4" />
    <label for="star2" class="star star-4" title="Poor"></label>
    <input type="radio" id="star1" name="rating" value="1" class="radio-5" />
    <label for="star1" class="star star-5" title="Bad"></label>
  </div>
</div>
<div class="star-rating"> 
  <h3>Star rating</h3>
  <div class="rating">
    <input type="radio" id="star5" name="rating" value="5" class="radio-1" />
    <label for="star5" class="star star-1" title="Excellent"></label>
    <input type="radio" id="star4" name="rating" value="4" class="radio-2" />
    <label for="star4" class="star star-2" title="Great"></label>
    <input type="radio" id="star3" name="rating" value="3" class="radio-3" />
    <label for="star3" class="star star-3" title="Average"></label>
    <input type="radio" id="star2" name="rating" value="2" class="radio-4" />
    <label for="star2" class="star star-4" title="Poor"></label>
    <input type="radio" id="star1" name="rating" value="1" class="radio-5" />
    <label for="star1" class="star star-5" title="Bad"></label>
  </div>
</div>

SCSS:

CSS

//color
$lightgray: #D3D3D3;

//mixin 
@mixin transition($transition-property, $transition-time, $method) {
  -webkit-transition: $transition-property $transition-time $method;
  -moz-transition: $transition-property $transition-time $method;
  -ms-transition: $transition-property $transition-time $method;
  -o-transition: $transition-property $transition-time $method;
  transition: $transition-property $transition-time $method;
}
@mixin transform($transforms) {
  transform: $transforms;
  -moz-transform: $transforms;
  -ms-transform: $transforms;
  -webkit-transform: $transforms;
}

//mixin star shape
@mixin star-shape($width: $width, $background-color: transparent, $z-index: 0) {
  @include transform(rotate(35deg));
  @include transition(all, .3s, linear);
  border-bottom: $width * .7 solid $background-color;
  border-left: $width solid transparent;
  border-right: $width solid transparent;
  display: inline-block;
  height: 0;
  margin-bottom: $width / 2;
  margin-top: $width / 2;
  position: relative;
  width: 0;
  margin-right: .4em;
  &amp;:before,
  &amp;:after {
    @include transition(all, .3s, linear);
    content: '';
    display: block;
    height: 0;
    position: absolute;
    width: 0;
    z-index: $z-index - 1;
  }
  &amp;:before {
    @include transform(rotate(-35deg));
    border-bottom: $width * .8 solid $background-color;
    border-left: $width * .3 solid transparent;
    border-right: $width * .3 solid transparent;
    left: -$width * .65;
    top: -$width * .45;
  }
  &amp;:after {
    @include transform(rotate(-70deg));
    border-bottom: $width * .7 solid $background-color;
    border-left: $width solid transparent;
    border-right: $width solid transparent;
    left: -$width * 1.05;
    top: $width * .03;
  }
}
.star-rating {
  text-align: center;
 }
.star {
  @include star-shape(1.8em, $lightgray);
}
.rating {
  display: inline-block;
  position: relative;
  input[type="radio"] {
    position: absolute;
    opacity: 0;
    height: 1px;
    width: 1px;
    bottom: 0;
  }
}
//color
$lightgray: #D3D3D3;

//mixin 
@mixin transition($transition-property, $transition-time, $method) {
  -webkit-transition: $transition-property $transition-time $method;
  -moz-transition: $transition-property $transition-time $method;
  -ms-transition: $transition-property $transition-time $method;
  -o-transition: $transition-property $transition-time $method;
  transition: $transition-property $transition-time $method;
}
@mixin transform($transforms) {
  transform: $transforms;
  -moz-transform: $transforms;
  -ms-transform: $transforms;
  -webkit-transform: $transforms;
}

//mixin star shape
@mixin star-shape($width: $width, $background-color: transparent, $z-index: 0) {
  @include transform(rotate(35deg));
  @include transition(all, .3s, linear);
  border-bottom: $width * .7 solid $background-color;
  border-left: $width solid transparent;
  border-right: $width solid transparent;
  display: inline-block;
  height: 0;
  margin-bottom: $width / 2;
  margin-top: $width / 2;
  position: relative;
  width: 0;
  margin-right: .4em;
  &amp;:before,
  &amp;:after {
    @include transition(all, .3s, linear);
    content: '';
    display: block;
    height: 0;
    position: absolute;
    width: 0;
    z-index: $z-index - 1;
  }
  &amp;:before {
    @include transform(rotate(-35deg));
    border-bottom: $width * .8 solid $background-color;
    border-left: $width * .3 solid transparent;
    border-right: $width * .3 solid transparent;
    left: -$width * .65;
    top: -$width * .45;
  }
  &amp;:after {
    @include transform(rotate(-70deg));
    border-bottom: $width * .7 solid $background-color;
    border-left: $width solid transparent;
    border-right: $width solid transparent;
    left: -$width * 1.05;
    top: $width * .03;
  }
}
.star-rating {
  text-align: center;
 }
.star {
  @include star-shape(1.8em, $lightgray);
}
.rating {
  display: inline-block;
  position: relative;
  input[type="radio"] {
    position: absolute;
    opacity: 0;
    height: 1px;
    width: 1px;
    bottom: 0;
  }
}

Codepen

Step 3 : Create different colors stars (on hover and checked radio stars)

HTML:

<div class="star-rating"> 
  <h3>Star rating</h3>
  <div class="rating">
    <input type="radio" id="star5" name="rating" value="5" class="radio-1" />
    <label for="star5" class="star star-1" title="Excellent"></label>
    <input type="radio" id="star4" name="rating" value="4" class="radio-2" />
    <label for="star4" class="star star-2" title="Great"></label>
    <input type="radio" id="star3" name="rating" value="3" class="radio-3" />
    <label for="star3" class="star star-3" title="Average"></label>
    <input type="radio" id="star2" name="rating" value="2" class="radio-4" />
    <label for="star2" class="star star-4" title="Poor"></label>
    <input type="radio" id="star1" name="rating" value="1" class="radio-5" />
    <label for="star1" class="star star-5" title="Bad"></label>
  </div>
</div>
<div class="star-rating"> 
  <h3>Star rating</h3>
  <div class="rating">
    <input type="radio" id="star5" name="rating" value="5" class="radio-1" />
    <label for="star5" class="star star-1" title="Excellent"></label>
    <input type="radio" id="star4" name="rating" value="4" class="radio-2" />
    <label for="star4" class="star star-2" title="Great"></label>
    <input type="radio" id="star3" name="rating" value="3" class="radio-3" />
    <label for="star3" class="star star-3" title="Average"></label>
    <input type="radio" id="star2" name="rating" value="2" class="radio-4" />
    <label for="star2" class="star star-4" title="Poor"></label>
    <input type="radio" id="star1" name="rating" value="1" class="radio-5" />
    <label for="star1" class="star star-5" title="Bad"></label>
  </div>
</div>

SCSS:

//color
$black: #000000;
$white: #ffffff;
$lightgray: #D3D3D3;
$red: #FF5254;
$orange: #FF6C35;
$yellow: #FF9900;
$green: #3F9E37;
$dark-green:#14892c;

//mixin 
@mixin transition($transition-property, $transition-time, $method) {
  -webkit-transition: $transition-property $transition-time $method;
  -moz-transition: $transition-property $transition-time $method;
  -ms-transition: $transition-property $transition-time $method;
  -o-transition: $transition-property $transition-time $method;
  transition: $transition-property $transition-time $method;
}

@mixin transform($transforms) {
  transform: $transforms;
  -moz-transform: $transforms;
  -ms-transform: $transforms;
  -webkit-transform: $transforms;
}

//mixin star shape
@mixin star-shape($width: $width, $background-color: transparent, $z-index: 0) {
  @include transform(rotate(35deg));
  @include transition(all, .3s, linear);
  border-bottom: $width * .7 solid $background-color;
  border-left: $width solid transparent;
  border-right: $width solid transparent;
  display: inline-block;
  height: 0;
  margin-bottom: $width / 2;
  margin-top: $width / 2;
  position: relative;
  width: 0;
  margin-right: .4em;
  &amp;:before,
  &amp;:after {
    @include transition(all, .3s, linear);
    content: '';
    display: block;
    height: 0;
    position: absolute;
    width: 0;
    z-index: $z-index - 1;
  }
  &amp;:before {
    @include transform(rotate(-35deg));
    border-bottom: $width * .8 solid $background-color;
    border-left: $width * .3 solid transparent;
    border-right: $width * .3 solid transparent;
    left: -$width * .65;
    top: -$width * .45;
  }
  &amp;:after {
    @include transform(rotate(-70deg));
    border-bottom: $width * .7 solid $background-color;
    border-left: $width solid transparent;
    border-right: $width solid transparent;
    left: -$width * 1.05;
    top: $width * .03;
  }
}
.star-rating {
  text-align: center;
}
.rating {
  display: inline-block;
  position: relative;
  input[type="radio"] {
    position: absolute;
    opacity: 0;
    height: 1px;
    width: 1px;
    bottom: 0;
  }
}
// star shape
.star {
  @include star-shape(1.8em, $lightgray);
  float: right;
}

//with different color
$colors: $dark-green,
$green,
$yellow,
$orange,
$red;
@for $i from length($colors) through 1 {
  .star-#{$i} {
    &amp;.active,
    &amp;.active ~ .star {
      border-bottom-color: nth($colors, $i);
      &amp;:before,
      &amp;:after {
        border-bottom-color: nth($colors, $i);
      }
    }
  }
  input[type="radio"] + .star-#{$i} {
    cursor: pointer;
    &amp;:hover,
    &amp;:hover ~ .star {
      border-bottom-color: nth($colors, $i);
      &amp;:before,
      &amp;:after {
        border-bottom-color: nth($colors, $i);
      }
    }
  }
  .rating:not(:hover) {
    .radio-#{$i}:checked ~ .star {
      border-bottom-color: nth($colors, $i);
      &amp;:before,
      &amp;:after {
        border-bottom-color: nth($colors, $i);
      }
    }
  }
}
//color
$black: #000000;
$white: #ffffff;
$lightgray: #D3D3D3;
$red: #FF5254;
$orange: #FF6C35;
$yellow: #FF9900;
$green: #3F9E37;
$dark-green:#14892c;

//mixin 
@mixin transition($transition-property, $transition-time, $method) {
  -webkit-transition: $transition-property $transition-time $method;
  -moz-transition: $transition-property $transition-time $method;
  -ms-transition: $transition-property $transition-time $method;
  -o-transition: $transition-property $transition-time $method;
  transition: $transition-property $transition-time $method;
}

@mixin transform($transforms) {
  transform: $transforms;
  -moz-transform: $transforms;
  -ms-transform: $transforms;
  -webkit-transform: $transforms;
}

//mixin star shape
@mixin star-shape($width: $width, $background-color: transparent, $z-index: 0) {
  @include transform(rotate(35deg));
  @include transition(all, .3s, linear);
  border-bottom: $width * .7 solid $background-color;
  border-left: $width solid transparent;
  border-right: $width solid transparent;
  display: inline-block;
  height: 0;
  margin-bottom: $width / 2;
  margin-top: $width / 2;
  position: relative;
  width: 0;
  margin-right: .4em;
  &amp;:before,
  &amp;:after {
    @include transition(all, .3s, linear);
    content: '';
    display: block;
    height: 0;
    position: absolute;
    width: 0;
    z-index: $z-index - 1;
  }
  &amp;:before {
    @include transform(rotate(-35deg));
    border-bottom: $width * .8 solid $background-color;
    border-left: $width * .3 solid transparent;
    border-right: $width * .3 solid transparent;
    left: -$width * .65;
    top: -$width * .45;
  }
  &amp;:after {
    @include transform(rotate(-70deg));
    border-bottom: $width * .7 solid $background-color;
    border-left: $width solid transparent;
    border-right: $width solid transparent;
    left: -$width * 1.05;
    top: $width * .03;
  }
}
.star-rating {
  text-align: center;
}
.rating {
  display: inline-block;
  position: relative;
  input[type="radio"] {
    position: absolute;
    opacity: 0;
    height: 1px;
    width: 1px;
    bottom: 0;
  }
}
// star shape
.star {
  @include star-shape(1.8em, $lightgray);
  float: right;
}

//with different color
$colors: $dark-green,
$green,
$yellow,
$orange,
$red;
@for $i from length($colors) through 1 {
  .star-#{$i} {
    &amp;.active,
    &amp;.active ~ .star {
      border-bottom-color: nth($colors, $i);
      &amp;:before,
      &amp;:after {
        border-bottom-color: nth($colors, $i);
      }
    }
  }
  input[type="radio"] + .star-#{$i} {
    cursor: pointer;
    &amp;:hover,
    &amp;:hover ~ .star {
      border-bottom-color: nth($colors, $i);
      &amp;:before,
      &amp;:after {
        border-bottom-color: nth($colors, $i);
      }
    }
  }
  .rating:not(:hover) {
    .radio-#{$i}:checked ~ .star {
      border-bottom-color: nth($colors, $i);
      &amp;:before,
      &amp;:after {
        border-bottom-color: nth($colors, $i);
      }
    }
  }
}

Codepen

It’s done. Cheers!

Happy Coding!

Admin Web Developer

Leave a Reply

Your email address will not be published. Required fields are marked *