You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

带小数的星级评分实现问题:黄色填充未完全覆盖星星

Hey there! Let’s get that partial star fill issue sorted out—no more pink background peeking through your gold stars. The problem stems from a couple of small positioning and stacking order quirks in your CSS. Let’s break down the fixes:

Core Issues in Your Original CSS

  • Using a non-integer z-index: 0.5 can cause inconsistent stacking behavior across browsers—z-index values should be whole numbers to ensure predictable layering.
  • Missing alignment properties (like top: 0 and matching height) mean your gold star layer isn’t fully covering the pink background stars vertically.

Revised CSS Solution

Here’s the adjusted code with explanations:

.star-ratings {
  float: left;
  display: block;
  unicode-bidi: bidi-override;
  color: pink;
  font-size: 40px;
  height: 50px;
  position: relative;
  padding: 0;
  overflow: hidden; /* Clip any overflow from child layers */
}

.star-ratings .star-ratings-top {
  color: gold;
  padding: 0;
  position: absolute;
  z-index: 1; /* Ensure this sits above the background stars */
  display: block;
  left: 0;
  top: 0; /* Align perfectly with the top of the container */
  overflow: hidden; /* Clip the gold stars to your desired width */
  height: 100%; /* Match parent height for full vertical coverage */
}

.star-ratings .star-ratings-bottom {
  position: relative;
  z-index: 0; /* Base layer for background stars */
  display: block;
}

Key Fixes Explained

  1. Proper Stacking Order: Using z-index: 1 for the gold layer and z-index: 0 for the pink background ensures the fill always sits on top.
  2. Full Vertical Coverage: Adding top: 0 and height: 100% to the gold layer eliminates any vertical gaps where the pink background could show through.
  3. Consistent Clipping: The parent container’s overflow: hidden acts as a safety net to make sure no star elements spill outside the rating box.

Quick HTML Check

Make sure your HTML structure pairs the two star layers correctly, with the top layer’s width set dynamically based on your decimal rating (e.g., width: 64% for 3.2 stars):

<div class="star-ratings">
  <div class="star-ratings-top" style="width: 64%">★★★★★</div>
  <div class="star-ratings-bottom">★★★★★</div>
</div>

That should fix the partial fill issue and make your gold stars fully cover the pink background wherever they’re supposed to!

内容的提问来源于stack exchange,提问作者Gabriela Boyadjiyska

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:28:48