3

I'm generating a rating system using font-awesome fa-heart class. It is working fine by colouring the full heart, but I'm in trouble when trying to fill it in red only the first half of the heart. I have been searching but always are stars and not a half-heart, always full heart and not from font-awesome.

Font awesome version used 4.0.3 and cannot be changed...

Any trick to do that?

.full {
  color:red;
}
.half {

}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css">

<div class="rating-love ">
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart half"></span>  
     <a href="#reviews"><span>23 Review(s)</span></a>
                    </div>   
Albeis
  • 1,544
  • 2
  • 17
  • 30
  • 2
    FA is an icon font, so each icon is a single glyph. As far as I'm aware there's no way in HTML/CSS/JS to have a single glyph have different colours. You could do something like use a paler colour for a "partial" heart. – jonrsharpe Aug 07 '18 at 15:36
  • Not dupe, but related: https://stackoverflow.com/questions/23569441/is-it-possible-to-apply-css-to-half-of-a-character – James Douglas Aug 07 '18 at 16:45

3 Answers3

6

Here is another idea where you can easily have any kind of rating by simply adjusting a CSS variable:

.rating-love {
  display: inline-grid!important;
}

.rating-love:before,
.rating-love:after {
  content: "\f004  \f004  \f004  \f004  \f004";
  grid-area: 1/1;
}

.rating-love:after {
  white-space: nowrap;
  overflow: hidden;
  width: var(--w);
  color: red;
}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css">

<div class="rating-love fa" style="--w: 50%;"></div>

<br>

<div class="rating-love fa" style="--w: 30%;"></div>

<br>

<div class="rating-love fa" style="--w: 80%;"></div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
4

This can be done using text Clip & Background Gradient, I hope this trick will work for you too. I just made a gradient with 2 colors and divided them with clip its looking fine. You can make half of the heart white if you want just by changing the hexcode in css.

.fa-heart.half:before {
    background: -webkit-linear-gradient(180deg,#000 0%, #000 50%,#FF0000 50%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.full {
  color:red;
}
.half {

}
.fa-heart.half:before {
    background: -webkit-linear-gradient(180deg,#000 0%, #000 50%,#FF0000 50%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css">

<div class="rating-love ">
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart full"></span>
     <span class="fa fa-heart half"></span>  
     <a href="#reviews"><span>23 Review(s)</span></a>
                    </div>   
Robin
  • 81
  • 6
3

Without using JavaScript you'd cut the width of the span to half, hiding the overflow.

.full {
  color: red;
  vertical-align:text-bottom;
}

.half {
  color: red;
  width: 8px;
  overflow: hidden;
  vertical-align:text-bottom;
  margin-right: 8px;
}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css">

<div class="rating-love ">
  <span class="fa fa-heart full"></span>
  <span class="fa fa-heart full"></span>
  <span class="fa fa-heart full"></span>
  <span class="fa fa-heart full"></span>
  <span class="fa fa-heart half"></span>
  <a href="#reviews"><span>23 Review(s)</span></a>
</div>
James Douglas
  • 3,328
  • 2
  • 22
  • 43
j08691
  • 204,283
  • 31
  • 260
  • 272
  • This works, but there is some margin above the last heart which ruins the effect... – James Douglas Aug 07 '18 at 16:05
  • @JamesDouglas Incorrect, the last heart span has no margin other than the right one which I gave it. So, nothing ruined. – j08691 Aug 07 '18 at 16:13
  • I see margin on my computer. I am using Firefox on OSX. [Here is a screenshot of what I see.](https://s8.postimg.cc/vvaz6kgp1/Screen_Shot_2018-08-07_at_17.18.09.png) – James Douglas Aug 07 '18 at 16:19
  • @JamesDouglas Looks like the vertical align instead. – j08691 Aug 07 '18 at 16:22
  • It can be solved by adding `vertical-align:text-bottom;` to the `.full` spans as well; I have taken the liberty to edit your answer to include it. – James Douglas Aug 07 '18 at 16:28