Reputation: 147
i want to create a rate star with example my html script.
this css script run if my html script like <input type="radio" name="rating" id="rating-5"><label for="rating-5" class="fas fa-star"></label>
,
but i want run like this
<label for="rating-5" class="fas fa-star"> <input type="radio" name="rating" id="rating-5"> </label>
like the example script below and css not working
example:
{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: arial;
}
.star-rating input {
display: none;
}
.star-rating {
margin: 50px auto;
display: table;
width: 350px;
}
.star-rating label {
padding: 10px;
float: right;
font-size: 44px;
color: #eee;
}
.star-rating input:not(:checked)~label:hover,
.star-rating input:not(:checked)~label:hover~label {
color: #ffc107;
}
.star-rating input:checked {
color: #ffc107;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.10.2/css/all.css" />
<div class="star-rating">
<div class="star-input">
<label for="rating-5" class="fas fa-star">
<input type="radio" name="rating" id="rating-5"></label>
<label for="rating-4" class="fas fa-star">
<input type="radio" name="rating" id="rating-4"></label>
<label for="rating-3" class="fas fa-star">
<input type="radio" name="rating" id="rating-3"></label>
<label for="rating-2" class="fas fa-star">
<input type="radio" name="rating" id="rating-2"></label>
<label for="rating-1" class="fas fa-star">
<input type="radio" name="rating" id="rating-1"></label>
</div>
</div>
pls for help my problems thanks before and sorry for my bad english..
Upvotes: 4
Views: 2548
Reputation: 517
What if we take the input off the label?
EDITED!
HTML:
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.10.2/css/all.css" />
<div class="star-rating">
<div class="star-input">
<input type="radio" name="rating" id="rating-5">
<label for="rating-5" class="fas fa-star">
</label>
<input type="radio" name="rating" id="rating-4">
<label for="rating-4" class="fas fa-star">
</label>
<input type="radio" name="rating" id="rating-3">
<label for="rating-3" class="fas fa-star">
</label>
<input type="radio" name="rating" id="rating-2">
<label for="rating-2" class="fas fa-star">
</label>
<input type="radio" name="rating" id="rating-1">
<label for="rating-1" class="fas fa-star">
</label>
</div>
</div>
CSS:
{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: arial;
}
.star-rating input {
display: none;
}
.star-rating {
margin: 50px auto;
display: table;
width: 350px;
}
.star-rating label {
padding: 10px;
float: right;
font-size: 44px;
color: #eee;
}
.star-rating input:not(:checked)~label:hover,
.star-rating input:not(:checked)~label:hover~label {
color: #ffc107;
}
.star-rating input[type="radio"]:checked ~label
{
color: red!important;
}
It seems now and the selection works. Without falling out in a special case with nth-child and etc ..
Upvotes: 0
Reputation: 452
This could be the solution without using JS.
.rating {
display: inline-block;
position: relative;
height: 50px;
line-height: 50px;
font-size: 25px;
}
.rating label {
position: absolute;
top: 0;
left: 0;
height: 100%;
cursor: pointer;
}
.rating label:last-child {
position: static;
}
.rating label:nth-child(1) {
z-index: 5;
}
.rating label:nth-child(2) {
z-index: 4;
}
.rating label:nth-child(3) {
z-index: 3;
}
.rating label:nth-child(4) {
z-index: 2;
}
.rating label:nth-child(5) {
z-index: 1;
}
.rating label input {
position: absolute;
top: 0;
left: 0;
opacity: 0;
}
.rating label .icon {
float: left;
color: transparent;
}
.rating label:last-child .icon {
color: #000;
}
.rating:not(:hover) label input:checked ~ .icon,
.rating:hover label:hover input ~ .icon {
color: #ffa904;
}
.rating label input:focus:not(:checked) ~ .icon:last-child {
color: #000;
text-shadow: 0 0 5px #ffa904;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.10.2/css/all.css" />
<form class="rating">
<label>
<input type="radio" name="stars" value="1" />
<span class="fa fa-star icon"></span>
</label>
<label>
<input type="radio" name="stars" value="2" />
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
</label>
<label>
<input type="radio" name="stars" value="3" />
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
</label>
<label>
<input type="radio" name="stars" value="4" />
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
</label>
<label>
<input type="radio" name="stars" value="5" />
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
<span class="fa fa-star icon"></span>
</label>
</form>
Upvotes: 4