I ran across an example of a CSS star rating system.
Could someone help me understand how it works?
Namely, what does this selector target:
.reviewer__stars input:checked ~ label { color: #FFC40E; }
and what does this selector target:
.reviewer__stars input + label:hover, .reviewer__stars input + label:hover ~ label {
color: #ffdd74;
}
Here’s the complete code:
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>Star rating</title>
<style>
.reviewer__stars {
display: flex;
justify-content: center;
}
.reviewer__stars input { display: none; }
.reviewer__stars input:checked ~ label { color: #FFC40E; }
.reviewer__stars input + label { font-size: 0; }
.reviewer__stars input + label:before {
content: '\2605';
font-size: 2rem;
}
.reviewer__stars input + label[for="star5"] { order: 5; }
.reviewer__stars input + label[for="star4"] { order: 4; }
.reviewer__stars input + label[for="star3"] { order: 3; }
.reviewer__stars input + label[for="star2"] { order: 2; }
.reviewer__stars input + label[for="star1"] { order: 1; }
.reviewer__stars input + label:hover, .reviewer__stars input + label:hover ~ label {
color: #ffdd74;
}
</style>
</head>
<body>
<form action="/reviews/5a68596aa4e0c32fd867be7c" class="reviewer" method="post">
<div class="reviewer__meta">
<div class="reviewer__stars">
<input id="star5" name="rating" required="required" type="radio" value="5">
<label for="star5">5 Stars</label>
<input id="star4" name="rating" required="required" type="radio" value="4">
<label for="star4">4 Stars</label>
<input id="star3" name="rating" required="required" type="radio" value="3">
<label for="star3">3 Stars</label>
<input id="star2" name="rating" required="required" type="radio" value="2">
<label for="star2">2 Stars</label>
<input id="star1" name="rating" required="required" type="radio" value="1">
<label for="star1">1 Stars</label>
</div>
</div>
</form>
</body>
</html>