Reputation: 468
I had upgraded my rating plugin with font awesome icons instead of images. I am facing problem on half star rated which I can't replace white color on the fa-star-half-o icon. I would like to replace the white color with grey color similar to expected result below.
Please help
Code :
<div class="ratingbox"><i class="fa fa-star"></i><i class="fa fa-star">
</i><i class="fa fa-star-half-o"></i><i class="fa fa-star unrated"></i>
<i class="fa fa-star enlang unrated"></i></div>
i
{
float:left
}
.ratingbox
{
display: block;
clear: both;
}
.fa-star-half-o,.fa-star
{
color: #ffab00;
}
.rated{
color: #ffab00;
cursor: pointer;
}
.unrated{
color: #757575;
cursor: pointer;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="ratingbox">
<i class="fa fa-star"></i><i class="fa fa-star"></i>
<i class="fa fa-star-half-o"></i><i class="fa fa-star unrated"></i><i class="fa fa-star enlang unrated"></i>
</div>
Current Result
Expected Result
Upvotes: 4
Views: 5311
Reputation: 43870
You can use CSS ::before
and ::after
pseudo-elements refer here for CSS unicodes.
.unrated
starhalf-o
; ::before
and ::after
half-o
is relative.::before
pseudo-element to color:grey
.i {
float: left;
}
.ratingbox {
display: block;
clear: both;
}
.fa-star-half-o,
.fa-star {
color: #ffab00;
}
.rated {
color: #ffab00;
cursor: pointer;
}
.unrated {
color: #757575;
cursor: pointer;
}
.fa-star-half-o {
position: relative;
}
.fa-star-half-o:after {
font-family: FontAwesome;
content: '\f089';
position: absolute;
z-index: 1;
}
.fa-star-half-o:before {
font-family: FontAwesome;
content: '\f089';
position: absolute;
color: grey;
z-index: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="ratingbox">
<i class="fa fa-star"></i><i class="fa fa-star"></i>
<i class="fa fa-star-half-o"></i><i class="fa fa-star unrated"></i><i class="fa fa-star unrated"></i><i class="fa fa-star unrated"></i>
</div>
Upvotes: 2
Reputation: 16575
Everything is possible in web design but maybe you manage to use some trick:
i
{
float:left;
position: relative;
}
.ratingbox
{
display: block;
clear: both;
}
.fa-star-half-o,.fa-star
{
color: #ffab00;
}
.rated{
color: #ffab00;
cursor: pointer;
}
.unrated{
color: #757575;
cursor: pointer;
}
.fa-star-half-empty:after, .fa-star-half-full:after, .fa-star-half-o:after {
content: "\f123";
transform: rotateY(-180deg);
display: inline-block;
left: 6px;
position: absolute;
top: 0;
color: #757575;
overflow: hidden;
width: 8px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="ratingbox">
<i class="fa fa-star"></i><i class="fa fa-star"></i>
<i class="fa fa-star-half-o"></i><i class="fa fa-star unrated"></i><i class="fa fa-star enlang unrated"></i>
</div>
Note: I just add fa-star-half-o
character to after
element, i mean this content: "\f123";
then reverse it transform: rotateY(-180deg);
and half width and absolute position you can see what's happened here:
.fa-star-half-empty:after, .fa-star-half-full:after, .fa-star-half-o:after {
content: "\f123";
transform: rotateY(-180deg);
display: inline-block;
left: 6px;
position: absolute;
top: 0;
color: #757575;
overflow: hidden;
width: 8px;
}
It's not clean but better than nothing!
Upvotes: 3