Simple Rating Star CSS - html

I am trying to build a simple rating system using HTML and CSS. It's a very simple rating bar which consist of 5 stars.
fieldset,
label {
margin: 0;
padding: 0;
margin-bottom: 20px;
}
.rating {
border: none;
float: left;
}
.rating>input {
display: none;
}
.rating>label:before {
margin: 5px;
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating>.half:before {
content: "\f089";
position: absolute;
}
.rating>label {
color: #ddd;
float: right;
}
.rating>input:checked~label,
/* show gold star when clicked */
.rating:not(:checked)>label:hover,
/* hover current star */
.rating:not(:checked)>label:hover~label {
color: #FFD700;
}
/* hover previous stars in list */
.rating>input:checked+label:hover,
/* hover current star when changing rating */
.rating>input:checked~label:hover,
.rating>label:hover~input:checked~label,
/* lighten current selection */
.rating>input:checked~label:hover~label {
color: #FFED85;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<fieldset class="rating">
<input type="radio" id="5star" name="rating" value="5" />
<label class="full" for="star5" title="Excellent"></label>
<input type="radio" id="4halfstar" name="rating" value="4.5" />
<label class="half" for="4halfstar" title="Good"></label>
<input type="radio" id="4star" name="rating" value="4" />
<label class="full" for="4star" title="Pretty good"></label>
<input type="radio" id="3halfstar" name="rating" value="3.5" />
<label class="half" for="3halfstar" title="Nice"></label>
<input type="radio" id="3star" name="rating" value="3" />
<label class="full" for="3star" title="Ok"></label>
<input type="radio" id="2halfstar" name="rating" value="2.5" />
<label class="half" for="2halfstar" title="Kinda bad"></label>
<input type="radio" id="2star" name="rating" value="2" />
<label class="full" for="2star" title="Bad"></label>
<input type="radio" id="1halfstar" name="rating" value="1.5" />
<label class="half" for="1halfstar" title="Meh"></label>
<input type="radio" id="1star" name="rating" value="1" />
<label class="full" for="1star" title="Umm"></label>
<input type="radio" id="halfstar" name="rating" value="0.5" />
<label class="half" for="halfstar" title="Worst"></label>
</fieldset>
The problem is that I can only select the star up to 4.5 star which is half of the 5th star, what I want is to select the full 5 star. I have included an image of the rating bar below. Help is really appreciated.
Rating bar

Just a typo- needed to change star5 to 5star:
fieldset,
label {
margin: 0;
padding: 0;
margin-bottom: 20px;
}
.rating {
border: none;
float: left;
}
.rating>input {
display: none;
}
.rating>label:before {
margin: 5px;
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating>.half:before {
content: "\f089";
position: absolute;
}
.rating>label {
color: #ddd;
float: right;
}
.rating>input:checked~label,
/* show gold star when clicked */
.rating:not(:checked)>label:hover,
/* hover current star */
.rating:not(:checked)>label:hover~label {
color: #FFD700;
}
/* hover previous stars in list */
.rating>input:checked+label:hover,
/* hover current star when changing rating */
.rating>input:checked~label:hover,
.rating>label:hover~input:checked~label,
/* lighten current selection */
.rating>input:checked~label:hover~label {
color: #FFED85;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<fieldset class="rating">
<input type="radio" id="5star" name="rating" value="5" />
<label class="full" for="5star" title="Excellent"></label>
<input type="radio" id="4halfstar" name="rating" value="4.5" />
<label class="half" for="4halfstar" title="Good"></label>
<input type="radio" id="4star" name="rating" value="4" />
<label class="full" for="4star" title="Pretty good"></label>
<input type="radio" id="3halfstar" name="rating" value="3.5" />
<label class="half" for="3halfstar" title="Nice"></label>
<input type="radio" id="3star" name="rating" value="3" />
<label class="full" for="3star" title="Ok"></label>
<input type="radio" id="2halfstar" name="rating" value="2.5" />
<label class="half" for="2halfstar" title="Kinda bad"></label>
<input type="radio" id="2star" name="rating" value="2" />
<label class="full" for="2star" title="Bad"></label>
<input type="radio" id="1halfstar" name="rating" value="1.5" />
<label class="half" for="1halfstar" title="Meh"></label>
<input type="radio" id="1star" name="rating" value="1" />
<label class="full" for="1star" title="Umm"></label>
<input type="radio" id="halfstar" name="rating" value="0.5" />
<label class="half" for="halfstar" title="Worst"></label>
</fieldset>

Related

My rating button works on its own but when i duplicate it, it doesn't work

when I select a rating, it will stay "lit up" but when i duplicate it and give it a different class, when I select a rating on the second copy, it changes the first copy and the second copy doesn't get updated. its hard to explain what happens in text and its better to run it and see it for yourself
.price>div {
float: left;
height: 46px;
padding: 0 10px;
}
.price>div:not(:checked)>input {
position: absolute;
top: -9999px;
}
.price>div:not(:checked)>label {
float: right;
width: 1em;
overflow: hidden;
white-space: nowrap;
cursor: pointer;
font-size: 30px;
content: url(../images/shark\ unselected.png);
}
.price>div:not(:checked)>label:before {
content: url(../images/shark\ unselected.png);
}
.price>div>input:checked~label {
content: url(../images/shark.png);
}
.price>div:not(:checked)>label:hover,
.price>div:not(:checked)>label:hover~label {
content: url(../images/shark.png);
}
.price>div>input:checked+label:hover,
.price>div>input:checked+label:hover~label,
.price>div>input:checked~label:hover,
.price>div>input:checked~label:hover~label,
.price>div>label:hover~input:checked~label {
content: url(../images/shark.png);
}
.taste>div {
float: left;
height: 46px;
padding: 0 10px;
}
.taste>div:not(:checked)>input {
position: absolute;
top: -9999px;
}
.taste>div:not(:checked)>label {
float: right;
width: 1em;
overflow: hidden;
white-space: nowrap;
cursor: pointer;
font-size: 30px;
content: url(../images/price-tag\ unselected.png);
}
.taste>div:not(:checked)>label:before {
content: url(../images/price-tag\ unselected.png);
}
.taste>div>input:checked~label {
content: url(../images/price-tag.png);
}
.taste>div:not(:checked)>label:hover,
.taste>div:not(:checked)>label:hover~label {
content: url(../images/price-tag.png);
}
.taste>div>input:checked+label:hover,
.taste>div>input:checked+label:hover~label,
.taste>div>input:checked~label:hover,
.taste>div>input:checked~label:hover~label,
.taste>div>label:hover~input:checked~label {
content: url(../images/price-tag.png);
}
<div class="price d-inline-block w-100">
<h3>Price</h3>
<div class="d-inline-block">
<input type="radio" id="star5" name="price" value="5" />
<label for="star5" title="text">5 stars</label>
<input type="radio" id="star4" name="price" value="4" />
<label for="star4" title="text">4 stars</label>
<input type="radio" id="star3" name="price" value="3" />
<label for="star3" title="text">3 stars</label>
<input type="radio" id="star2" name="price" value="2" />
<label for="star2" title="text">2 stars</label>
<input type="radio" id="star1" name="price" value="1" />
<label for="star1" title="text">1 star</label>
</div>
</div>
<div class="taste d-inline-block w-100">
<h3>Taste</h3>
<div class="d-inline-block">
<input type="radio" id="star5" name="taste" value="5" />
<label for="star5" title="text">5 stars</label>
<input type="radio" id="star4" name="taste" value="4" />
<label for="star4" title="text">4 stars</label>
<input type="radio" id="star3" name="taste" value="3" />
<label for="star3" title="text">3 stars</label>
<input type="radio" id="star2" name="taste" value="2" />
<label for="star2" title="text">2 stars</label>
<input type="radio" id="star1" name="taste" value="1" />
<label for="star1" title="text">1 star</label>
</div>
</div>
Dont repeat ID, its unique element. It wouldnt work if you duplicate it. Rename second buttons ID and for.
<h3>Taste</h3>
<div class="d-inline-block">
<input type="radio" id="star5-2" name="taste" value="5" />
<label for="star5-2" title="text">5 stars</label>
<input type="radio" id="star4-2" name="taste" value="4" />
<label for="star4-2" title="text">4 stars</label>
<input type="radio" id="star3-2" name="taste" value="3" />
<label for="star3-2" title="text">3 stars</label>
<input type="radio" id="star2-2" name="taste" value="2" />
<label for="star2-2" title="text">2 stars</label>
<input type="radio" id="star1-2" name="taste" value="1" />
<label for="star1-2" title="text">1 star</label>
</div>

Half star rating with empty stars and radio buttons

Is it possible to achieve this with radio button labels and font awesome icons?
half star rating style I need
The most that I have managed to do is this, using two font awesome icons as radio button labels but I don't want unselected areas to be grey, rather empty star with border just like in the image above:
half star rating that I got
Here is my code:
.rate{
display: inline-block;
border: 0;
color: #b0afaf;
}
.rate > input{
display: none;
}
.rate > label {
float: right;
}
.rate > label:before {
display: inline-block;
font-size: 1.1rem;
padding: .3rem .2rem;
margin: 0;
cursor: pointer;
font-family: FontAwesome;
content: "\f005 ";
}
.rate .half:before {
content: "\f089 ";
position: absolute;
padding-right: 0;
}
input:checked ~ label,
label:hover, label:hover ~ label {
color: #dbc00b;
}
input:checked + label:hover, input:checked ~ label:hover,
input:checked ~ label:hover ~ label,
label:hover ~ input:checked ~ label {
color: #f2ec35;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
<div class="rate">
<input type="radio" id="star5" name="rating" value="5" />
<label for="star5" title="Awesome"></label>
<input type="radio" id="star4.5" name="rating" value="4.5" />
<label for="star4.5" class="half"></label>
<input type="radio" id="star4" name="rating" value="4" />
<label for="star4"></label>
<input type="radio" id="star3.5" name="rating" value="3.5" />
<label for="star3.5" class="half"></label>
<input type="radio" id="star3" name="rating" value="3" />
<label for="star3"></label>
<input type="radio" id="star2.5" name="rating" value="2.5" />
<label for="star2.5" class="half"></label>
<input type="radio" id="star2" name="rating" value="2" />
<label for="star2"></label>
<input type="radio" id="star1.5" name="rating" value="1.5" />
<label for="star1.5" class="half"></label>
<input type="radio" id="star1" name="rating" value="1" />
<label for="star1"></label>
<input type="radio" id="star0.5" name="rating" value="0.5" />
<label for="star0.5" class="half"></label>
</div>

css sibling limited to parent object

I've got the following issue, with a star rating css example. It's not working when more then one fieldset is added.
fieldset.star {
box-sizing:border-box;
width: -moz-fit-content;
width: -webkit-fit-content;
width: fit-content;
position:relative;
margin: 10px auto 10px auto;
}
fieldset.star.star5 > label:nth-of-type(even){
display:none;
}
fieldset.star > input[type="radio"] {
display: none;
}
fieldset.star > label {
display:inline-block;
float:right;
color:rgba(255,255,255,.5);
}
fieldset.star > label:before {
display: inline-block;
font-size: 4rem;
padding: .3rem .2rem;
margin: 0;
cursor: pointer;
font-family: FontAwesome;
content: "\f005 "; /* full star */
}
fieldset.star .half{
color:rgba(255,255,255,.1);
}
fieldset.star .half:before {
content: "\f089 "; /* half star no outline */
position: absolute;
padding-right: 0;
}
/* Click + hover color */
fieldset.star > input:checked ~ label, /* color current and previous stars on checked */
fieldset.star > label:hover,
fieldset.star > label:hover ~ label {
color: #DDBB00;
} /* color previous stars on hover */
/* Hover highlights */
fieldset.star > input:checked + label:hover,
fieldset.star > input:checked ~ label:hover, /* highlight current and previous stars */
fieldset.star > input:checked ~ label:hover ~ label, /* highlight previous selected stars for new rating */
fieldset.star > label:hover ~ input:checked ~ label /* highlight previous selected stars */ {
color: #FFD700;
}
<fieldset class="star">
<input type="radio" id="rating10" name="answer" value="10"><label for="rating10"></label>
<input type="radio" id="rating9" name="answer" value="9"><label class="half" for="rating9"></label>
<input type="radio" id="rating8" name="answer" value="8"><label for="rating8"></label>
<input type="radio" id="rating7" name="answer" value="7"><label class="half" for="rating7"></label>
<input type="radio" id="rating6" name="answer" value="6"><label for="rating6"></label>
<input type="radio" id="rating5" name="answer" value="5"><label class="half" for="rating5"></label>
<input type="radio" id="rating4" name="answer" value="4"><label for="rating4"></label>
<input type="radio" id="rating3" name="answer" value="3"><label class="half" for="rating3"></label>
<input type="radio" id="rating2" name="answer" value="2"><label for="rating2"></label>
<input type="radio" id="rating1" name="answer" value="1"><label class="half" for="rating1"></label>
</fieldset>
<fieldset class="star">
<input type="radio" id="rating110" name="answer" value="10"><label for="rating110"></label>
<input type="radio" id="rating19" name="answer" value="9"><label class="half" for="rating19"></label>
<input type="radio" id="rating18" name="answer" value="8"><label for="rating18"></label>
<input type="radio" id="rating17" name="answer" value="7"><label class="half" for="rating17"></label>
<input type="radio" id="rating16" name="answer" value="6"><label for="rating16"></label>
<input type="radio" id="rating15" name="answer" value="5"><label class="half" for="rating15"></label>
<input type="radio" id="rating14" name="answer" value="4"><label for="rating14"></label>
<input type="radio" id="rating13" name="answer" value="3"><label class="half" for="rating13"></label>
<input type="radio" id="rating12" name="answer" value="2"><label for="rating12"></label>
<input type="radio" id="rating11" name="answer" value="1"><label class="half" for="rating11"></label>
</fieldset>
Combined with this css, it works great,but not when there are more of the same elements [fieldset]. I can't find the right answer for this, but I want this to work out properly. I know i can use javascript/jquery [plugins] for this but I want to use this css solution if it's possible
Does someome have some thoughts?
Each set of fieldsets should have a different name.
fieldset.star {
box-sizing: border-box;
width: -moz-fit-content;
width: -webkit-fit-content;
width: fit-content;
position: relative;
margin: 10px auto 10px auto;
}
fieldset.star.star5>label:nth-of-type(even) {
display: none;
}
fieldset.star>input[type="radio"] {
display: none;
}
fieldset.star>label {
display: inline-block;
float: right;
color: rgba(255, 255, 255, .5);
}
fieldset.star>label:before {
display: inline-block;
font-size: 4rem;
padding: .3rem .2rem;
margin: 0;
cursor: pointer;
font-family: FontAwesome;
content: "\f005 ";
/* full star */
}
fieldset.star .half {
color: rgba(255, 255, 255, .1);
}
fieldset.star .half:before {
content: "\f089 ";
/* half star no outline */
position: absolute;
padding-right: 0;
}
/* Click + hover color */
fieldset.star>input:checked~label,
/* color current and previous stars on checked */
fieldset.star>label:hover,
fieldset.star>label:hover~label {
color: #DDBB00;
}
/* color previous stars on hover */
/* Hover highlights */
fieldset.star>input:checked+label:hover,
fieldset.star>input:checked~label:hover,
/* highlight current and previous stars */
fieldset.star>input:checked~label:hover~label,
/* highlight previous selected stars for new rating */
fieldset.star>label:hover~input:checked~label
/* highlight previous selected stars */
{
color: #FFD700;
}
<fieldset class="star">
<input type="radio" id="rating10" name="answer" value="10"><label for="rating10"></label>
<input type="radio" id="rating9" name="answer" value="9"><label class="half" for="rating9"></label>
<input type="radio" id="rating8" name="answer" value="8"><label for="rating8"></label>
<input type="radio" id="rating7" name="answer" value="7"><label class="half" for="rating7"></label>
<input type="radio" id="rating6" name="answer" value="6"><label for="rating6"></label>
<input type="radio" id="rating5" name="answer" value="5"><label class="half" for="rating5"></label>
<input type="radio" id="rating4" name="answer" value="4"><label for="rating4"></label>
<input type="radio" id="rating3" name="answer" value="3"><label class="half" for="rating3"></label>
<input type="radio" id="rating2" name="answer" value="2"><label for="rating2"></label>
<input type="radio" id="rating1" name="answer" value="1"><label class="half" for="rating1"></label>
</fieldset>
<fieldset class="star">
<input type="radio" id="rating110" name="answer1" value="10"><label for="rating110"></label>
<input type="radio" id="rating19" name="answer1" value="9"><label class="half" for="rating19"></label>
<input type="radio" id="rating18" name="answer1" value="8"><label for="rating18"></label>
<input type="radio" id="rating17" name="answer1" value="7"><label class="half" for="rating17"></label>
<input type="radio" id="rating16" name="answer1" value="6"><label for="rating16"></label>
<input type="radio" id="rating15" name="answer1" value="5"><label class="half" for="rating15"></label>
<input type="radio" id="rating14" name="answer1" value="4"><label for="rating14"></label>
<input type="radio" id="rating13" name="answer1" value="3"><label class="half" for="rating13"></label>
<input type="radio" id="rating12" name="answer1" value="2"><label for="rating12"></label>
<input type="radio" id="rating11" name="answer1" value="1"><label class="half" for="rating11"></label>
</fieldset>

Unable to stop a disabled checkbox from being affected by a hover event in the CSS

I have two sets of checkboxes, which act as a star rating system. One of the star ratings is disabled (by using the disabled attribute on the checkboxes), while the other one isnt.
When the user hovers over stars in the star rating system, they change colour to yellow. However if the star rating system is disabled, I do not want them to change colour when covered. I have tried to do this by using :not([disabled]) in the checkbox hover event in the CSS, but the stars still change colour on hover.
.rating,
.rating label {
margin: 0;
padding: 0;
margin-left: auto;
}
.rating {
border: none;
float: left;
}
.rating input {
display: none;
}
.rating label:before {
margin: 5px;
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating .half:before {
content: "\f089";
position: absolute;
}
.rating label {
color: #ddd;
float: right;
}
.rating input:checked~label,
/* show gold star when clicked */
.rating:not(:checked):not([disabled]) label:hover,
/* hover current star */
.rating:not(:checked) label:hover~label {
color: #FFD700;
}
/* hover current star when changing rating */
.rating input:checked~label:hover,
.rating label:hover~input:checked~label,
/* lighten current selection */
.rating input:checked~label:hover~label {
color: #FFED85;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<p>This star rating system should not change colour on hover as the checkboxes are disabled</p>
<fieldset class="rating" id="93">
<input type="checkbox" id="5star_1" name="rating" value="5" disabled/>
<label class="full" for="5star_1" title="Excellent"></label>
<input type="checkbox" id="4halfstar_1" name="rating" value="4.5" disabled/>
<label class="half" for="4halfstar_1" title="Good"></label>
<input type="checkbox" id="4star_1" name="rating" value="4" disabled/>
<label class="full" for="4star_1" title="Pretty good"></label>
<input type="checkbox" id="3halfstar_1" name="rating" value="3.5" disabled/>
<label class="half" for="3halfstar_1" title="Nice"></label>
<input type="checkbox" id="3star_1" name="rating" value="3" disabled/>
<label class="full" for="3star_1" title="Ok"></label>
<input type="checkbox" id="2halfstar_1" name="rating" value="2.5" disabled/>
<label class="half" for="2halfstar_1" title="Kinda bad"></label>
<input type="checkbox" id="2star_1" name="rating" value="2" disabled/>
<label class="full" for="2star_1" title="Bad"></label>
<input type="checkbox" id="1halfstar_1" name="rating" value="1.5" disabled/>
<label class="half" for="1halfstar_1" title="Meh"></label>
<input type="checkbox" id="1star_1" name="rating" value="1" disabled/>
<label class="full" for="1star_1" title="Umm"></label>
<input type="checkbox" id="halfstar_1" name="rating" value="0.5" disabled/>
<label class="half" for="halfstar_1" title="Worst"></label>
</fieldset>
<br><br>
<p>This one does what it is supposed to (change its colour on hover)</p>
<fieldset class="rating" id="23">
<input type="checkbox" id="5star" name="rating" value="5" />
<label class="full" for="5star" title="Excellent"></label>
<input type="checkbox" id="4halfstar" name="rating" value="4.5" />
<label class="half" for="4halfstar" title="Good"></label>
<input type="checkbox" id="4star" name="rating" value="4" />
<label class="full" for="4star" title="Pretty good"></label>
<input type="checkbox" id="3halfstar" name="rating" value="3.5" />
<label class="half" for="3halfstar" title="Nice"></label>
<input type="checkbox" id="3star" name="rating" value="3" />
<label class="full" for="3star" title="Ok"></label>
<input type="checkbox" id="2halfstar" name="rating" value="2.5" />
<label class="half" for="2halfstar" title="Kinda bad"></label>
<input type="checkbox" id="2star" name="rating" value="2" />
<label class="full" for="2star" title="Bad"></label>
<input type="checkbox" id="1halfstar" name="rating" value="1.5" />
<label class="half" for="1halfstar" title="Meh"></label>
<input type="checkbox" id="1star" name="rating" value="1" />
<label class="full" for="1star" title="Umm"></label>
<input type="checkbox" id="halfstar" name="rating" value="0.5" />
<label class="half" for="halfstar" title="Worst"></label>
</fieldset>
Here is what it looks like in the developer tools with the hover state being forced on:
You are asking CSS to check if the container class has the disabled attribute (or not). You need to ask it if the input within the class has this attribute
something like:
.rating input:checked~label,
.rating input:not(:checked):not(:disabled) + label:hover,
.rating input:not(:checked):not(:disabled) + label:hover~label {
color: #FFD700;
}
Note:
You may do well to remove the first and third rules as well, and simply have the second rule apply.
Snippet:
.rating,
.rating label {
margin: 0;
padding: 0;
margin-left: auto;
}
.rating {
border: none;
float: left;
}
.rating input {
display: none;
}
.rating label:before {
margin: 5px;
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating .half:before {
content: "\f089";
position: absolute;
}
.rating label {
color: #ddd;
float: right;
}
.rating input:checked~label,
.rating input:not(:checked):not(:disabled) + label:hover,
.rating input:not(:checked):not(:disabled) + label:hover~label {
color: #FFD700;
}
.rating input:checked~label:hover,
.rating label:hover~input:checked~label,
.rating input:checked~label:hover~label {
color: #FFED85;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<p>This star rating system should not change colour on hover as the checkboxes are disabled</p>
<fieldset class="rating" id="93">
<input type="checkbox" id="5star_1" name="rating" value="5" disabled/>
<label class="full" for="5star_1" title="Excellent"></label>
<input type="checkbox" id="4halfstar_1" name="rating" value="4.5" disabled/>
<label class="half" for="4halfstar_1" title="Good"></label>
<input type="checkbox" id="4star_1" name="rating" value="4" disabled/>
<label class="full" for="4star_1" title="Pretty good"></label>
<input type="checkbox" id="3halfstar_1" name="rating" value="3.5" disabled/>
<label class="half" for="3halfstar_1" title="Nice"></label>
<input type="checkbox" id="3star_1" name="rating" value="3" disabled/>
<label class="full" for="3star_1" title="Ok"></label>
<input type="checkbox" id="2halfstar_1" name="rating" value="2.5" disabled/>
<label class="half" for="2halfstar_1" title="Kinda bad"></label>
<input type="checkbox" id="2star_1" name="rating" value="2" disabled/>
<label class="full" for="2star_1" title="Bad"></label>
<input type="checkbox" id="1halfstar_1" name="rating" value="1.5" disabled/>
<label class="half" for="1halfstar_1" title="Meh"></label>
<input type="checkbox" id="1star_1" name="rating" value="1" disabled/>
<label class="full" for="1star_1" title="Umm"></label>
<input type="checkbox" id="halfstar_1" name="rating" value="0.5" disabled/>
<label class="half" for="halfstar_1" title="Worst"></label>
</fieldset>
<br><br>
<p>This one does what it is supposed to (change its colour on hover)</p>
<fieldset class="rating" id="23">
<input type="checkbox" id="5star" name="rating" value="5" />
<label class="full" for="5star" title="Excellent"></label>
<input type="checkbox" id="4halfstar" name="rating" value="4.5" />
<label class="half" for="4halfstar" title="Good"></label>
<input type="checkbox" id="4star" name="rating" value="4" />
<label class="full" for="4star" title="Pretty good"></label>
<input type="checkbox" id="3halfstar" name="rating" value="3.5" />
<label class="half" for="3halfstar" title="Nice"></label>
<input type="checkbox" id="3star" name="rating" value="3" />
<label class="full" for="3star" title="Ok"></label>
<input type="checkbox" id="2halfstar" name="rating" value="2.5" />
<label class="half" for="2halfstar" title="Kinda bad"></label>
<input type="checkbox" id="2star" name="rating" value="2" />
<label class="full" for="2star" title="Bad"></label>
<input type="checkbox" id="1halfstar" name="rating" value="1.5" />
<label class="half" for="1halfstar" title="Meh"></label>
<input type="checkbox" id="1star" name="rating" value="1" />
<label class="full" for="1star" title="Umm"></label>
<input type="checkbox" id="halfstar" name="rating" value="0.5" />
<label class="half" for="halfstar" title="Worst"></label>
</fieldset>

Stars (radio button) won't change colour when selected

I have two star rating systems using radio buttons. When the user clicks the star, that star and all the stars behind it are supposed to turn yellow. You can see this in the comments in my CSS code (such as /* show gold star when clicked */).
I believe it has something to do with with the ::before element which appears maybe? But I'm not totally sure on this:
.rating {
border: none;
float: left;
}
.rating {
margin: 0;
padding: 0;
}
.rating input {
display: none;
}
.rating label:before {
margin: 5px;
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating .half:before {
content: "\f089";
position: absolute;
}
.rating label {
color: #ddd;
float: right;
}
.rating input:checked~label,
/* show gold star when clicked */
.rating:not(:checked) label:hover,
/* hover current star */
.rating:not(:checked) label:hover~label {
color: #FFD700;
}
/* hover previous stars in list */
.rating input:checked+label:hover,
/* hover current star when changing rating */
.rating input:checked~label:hover,
.rating label:hover~input:checked~label,
/* lighten current selection */
.rating input:checked~label:hover~label {
color: #FFED85;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<fieldset class="rating">
<label class="full" title="Excellent">
<input type="radio" name="rating" value="5" />
</label>
<label class="half" title="Good">
<input type="radio" name="rating" value="4.5" />
</label>
<label class="full" title="Pretty Good">
<input type="radio" name="rating" value="4" />
</label>
<label class="half" title="Nice">
<input type="radio" name="rating" value="3.5" />
</label>
<label class="full" title="Ok">
<input type="radio" name="rating" value="3" />
</label>
<label class="half" title="Kinda Bad">
<input type="radio" name="rating" value="2.5" />
</label>
<label class="full" title="Bad">
<input type="radio" name="rating" value="2" />
</label>
<label class="half" title="Meh">
<input type="radio" name="rating" value="1.5" />
</label>
<label class="full" title="Umm">
<input type="radio" name="rating" value="1" />
</label>
<label class="half" title="Worst">
<input type="radio" name="rating" value="0.5" />
</label>
</fieldset>
<br><br>
<fieldset class="rating">
<label class="full" title="Excellent">
<input type="radio" name="rating" value="5" />
</label>
<label class="half" title="Good">
<input type="radio" name="rating" value="4.5" />
</label>
<label class="full" title="Pretty Good">
<input type="radio" name="rating" value="4" />
</label>
<label class="half" title="Nice">
<input type="radio" name="rating" value="3.5" />
</label>
<label class="full" title="Ok">
<input type="radio" name="rating" value="3" />
</label>
<label class="half" title="Kinda Bad">
<input type="radio" name="rating" value="2.5" />
</label>
<label class="full" title="Bad">
<input type="radio" name="rating" value="2" />
</label>
<label class="half" title="Meh">
<input type="radio" name="rating" value="1.5" />
</label>
<label class="full" title="Umm">
<input type="radio" name="rating" value="1" />
</label>
<label class="half" title="Worst">
<input type="radio" name="rating" value="0.5" />
</label>
</fieldset>
As mentioned above, this is achievable in pure CSS, but it does require adding IDs and for attributes and restructuring the HTML a little to make the labels and radio buttons siblings, like so:
$(document).on('click', 'fieldset label', function () {
var that = this;
setTimeout(function() {
console.log('hidden val=',$(that).parent().find("input[type='hidden']").val());
console.log('checked val=',$(that).parent().find("input[type='radio']:checked").val());
},1);
});
.rating {
border: none;
float: left;
}
.rating {
margin: 0;
padding: 0;
}
.rating input {
display: none;
}
.rating label:before {
margin: 5px;
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating .half:before {
content: "\f089";
position: absolute;
}
.rating label {
color: #ddd;
float: right;
}
.rating input:checked~label,
/* show gold star when clicked */
.rating:not(:checked) label:hover,
/* hover current star */
.rating:not(:checked) label:hover~label {
color: #FFD700;
}
/* hover previous stars in list */
.rating input:checked+label:hover,
/* hover current star when changing rating */
.rating input:checked~label:hover,
.rating label:hover~input:checked~label,
/* lighten current selection */
.rating input:checked~label:hover~label {
color: #FFED85;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<fieldset class="rating">
<input type="hidden" value="1">
<input type="radio" id="5star" name="rating" value="5" />
<label class="full" for="5star" title="Excellent"></label>
<input type="radio" id="4halfstar" name="rating" value="4.5" />
<label class="half" for="4halfstar" title="Good"></label>
<input type="radio" id="4star" name="rating" value="4" />
<label class="full" for="4star" title="Pretty good"></label>
<input type="radio" id="3halfstar" name="rating" value="3.5" />
<label class="half" for="3halfstar" title="Nice"></label>
<input type="radio" id="3star" name="rating" value="3" />
<label class="full" for="3star" title="Ok"></label>
<input type="radio" id="2halfstar" name="rating" value="2.5" />
<label class="half" for="2halfstar" title="Kinda bad"></label>
<input type="radio" id="2star" name="rating" value="2" />
<label class="full" for="2star" title="Bad"></label>
<input type="radio" id="1halfstar" name="rating" value="1.5" />
<label class="half" for="1halfstar" title="Meh"></label>
<input type="radio" id="1star" name="rating" value="1" />
<label class="full" for="1star" title="Umm"></label>
<input type="radio" id="halfstar" name="rating" value="0.5" />
<label class="half" for="halfstar" title="Worst"></label>
</fieldset>
<br><br>
<fieldset class="rating">
<input type="hidden" value="2">
<input type="radio" id="5star2" name="rating2" value="5" />
<label class="full" for="5star2" title="Excellent"></label>
<input type="radio" id="4halfstar2" name="rating2" value="4.5" />
<label class="half" for="4halfstar2" title="Good"></label>
<input type="radio" id="4star2" name="rating2" value="4" />
<label class="full" for="4star2" title="Pretty good"></label>
<input type="radio" id="3halfstar2" name="rating2" value="3.5" />
<label class="half" for="3halfstar2" title="Nice"></label>
<input type="radio" id="3star2" name="rating2" value="3" />
<label class="full" for="3star2" title="Ok"></label>
<input type="radio" id="2halfstar2" name="rating2" value="2.5" />
<label class="half" for="2halfstar2" title="Kinda bad"></label>
<input type="radio" id="2star2" name="rating2" value="2" />
<label class="full" for="2star2" title="Bad"></label>
<input type="radio" id="1halfstar2" name="rating2" value="1.5" />
<label class="half" for="1halfstar2" title="Meh"></label>
<input type="radio" id="1star2" name="rating2" value="1" />
<label class="full" for="1star2" title="Umm"></label>
<input type="radio" id="halfstar2" name="rating2" value="0.5" />
<label class="half" for="halfstar2" title="Worst"></label>
</fieldset>