html - 半星和体透星

标签 html css svg css-shapes

我需要在 css 中创建评级星,到目前为止我有两个要求,

  1. 半色星星。
  2. 有边框但 Body 透明的星星。

到目前为止,我已经能够使用 CSS 创建一个全彩色的星星。但是使用这种结构我无法实现上述要求。

这就是我现在拥有的,我跟着这个ANSWER供我引用

.star {
  position: relative;
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: .9em;
  margin-right: .9em;
  margin-bottom: 1.2em;
  border-right: .3em solid transparent;
  border-bottom: .7em solid #FC0;
  border-left: .3em solid transparent;
  /* Controlls the size of the stars. */
  font-size: 24px;
}

.star:before,
.star:after {
  content: '';
  display: block;
  width: 0;
  height: 0;
  position: absolute;
  top: .6em;
  left: -1em;
  border-right: 1em solid transparent;
  border-bottom: .7em solid #FC0;
  border-left: 1em solid transparent;
  transform: rotate(-35deg);
}

.star:after {
  transform: rotate(35deg);
}
<p>
  <i class="star"></i>
  <i class="star half"></i>
  <i class="star transparent"></i>
</p>

最佳答案

根据您的要求,我会使用 SVG。我会这样做:

.star{border:1px solid #d9d9d9; width:30px;}
.star{fill:gold; stroke:orange; stroke-width:5px;}
.star.full use:nth-child(2){display:none;}
.star.half use:nth-child(1),
.star.empty use:nth-child(2)
{display:none;}
.star.empty{fill:none;}
<svg viewBox="0 0 95.1 90.45" style="width:0; height:0; display:none">
<defs>  
<polygon id="half_star" points="47.6, 75 18.21, 90.45 23.82, 57.72 0.047, 34.55 32.9, 29.77 47.6, 0"></polygon>
<polygon id="star" points="47.6, 0 62.29, 29.77 95.15, 34.55 71.38, 57.73 76.99, 90.458 47.6, 75 18.21, 90.45 23.82, 57.73 0.05, 34.55 32.9, 29.77"></polygon>
</defs> 
</svg>

<svg viewBox="0 0 95.1 90.45" class="star full">
  <use xlink:href="#star" />
  <use xlink:href="#half_star" />
</svg>
<svg viewBox="0 0 95.1 90.45" class="star half">
  <use xlink:href="#star" />
  <use xlink:href="#half_star" />
</svg>
<svg viewBox="0 0 95.1 90.45" class="star empty">
  <use xlink:href="#star" />
  <use xlink:href="#half_star" />
</svg>

关于html - 半星和体透星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53442146/

相关文章:

Flutter - 更改 SVG 图标颜色

css - 在 Mobile Safari 中旋转期间出现断断续续的 SVG 渲染

jquery - 如何将图像锁定在 Bootstrap 容器内的特定高度

html - 如何让 "fit-content"跨浏览器工作?

css - 管理响应式布局中的文本列

html - 如何使用媒体查询定位平板电脑而不是 IE9

javascript - 了解如何更改 SVG

html - Safari 中的过渡有问题

javascript - Google map 中带有链接的按钮

javascript - 直接从 codepen 复制的 donut 代码不起作用