Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/d2286fc92782752a59c45ccc6b4c77b1 to your computer and use it in GitHub Desktop.
Save anonymous/d2286fc92782752a59c45ccc6b4c77b1 to your computer and use it in GitHub Desktop.
Interactive Star Rating Component (CSS Only)
<!-- An interactive CSS star rating component.
Has hover and selected states, and could be integrated into a form very easily (as underneath the visual styling it is just radio buttons). Size and margin between the stars can be easily changed by amending the sass variables.
No JS, all CSS and HTML.
Works on IE9>, Chrome, Firefox, Safari
-->
<div class="container">
<h1>Interactive Star Rating Component (CSS Only) <a href="https://twitter.com/AshNolan_" target="blank">created by @AshNolan_</a></h1>
<div class="demo">
<div class="ratingControl">
<input type="radio" id="rating-5" name="rating" value="5">
<label class="ratingControl-stars ratingControl-stars--5" for="rating-5">5</label>
<input type="radio" id="rating-45" name="rating" value="4.5">
<label class="ratingControl-stars ratingControl-stars--45 ratingControl-stars--half" for="rating-45">45</label>
<input type="radio" id="rating-4" name="rating" value="4">
<label class="ratingControl-stars ratingControl-stars--4" for="rating-4">4</label>
<input type="radio" id="rating-35" name="rating" value="3.5">
<label class="ratingControl-stars ratingControl-stars--35 ratingControl-stars--half" for="rating-35">35</label>
<input type="radio" id="rating-3" name="rating" value="3">
<label class="ratingControl-stars ratingControl-stars--3" for="rating-3">3</label>
<input type="radio" id="rating-25" name="rating" value="2.5">
<label class="ratingControl-stars ratingControl-stars--25 ratingControl-stars--half" for="rating-25">25</label>
<input type="radio" id="rating-2" name="rating" value="2">
<label class="ratingControl-stars ratingControl-stars--2" for="rating-2">2</label>
<input type="radio" id="rating-15" name="rating" value="1.5">
<label class="ratingControl-stars ratingControl-stars--15 ratingControl-stars--half" for="rating-15">15</label>
<input type="radio" id="rating-1" name="rating" value="1">
<label class="ratingControl-stars ratingControl-stars--1" for="rating-1">1</label>
<input type="radio" id="rating-05" name="rating" value="0.5">
<label class="ratingControl-stars ratingControl-stars--05 ratingControl-stars--half" for="rating-05">05</label>
</div>
</div>
</div>

Interactive Star Rating Component (CSS Only)

An interactive CSS star rating component.

Has hover and selected states, and could be integrated into a form very easily (as underneath the visual styling it is just radio buttons). Size and margin between the stars can be easily changed by amending the sass variables.

No JS, all CSS and HTML.

Works on IE9>, Chrome, Firefox, Safari

A Pen by Ashley Nolan on CodePen.

License.

$ratingControl-star-size: 65px;
$ratingControl-star-margin: 10px;
$ratingControl-star-width: $ratingControl-star-size + $ratingControl-star-margin; // width takes into account the size and the margin, as this makes the label wider based on these two values
$ratingControl-star-count : 5;
$icon-path: 'http://ashleynolan.co.uk/assets/img/content/';
// SVG datauris used for the stars to avoid loading flash when interacted with
$icon-star: 'data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22no%22%3F%3E%3Csvg%20width%3D%2215px%22%20height%3D%2214px%22%20viewBox%3D%220%200%2015%2014%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%20%20%20%20%3Ctitle%3EEmpty%20Star%3C%2Ftitle%3E%20%20%20%20%3Cdefs%3E%3C%2Fdefs%3E%20%20%20%20%3Cg%20id%3D%22Page-1%22%20stroke%3D%22none%22%20stroke-width%3D%221%22%20fill%3D%22none%22%20fill-rule%3D%22evenodd%22%3E%20%20%20%20%20%20%20%20%3Cg%20id%3D%22Order-Details---Order-final---Not-reviewed%22%20transform%3D%22translate%28-108.000000%2C%20-385.000000%29%22%20fill%3D%22%23D5D5D5%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20id%3D%22Group%22%20transform%3D%22translate%28108.000000%2C%20316.000000%29%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20transform%3D%22translate%280.000000%2C%2069.000000%29%22%20id%3D%22Star-7%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cpolygon%20points%3D%227.5%2011.25%203.09161061%2013.5676275%203.93353806%208.65881373%200.367076128%205.18237254%205.2958053%204.46618627%207.5%200%209.7041947%204.46618627%2014.6329239%205.18237254%2011.0664619%208.65881373%2011.9083894%2013.5676275%20%22%3E%3C%2Fpolygon%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%3C%2Fg%3E%3C%2Fsvg%3E';
$icon-star--filled: 'data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22no%22%3F%3E%3Csvg%20width%3D%2215px%22%20height%3D%2214px%22%20viewBox%3D%220%200%2015%2014%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%20%20%20%20%3Ctitle%3ESolid%20Star%3C%2Ftitle%3E%20%20%20%20%3Cdefs%3E%3C%2Fdefs%3E%20%20%20%20%3Cg%20id%3D%22Page-1%22%20stroke%3D%22none%22%20stroke-width%3D%221%22%20fill%3D%22none%22%20fill-rule%3D%22evenodd%22%3E%20%20%20%20%20%20%20%20%3Cg%20id%3D%22Order-Details---Order-final---Not-reviewed%22%20transform%3D%22translate%28-108.000000%2C%20-385.000000%29%22%20fill%3D%22%23FEC844%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20id%3D%22Group%22%20transform%3D%22translate%28108.000000%2C%20316.000000%29%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20transform%3D%22translate%280.000000%2C%2069.000000%29%22%20id%3D%22Star-7%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cpolygon%20points%3D%227.5%2011.25%203.09161061%2013.5676275%203.93353806%208.65881373%200.367076128%205.18237254%205.2958053%204.46618627%207.5%200%209.7041947%204.46618627%2014.6329239%205.18237254%2011.0664619%208.65881373%2011.9083894%2013.5676275%20%22%3E%3C%2Fpolygon%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%3C%2Fg%3E%3C%2Fsvg%3E';
$icon-star--light: 'data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22no%22%3F%3E%3Csvg%20width%3D%2215px%22%20height%3D%2214px%22%20viewBox%3D%220%200%2015%2014%22%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%3E%20%20%20%20%3Ctitle%3ESolid%20Star%20%E2%80%93%20Light%3C%2Ftitle%3E%20%20%20%20%3Cdefs%3E%3C%2Fdefs%3E%20%20%20%20%3Cg%20id%3D%22Page-1%22%20stroke%3D%22none%22%20stroke-width%3D%221%22%20fill%3D%22none%22%20fill-rule%3D%22evenodd%22%3E%20%20%20%20%20%20%20%20%3Cg%20id%3D%22Order-Details---Order-final---Not-reviewed%22%20transform%3D%22translate%28-108.000000%2C%20-385.000000%29%22%20fill%3D%22%23FFE39C%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20id%3D%22Group%22%20transform%3D%22translate%28108.000000%2C%20316.000000%29%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cg%20transform%3D%22translate%280.000000%2C%2069.000000%29%22%20id%3D%22Star-7%22%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cpolygon%20points%3D%227.5%2011.25%203.09161061%2013.5676275%203.93353806%208.65881373%200.367076128%205.18237254%205.2958053%204.46618627%207.5%200%209.7041947%204.46618627%2014.6329239%205.18237254%2011.0664619%208.65881373%2011.9083894%2013.5676275%20%22%3E%3C%2Fpolygon%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%20%20%20%20%3C%2Fg%3E%20%20%20%20%3C%2Fg%3E%3C%2Fsvg%3E';
.ratingControl {
position: relative;
width: $ratingControl-star-width * $ratingControl-star-count;
height: $ratingControl-star-size;
user-select: none;
margin: 0 auto;
input {
visibility: hidden;
}
}
.ratingControl-stars {
position: absolute;
top: 0;
width: $ratingControl-star-width;
height: $ratingControl-star-size;
background-image: url($icon-star);
// uses datauri, but image is actually being pulled from here: '#{$icon-path}/star--empty.svg';
background-size: auto $ratingControl-star-size;
background-repeat: no-repeat;
cursor: pointer;
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
// hover effect applied to the hovered star and all preceeding stars (and if the radio button is checked)
&:hover,
.ratingControl-stars:hover ~ &,
input:checked ~ & {
background-image: url($icon-star--filled);
// uses datauri, but image is actually being pulled from here: #{$icon-path}/star--filled.svg
}
// displays a slightly different coloured star when someone is actively clicking the item
&:active,
.ratingControl-stars:active ~ &,
input:checked ~ &:active {
background-image: url($icon-star--light);
// uses datauri, but image is actually being pulled from here: #{$icon-path}/star--filled--light.svg
}
}
.ratingControl-stars--05 { left: $ratingControl-star-width * 0; }
.ratingControl-stars--1 { left: $ratingControl-star-width * 0; }
.ratingControl-stars--15 { left: $ratingControl-star-width * 1; }
.ratingControl-stars--2 { left: $ratingControl-star-width * 1; }
.ratingControl-stars--25 { left: $ratingControl-star-width * 2; }
.ratingControl-stars--3 { left: $ratingControl-star-width * 2; }
.ratingControl-stars--35 { left: $ratingControl-star-width * 3; }
.ratingControl-stars--4 { left: $ratingControl-star-width * 3; }
.ratingControl-stars--45 { left: $ratingControl-star-width * 4; }
.ratingControl-stars--5 { left: $ratingControl-star-width * 4; }
.ratingControl-stars--half {
width: ceil($ratingControl-star-size / 2) + 1;
}
// Define the fonts used in the Pen
@import url(http://fonts.googleapis.com/css?family=Francois+One);
@import url(http://fonts.googleapis.com/css?family=PT+Sans);
@font-face {
font-family: 'Audiowide';
font-style: normal;
font-weight: 400;
src: local('Audiowide'), local('Audiowide-Regular'), url(http://themes.googleusercontent.com/static/fonts/audiowide/v2/8XtYtNKEyyZh481XVWfVOj8E0i7KZn-EPnyo3HZu7kw.woff) format('woff');
}
$color-bg: #fff; //background for the demo
body {
font-size: 62.5%;
background-color: $color-bg;
margin: 10px;
border: 1px solid #333;
}
h1, h2, h3 {
font-family: 'PT Sans', sans-serif;
text-transform: uppercase;
}
h1 {
font-size: 2.4em;
background-color: rgba(41, 41, 41, 1);
text-align: center;
padding: 20px;
margin: 0;
color: #fff;
a {
display: block;
margin-top: 10px;
text-transform: none;
color: #aaa;
font-size: 16px;
text-decoration: none;
}
}
.demo {
padding: 40px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment