css - 半星未正确定位
问题描述
我在我的网站上实施了星级评分系统,但是,我遇到了一个问题:半星在移动设备(iPhone 11)上的位置不正确。它们看起来像这样:
CSS 代码:
.star {
font-size: x-large;
width: 20px;
display: inline-block;
color: gray;
}
.star:last-child {
margin-right: 0;
}
.star:before {
content: '\2605';
}
.star.on {
color: orange;
}
.star.half:after {
content: '\2605';
color: orange;
position: absolute;
margin-left: -20px;
width: 10px;
overflow: hidden;
}
这是我的 HTML 代码:
<div class="stars">
@for (int i = 0; i < StarRatingInt; i++)
{
<span class="star on"></span>
}
@if (Model.StarRating > StarRatingInt)
{
<span class="star half"></span>
}
@for (int i = 0; i < 5 - Math.Round(Model.StarRating, 0, MidpointRounding.AwayFromZero); i++)
{
<span class="star"></span>
}
</div>
我花了几个小时来解决这个问题,但无济于事。我搜索了其他星级评分系统,但没有一个看起来像我想要的那样。
解决方案
通过.star.half:after
,您已将半星放在absolute
位置上。所以需要在position:relative
父类中添加css属性,.star
把半星放在星项里面。
.star {
font-size: x-large;
width: 20px;
display: inline-block;
color: gray;
/* This part is required */
position: relative;
}
推荐阅读
- angular - 如何使用引导程序使 ngFor 水平?
- c# - 在自己的 .NET Core API 中使用来自另一个 API 的 JSON
- python - 在numpy中查找每一行的点积
- php - 基于用户角色的 Woocommerce 最低订单总数
- java - 如何使用 Selenium 和 Java 识别是否选择了带有伪元素 ::after 样式的单选按钮选项
- postgresql - Datagrip:创建postgres索引而不等待执行
- javascript - Bcrypt.compareSync 总是返回 false
- c# - ASP.NET Core MVC + Entity Framework Core 如何获取新增对象的id
- python - 使用字符串转换连接两个整数时出错
- flutter - Flutter StatefulWidget 不会更新