Why is this :checked selector not working? - html

In the example below, why does the selected star and all the stars before it not stay yellow?
#import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
.rating {
display: inline;
border: none;
}
.rating > label > input {
margin: 0px;
}
.rating > label:before {
margin: 0px;
font-size: 1em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
.rating > label {
color: #ddd;
}
.rating > input:checked ~ label, /* show gold star when clicked */
.rating:hover label { color: #FFD700; } /* hover previous stars in list */
.rating:not(:checked) > label:hover { color: #FFD700; } /* hover current star */
.rating:not(:checked) > label:hover ~ label { color: #ddd; } /* un-hover stars after current star */
<form>
<fieldset class="rating">
<label for="radio1"><input id="radio1" type="radio" value="1"></label>
<label for="radio2"><input id="radio2" type="radio" value="2"></label>
<label for="radio3"><input id="radio3" type="radio" value="3"></label>
<input type="submit" value="Submit">
</fieldset>
</form>
The lines of code that are causing the problem in my code are:
/* css */
.rating > input:checked ~ label, /* show gold star when clicked */
.rating:hover label { color: #FFD700; } /* hover previous stars in list */
I can't figure out why the selectors aren't working as intended.
Please help!

because your checkbox isn't a child of .rating, you've wrapped it in a label so your code must be you need to take your input out of the label:
<input id="radio1" type="radio" value="1"><label for="radio1"></label>
and then change your css to:
.rating > input:checked + label{}

A working JSFiddle https://jsfiddle.net/LeoAref/s3btcwjg/
Here we can use the Adjacent sibling CSS selector +
.rating input:checked + label,
.rating input:hover + label,
.rating label:hover { color: #FFD700; }
And edit the HTML to be like:
<form>
<fieldset class="rating">
<input id="radio1" type="radio" name="radio" value="1"><label for="radio1"></label>
<input id="radio2" type="radio" name="radio" value="2"><label for="radio2"></label>
<input id="radio3" type="radio" name="radio" value="3"><label for="radio3"></label>
<input type="submit" value="Submit">
</fieldset>
</form>

Related

Star Rating component fill all previous stars on hover

I am currently making a Star Rating component for our website, I use angular11 (should not matter I think), so far I have the rendering of the 5 stars depending on the current rating value and such, but I struggle to get the styling for the hovering correct.
Basicly for example if I have 2 out of 5 stars and I hover the 4th star the 3th should be filled too.
I struggle to find the correct css to achieve that
Thats what I got so far in CSS
.rating-icon-empty { fill: #9e9e9e }
.rating-icon { fill: #7C4DFF; margin-right: 5px; }
.editable .rating-icon-empty:hover { fill: #7C4DFF; cursor: pointer;}
My current CSS only fills the star that is hovered but not the previous unfilled stars.
Is there a way to achieve this with pure CSS or do I need to rely on hover listeners for each star?
I had your problem before, read these:
First Solution:
#import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
fieldset, label { margin: 0; padding: 0; }
body{ margin: 20px; }
h1 { font-size: 1.5em; margin: 10px; }
/****** Style Star Rating Widget *****/
.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;
}
/***** CSS Magic to Highlight Stars on Hover *****/
.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; }
<h1>Pure CSS Star Rating Widget</h1>
<fieldset class="rating">
<input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
<input type="radio" id="star4half" name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
<input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
<input type="radio" id="star3half" name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
<input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
<input type="radio" id="star2half" name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
<input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1half" name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
<input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
<input type="radio" id="starhalf" name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
</fieldset>
Source:
https://codepen.io/jamesbarnett/pen/vlpkh
Second Solution:
body {
display: flex;
justify-content: center;
align-items: center;
}
.rating {
unicode-bidi: bidi-override;
direction: rtl;
}
.rating>span {
display: inline-block;
position: relative;
width: 1.1em;
}
.rating>span:hover:before,
.rating>span:hover~span:before {
content: "\2605";
position: absolute;
}
<body>
<div class="rating">
<span>☆</span><span>☆</span><span>☆</span><span>☆</span><span>☆</span>
</div>
</body>
Source:
https://css-tricks.com/star-ratings/
if you have any problems let me know.

CSS: legend in the same row as fieldset

I've a stars rating system and I want to put some legend on it (so it is not only the stars icons).
As I'm using Bootstrap4, I was thinking on using a row, and 2 cols to align them horizontally, however, my legend keeps getting some pixels above my stars.
I've also tried using margin-top: 10px on the legend, without the desired result.
#import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
fieldset, label {
margin: 0;
padding: 0;
}
body {
margin: 20px;
}
h1 {
font-size: 1.5em;
margin: 10px;
}
/****** Style Star Rating Widget *****/
.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;
}
/***** CSS Magic to Highlight Stars on Hover *****/
.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://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-12 col-lg-12">
<fieldset class="rating hidden text-center">
<legend>Califícanos: </legend>
<input type="radio" id="star5" name="rating" value="5"/><label class="full"
for="star5"
title="Awesome - 5 stars"></label>
<input type="radio" id="star4half" name="rating" value="4.5"/><label class="half"
for="star4half"
title="Pretty good - 4.5 stars"></label>
<input type="radio" id="star4" name="rating" value="4"/><label class="full"
for="star4"
title="Pretty good - 4 stars"></label>
<input type="radio" id="star3half" name="rating" value="3.5"/><label class="half"
for="star3half"
title="Meh - 3.5 stars"></label>
<input type="radio" id="star3" name="rating" value="3"/><label class="full"
for="star3"
title="Meh - 3 stars"></label>
<input type="radio" id="star2half" name="rating" value="2.5"/><label class="half"
for="star2half"
title="Kinda bad - 2.5 stars"></label>
<input type="radio" id="star2" name="rating" value="2"/><label class="full"
for="star2"
title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1half" name="rating" value="1.5"/><label class="half"
for="star1half"
title="Meh - 1.5 stars"></label>
<input type="radio" id="star1" name="rating" value="1"/><label class="full"
for="star1"
title="Sucks big time - 1 star"></label>
<input type="radio" id="starhalf" name="rating" value="0.5"/><label class="half"
for="starhalf"
title="Sucks big time - 0.5 stars"></label>
</fieldset>
</div>
</div>
https://codepen.io/anon/pen/GevVYZ
I've also tried using span tag:
#import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
fieldset, label {
margin: 0;
padding: 0;
}
body {
margin: 20px;
}
h1 {
font-size: 1.5em;
margin: 10px;
}
/****** Style Star Rating Widget *****/
.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;
}
/***** CSS Magic to Highlight Stars on Hover *****/
.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://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-6 col-lg-6">
<span class="calificanos-text hidden">Califícanos: </span>
</div>
<div class="col-md-6 col-lg-6">
<fieldset class="rating hidden text-center">
<input type="radio" id="star5" name="rating" value="5"/><label class="full"
for="star5"
title="Awesome - 5 stars"></label>
<input type="radio" id="star4half" name="rating" value="4.5"/><label class="half"
for="star4half"
title="Pretty good - 4.5 stars"></label>
<input type="radio" id="star4" name="rating" value="4"/><label class="full"
for="star4"
title="Pretty good - 4 stars"></label>
<input type="radio" id="star3half" name="rating" value="3.5"/><label class="half"
for="star3half"
title="Meh - 3.5 stars"></label>
<input type="radio" id="star3" name="rating" value="3"/><label class="full"
for="star3"
title="Meh - 3 stars"></label>
<input type="radio" id="star2half" name="rating" value="2.5"/><label class="half"
for="star2half"
title="Kinda bad - 2.5 stars"></label>
<input type="radio" id="star2" name="rating" value="2"/><label class="full"
for="star2"
title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1half" name="rating" value="1.5"/><label class="half"
for="star1half"
title="Meh - 1.5 stars"></label>
<input type="radio" id="star1" name="rating" value="1"/><label class="full"
for="star1"
title="Sucks big time - 1 star"></label>
<input type="radio" id="starhalf" name="rating" value="0.5"/><label class="half"
for="starhalf"
title="Sucks big time - 0.5 stars"></label>
</fieldset>
</div>
</div>
https://codepen.io/anon/pen/rRGBvB
This is what I see in my html (localhost):
What I see locally:
I wrapped them all in the div col6 and set them to display flex and then i removed the margin 5px in .rating > label:before
#import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);
/* NEW */
.col-md-6.col-lg-6 {
display: flex;
}
.rating > label:before {
font-size: 1.25em;
font-family: FontAwesome;
display: inline-block;
content: "\f005";
}
/* END */
fieldset, label {
margin: 0;
padding: 0;
}
body {
margin: 20px;
}
h1 {
font-size: 1.5em;
margin: 10px;
}
/****** Style Star Rating Widget *****/
.rating {
border: none;
float: left;
}
.rating > input {
display: none;
}
.rating > .half:before {
content: "\f089";
position: absolute;
}
.rating > label {
color: #ddd;
float: right;
}
/***** CSS Magic to Highlight Stars on Hover *****/
.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;
}
<div class="row">
<div class="col-md-6 col-lg-6">
<span class="calificanos-text hidden">Califícanos: </span>
<fieldset class="rating hidden text-center">
<input type="radio" id="star5" name="rating" value="5"/><label class="full"
for="star5"
title="Awesome - 5 stars"></label>
<input type="radio" id="star4half" name="rating" value="4.5"/><label class="half"
for="star4half"
title="Pretty good - 4.5 stars"></label>
<input type="radio" id="star4" name="rating" value="4"/><label class="full"
for="star4"
title="Pretty good - 4 stars"></label>
<input type="radio" id="star3half" name="rating" value="3.5"/><label class="half"
for="star3half"
title="Meh - 3.5 stars"></label>
<input type="radio" id="star3" name="rating" value="3"/><label class="full"
for="star3"
title="Meh - 3 stars"></label>
<input type="radio" id="star2half" name="rating" value="2.5"/><label class="half"
for="star2half"
title="Kinda bad - 2.5 stars"></label>
<input type="radio" id="star2" name="rating" value="2"/><label class="full"
for="star2"
title="Kinda bad - 2 stars"></label>
<input type="radio" id="star1half" name="rating" value="1.5"/><label class="half"
for="star1half"
title="Meh - 1.5 stars"></label>
<input type="radio" id="star1" name="rating" value="1"/><label class="full"
for="star1"
title="Sucks big time - 1 star"></label>
<input type="radio" id="starhalf" name="rating" value="0.5"/><label class="half"
for="starhalf"
title="Sucks big time - 0.5 stars"></label>
</fieldset>
</div>
</div>

How do I change background image on stars?

I am trying to do a css hover effect.
I want the previous stars to also change their background image as the stars are hovered.
I have tried some different things but can't seem to understand the CSS that has to be used. Can anybody guide me?
I have the rating project here
HTML
<form action="" method="post" class="absolute babesRate flex">
<label>
<input type="radio" name="vote" value="1" id="vote1">
</label>
<label>
<input type="radio" name="vote" value="2" id="vote2">
</label>
<label>
<input type="radio" name="vote" value="3" id="vote3">
</label>
<label>
<input type="radio" name="vote" value="4" id="vote4">
</label>
<label>
<input type="radio" name="vote" value="5" id="vote5">
</label>
</form>
CSS
label{
background-image: url(../img/voteEmpty.png);
background-repeat: no-repeat;
width: 40px;
height: 40px;
}
label+input[type="radio"]:checked{
background-image: url(../img/voteFull.png);
}
label:hover{
background-image: url(../img/voteFull.png);
}
You need to do like this, where you put the input outside the label, or else they will not "see" the label(s), and then, with the flexbox order property, you swap them in markup so you can make use of the sibling selector ~.
.flex {
display: flex;
}
label {
display: inline-block;
background: gray url(../img/voteEmpty.png);
background-repeat: no-repeat;
width: 40px;
height: 40px;
margin: 2px;
}
input[type=radio] {
display: none
}
label:nth-of-type(1) { order: 5; }
label:nth-of-type(2) { order: 4; }
label:nth-of-type(3) { order: 3; }
label:nth-of-type(4) { order: 2; }
label:nth-of-type(5) { order: 1; }
input:checked ~ label {
background: red url(../img/voteFull.png);
}
form:hover label {
background: gray url(../img/voteEmpty.png);
}
form label:hover,
label:hover ~ label {
background: red url(../img/voteFull.png);
}
<form action="" method="post" class="absolute babesRate flex">
<input type="radio" name="vote" value="5" id="vote5">
<label for="vote5">5</label>
<input type="radio" name="vote" value="4" id="vote4">
<label for="vote4">4</label>
<input type="radio" name="vote" value="3" id="vote3">
<label for="vote3">3</label>
<input type="radio" name="vote" value="2" id="vote2">
<label for="vote2">2</label>
<input type="radio" name="vote" value="1" id="vote1">
<label for="vote1">1</label>
</form>
At the end of your styles, you have this:
.parent label:hover,
.parent label:hover ~ label {
background-image: url(../img/voteFull.png);
}
Change that to this one:
label:hover,
label:hover ~ label {
background-image: url(../img/voteFull.png);
}

How to hyperlink input files/labels

I am using a star rating system for email templates. Some examples I found allow you to only select the rating, but I would like to hyperlink each star so that it takes you to a different link depending on the star that is clicked. Is this possible? I tried to hyperlink the entire input type but it removes the star icon.
Here is the CSS and HTML, as well as an example: https://jsfiddle.net/pz62gcq0/
<!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Star rating using pure CSS</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
.rate {
border: 1px solid #cccccc;
float: left;
height: 46px;
padding: 0 10px;
}
.rate:not(:checked) > input {
position:absolute;
top:-9999px;
}
.rate:not(:checked) > label {
float:right;
width:1em;
overflow:hidden;
white-space:nowrap;
cursor:pointer;
font-size:30px;
color:#ccc;
}
.rate:not(:checked) > label:before {
content: '★ ';
}
.rate > input:checked ~ label {
color: #ffc700;
}
.rate:not(:checked) > label:hover,
.rate:not(:checked) > label:hover ~ label {
color: #deb217;
}
.rate > input:checked + label:hover,
.rate > input:checked + label:hover ~ label,
.rate > input:checked ~ label:hover,
.rate > input:checked ~ label:hover ~ label,
.rate > label:hover ~ input:checked ~ label {
color: #c59b08;
}
</style>
<body>
<div class="rate">
<input type="radio" id="star5" name="rate" value="5" /><label for="star5" title="text">5 stars</label>
<input type="radio" id="star4" name="rate" value="4" /><label for="star4" title="text">4 stars</label>
<input type="radio" id="star3" name="rate" value="3" /><label for="star3" title="text">3 stars</label>
<input type="radio" id="star2" name="rate" value="2" /><label for="star2" title="text">2 stars</label>
<input type="radio" id="star1" name="rate" value="1" /><label for="star1" title="text">1 star</label>
</div>
</body>
</html>
You could try something like this: https://jsfiddle.net/v0qrgopz/1/
I've converted the inputs to links so that clicking on them takes you somewhere. I've used Flexbox to reverse the order of them so that the selector a:hover ~ a works.
You could use some inline JavaScript via the onclick event attribute. Inside each of your input tags include the attribute:
onclick="window.open('http://www.stackoverflow.com')"
Example: https://jsfiddle.net/KyleVassella/pz62gcq0/2/
Insert whatever link you like. This could also be done using a separate JS file if you don't want to use inline.

How can I activate a CSS style for a label when hovering over the associated checkbox?

Every time I hover over the label of a checkbox it turns yellow:
Markup
<input type="checkbox" value="hello" id="hello" name="deletefiles[]"/>
<label for="hello">hello</label>
CSS
label:hover, label:active {
background:yellow;
}
When I hover over the related checkbox, I want the label to highlight. Is there a way to fire the same hover rule using CSS if I hover over the checkbox as well? Or will I have to use JavaScript for this...?
You can use a CSS sibling selector, like this:
label:hover, label:active, input:hover+label, input:active+label {
background:yellow;
}
Note that this won't work in IE6.
Just put the checkbox inside the label:
<label for="hello">
<input type="checkbox" value="hello" id="hello" name="deletefiles[]"/>
hello
</label>
Now when you hover over the checkbox, you'll also be hovering over the label, and your existing rules will suffice to highlight it.
The jQuery solution:
$(document).ready(function(){
$('#hello, label[for="hello"]').hover(function(){$(this).addClass('.hover');},
function(){$(this).removeClass('.hover');});
});
...
.hover
{
background-color: yellow;
}
And this DOES work in IE6.
/*CSS*/
/*-------------------------------------------------*/
input:not(:checked) + label:hover{
color: #d51e22;
cursor: pointer;
background-color: #bbb;
}
input:checked + label[for="tab1"],
input:checked + label[for="tab2"],
input:checked + label[for="tab3"],
input:checked + label[for="tab4"]{
 color: #d51e22;
 text-shadow: 0 0.04em 0.04em rgba(0,0,0,0.35);
background-color: #000;
}
label[for="tab1"],[for="tab2"],[for="tab3"],[for="tab4"] {
width:24%;
display: inline-block;
margin: 0 0 -1px;
padding: 25px 25px;
font-weight: 600;
font-size:24px;
text-align: center;
border-radius:15px;
background-color: #d51e22;
color: #fff;
/*border: 1px solid transparent;*/
}
/*HTML*/
/*-------------------------------------------------*/
<input id="tab1" type="radio" name="tabs" checked>
<label for="tab1">Text here</label>
<input id="tab2" type="radio" name="tabs">
<label for="tab2">Text here</label>
<input id="tab3" type="radio" name="tabs">
<label for="tab3">Text here</label>
<input id="tab4" type="radio" name="tabs">
<label for="tab4">Text here</label>