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>
Related
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>
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>
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>
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>
Thanks for taking a look...
Please see the following fiddle that shows two CSS only rating star widgets or the code below. When you select a rating on the second widget, for some reason it always changes the rating on the first widget. Any idea where the error is in my CSS? Sorry if this is a basic question, I am not very good with CSS yet. Thanks!
HTML:
<div class="stars">
<div class="rating" style="width:65%"></div>
<input type="radio" name="rating" id="star5" value="5">
<label for="star5"></label>
<input type="radio" name="rating" id="star4" value="4">
<label for="star4"></label>
<input type="radio" name="rating" id="star3" value="3">
<label for="star3"></label>
<input type="radio" name="rating" id="star2" value="2">
<label for="star2"></label>
<input type="radio" name="rating" id="star1" value="1">
<label for="star1"></label>
</div>
</br>
<div class="stars">
<div class="rating" style="width:65%"></div>
<input type="radio" name="rating" id="star5" value="5">
<label for="star5"></label>
<input type="radio" name="rating" id="star4" value="4">
<label for="star4"></label>
<input type="radio" name="rating" id="star3" value="3">
<label for="star3"></label>
<input type="radio" name="rating" id="star2" value="2">
<label for="star2"></label>
<input type="radio" name="rating" id="star1" value="1">
<label for="star1"></label>
</div>
CSS:
.stars{
width: 130px;
height: 26px;
background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 0 repeat-x;
position: relative;
}
.stars .rating{
height: 26px;
background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -26px repeat-x;
}
.stars input{
display: none;
}
.stars label{
display: none;
position: absolute;
top: 0;
left: 0;
height: 26px;
width: 130px;
cursor: pointer;
}
.stars:hover label{
display: block;
}
.stars label:hover{
background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -52px repeat-x;
}
.stars label + input + label{width: 104px;}
.stars label + input + label + input + label{width: 78px;}
.stars label + input + label + input + label + input + label{width: 52px;}
.stars label + input + label + input + label + input + label + input + label{width: 26px;}
.stars input:checked + label{
display: block;
background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -52px repeat-x;
}
Id's are unique in HTML. You can't have more than one element with the same id on the page and expect it to behave. This is exemplified by the behavior you are seeing. Since your label's have the for attribute set to the id's, the browser will check the input's for the first id it "sees", since it is only expecting one.
Each radio group also needs to have a unique name attribute, or all radios will be treated as a singe group and only one star will be able to be selected on the page.
So you need to change the id's for the second set of radios:
<div class="stars">
<div class="rating" style="width:65%"></div>
<input type="radio" name="rating" id="star5" value="5">
<label for="star5"></label>
<input type="radio" name="rating" id="star4" value="4">
<label for="star4"></label>
<input type="radio" name="rating" id="star3" value="3">
<label for="star3"></label>
<input type="radio" name="rating" id="star2" value="2">
<label for="star2"></label>
<input type="radio" name="rating" id="star1" value="1">
<label for="star1"></label>
</div>
</br>
<div class="stars">
<div class="rating" style="width:65%"></div>
<input type="radio" name="ratinga" id="star5a" value="5">
<label for="star5a"></label>
<input type="radio" name="ratinga" id="star4a" value="4">
<label for="star4a"></label>
<input type="radio" name="ratinga" id="star3a" value="3">
<label for="star3a"></label>
<input type="radio" name="ratinga" id="star2a" value="2">
<label for="star2a"></label>
<input type="radio" name="ratinga" id="star1a" value="1">
<label for="star1a"></label>
</div>