How to fill a percentage of a glyphicon [Bootstrap 3]
css, glyphicons, html, twitter-bootstrap
Solution
You could overlap 2 absolute positioned divs. Each would contain all 5 stars. Then the top div could have an overflow:hidden div in it, where you set the width equal to a percentage (based on the rating).
Here is a codepen that I put together that illustrates it: http://codepen.io/anon/pen/ogBWwX The code snippets are copied below for reference and includes a little extra than what is needed, for the purpose of the demo.
HTML:
<div class="container">
<div class="flt_left">
<form id="star_form">
<label for="star_input">Stars:</label>
<input type="text" id="star_input" />
<button id="setStars">Set Rating</button>
</form>
</div>
<div class="flt_left">
<div id="star_box">
<div class="star_limiter">
<div class="longbar">
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
</div>
</div>
</div>
<div class="star_bg">
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
</div>
</div>
<div style="clear:both;"></div>
<p id="rating_data"></p>
</div>
CSS:
.container{
padding: 15px;
margin: 10px;
}
p{
margin: 10px 0;
}
.flt_left{
float: left;
margin-right: 10px;
position: relative;
/*min-width: 250px;*/
}
#star_box,
.star_bg{
color: #BBD41C;
position: absolute;
margin: 4px 0;
top: 0;
left: 0;
display: table;
}
.glyphicon{
display: table-cell;
padding: 0 0 0 2px;
font-size: 18px;
}
#star_box{
z-index: 2;
}
.star_bg{
color: #555;
}
#star_box .star_limiter{
width:100%;
overflow: hidden;
position: relative;
}
Javascript:
function setWidth(perc){
$('#star_box .star_limiter').css('width',perc+'%');
}
function starToPercent(amt,outof){
if(typeof outof == 'undefined') outof = 5;
var percent = Math.round(amt*100/outof);
if(percent > 100) percent = 100;
$('#rating_data').text(amt+' of '+outof+' stars, or '+percent+'%');
setWidth(percent);
}
function setRating(){
var input = parseFloat($('#star_input').val());
var num_stars = $('#star_box .star_limiter .glyphicon-star').length;
if(!isNaN(input)) starToPercent(input, num_stars);
}
$(document).ready(function(){
var star_width = $('#star_box').width();
var star_height = $('#star_box').height();
$('#star_box').css('width',star_width+'px');
$('#star_box .star_limiter').css('height',star_height+'px');
$('#star_box .longbar').css('width',star_width+'px');
$('#setStars').click(setRating);
$('#star_form').submit(function(e){
e.preventDefault();
setRating();
});
});
Notes: The overlapped stars don't look great, since you see a bit of the color of the background stars around the edges. So, it looks better if you get rid of the background stars and just use a solid color. It also is less noticeable the more you match the color.
I set the CSS of the glyphicons to display:table-cell to prevent them from wrapping and to position them closer together. More space between the stars creates less accurate results on floats.
Problem
Hi there i have 5 star rating system and i would like to make it so that i can add say 4.3 stars rather than just 4. How would i do this? My current code is listed below: ``` <center> <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span> <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span> <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span> <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span> <span class="glyphicon glyphicon-star"></span> </center> ```