简体   繁体   English

只使用css宽度填充60%的星

[英]Only fill 60 percent star using css width

When I am try style=width:60% to .rating class, gold color should be fill up only 3 stars and rest of stars should be blank but it does not work. 当我尝试style=width:60%.rating类时,金色应该只填充3星,其余星星应该是空白但它不起作用。 Please help me what is my mistake ? 请帮帮我,我的错误是什么?

My html code: 我的HTML代码:

<div class="star">
   <div class="rating" style="width:60%">
      <span>&#9734;</span>
      <span>&#9734;</span>
      <span>&#9734;</span>
      <span>&#9734;</span>
      <span>&#9734;</span> 
   </div>
</div>

My css code: 我的css代码:

.star{ width:200px; position: relative;color: #bdbdbd;}

.rating span
 {   
    font-size:30px;
    margin-left:-4px;
    white-space: nowrap;
    overflow: hidden;
 }

.rating  span:before { 
 content:"\2605";
 position: absolute;
 color:gold;
 }

CHECK fiddle link 检查小提琴链接

This is what I have previously used. 这就是我以前使用的。 This works quite nicely - you can even have a fraction of a star filled out... 这很好用 - 你甚至可以填充一小部分星......

https://jsfiddle.net/4qqspqxh/ https://jsfiddle.net/4qqspqxh/

 .ratings { position: relative; vertical-align: middle; display: inline-block; color: #b1b1b1; overflow: hidden; } .full-stars{ position: absolute; left: 0; top: 0; white-space: nowrap; overflow: hidden; color: #fde16d; } .empty-stars:before, .full-stars:before { content: "\\2605\\2605\\2605\\2605\\2605"; font-size: 14pt; } .empty-stars:before { -webkit-text-stroke: 1px #848484; } .full-stars:before { -webkit-text-stroke: 1px orange; } /* Webkit-text-stroke is not supported on firefox or IE */ /* Firefox */ @-moz-document url-prefix() { .full-stars{ color: #ECBE24; } } /* IE */ <!--[if IE]> .full-stars{ color: #ECBE24; } <![endif]--> 
 <div class="ratings"> <div class="empty-stars"></div> <div class="full-stars" style="width:70%"></div> </div> 

You need to swap your gray and gold starts. 你需要交换你的灰色和金色开始。 Each star is 30px, so the total size is 150px, not 200px. 每颗星都是30px,所以总大小是150px,而不是200px。 To prevent the starts from wrapping to the next line, use overflow: hidden; 要防止开始包装到下一行,请使用overflow: hidden; . Try this: 试试这个:

 .star{ width:150px; position: relative;color: #bdbdbd;} .rating { width:60%; overflow: hidden; white-space: nowrap; } .rating span{ font-size:30px; white-space: nowrap; overflow: hidden; color: gold; } .rating span:before { content:"\\2606\\2606\\2606\\2606\\2606"; position: absolute; color: #bdbdbd; z-index: -1; } 
 <div class="star"> <div class="rating"> <span>&#x2605;&#x2605;&#x2605;&#x2605;&#x2605;</span> </div> </div> 

Note: the width of each star is 30px in IE, so the total width is 150px. 注意:IE中每颗星的宽度为30px,因此总宽度为150px。 However, in Chrome the width of the star is 25px, so the total width is 125px. 但是,在Chrome中,星的宽度为25px,因此总宽度为125px。 To solve this, so your percentage works across the browsers, you will need to specify the width of each star using display: inner-block; width: 30px; 要解决这个问题,所以你的百分比在浏览器中起作用,你需要使用display: inner-block; width: 30px;来指定每个星的宽度display: inner-block; width: 30px; display: inner-block; width: 30px; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM