使用 CSS 创建 5 星技能评分条


5 星技能评分条是任何展示评分和成就的个人作品集网站中必不可少的元素。评分条具有响应式设计,可以在各种设备上使用。在这里,我们使用单选按钮来创建评分条。

算法

  • 创建一个包含头部和主体部分的 HTML 文档。

  • 使用 CSS 设置主体背景颜色并居中内容。

  • 使用字体大小、方向和显示属性来设置评分元素的样式。

  • 隐藏单选按钮,并设置标签元素的样式以将其显示为块级元素。

  • 使用 CSS,通过 :hover、:checked 和 ~ 选择器为标签元素添加交互性。

  • 在主体部分创建一个具有 rating 类的 div 元素。

  • 添加五个具有不同值和 id 的单选输入元素。

  • 添加五个标签元素,其文本内容为星号符号,并将其 for 属性与相应的单选输入 id 匹配。

示例

<!DOCTYPE html>
<html>
<head>
  <title>5-Star Skills Rating Bar</title>
  <!-- The following CSS styles the rating bar and allows for customization -->
  <style>
    /* The body is styled to center the rating bar and give it a background color */
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: #a2f9ff;
    }
    /* The rating class is used to style the rating bar and make it responsive */
.rating {
  font-size: 0; /* Remove any font size */
  direction: rtl; /* Set direction to right-to-left for proper star display */
}

/* Hide the radio input element of the rating bar */
.rating input {
  display: none;
}

/* Style the label elements to display as stars and to allow for interactivity */
.rating label {
  display: inline-block;
  width: 20px;
  height: 20px;
  margin: 0;
  padding: 0;
  font-size: 24px;
  text-align: center;
  line-height: 20px;
  cursor: pointer;
  color: #ccc;
  transform: rotateY(180deg); /* Flip the star to display properly */
}

/* Style the label elements when hovered over or checked */
.rating label:hover,
.rating label:hover ~ label,
.rating input:checked ~ label {
  color: #f90; /* Change the color of the star to yellow when hovered over or checked */
}
</style>
</head>
<body>
  <!-- The rating class is used to create the rating bar using radio input elements and labels -->
  <div class="rating">
    <input type="radio" name="rating" id="rating-1" value="1" />
    <label for="rating-1">★</label>
    <input type="radio" name="rating" id="rating-2" value="2" />
    <label for="rating-2">★</label>
    <input type="radio" name="rating" id="rating-3" value="3" />
    <label for="rating-3">★</label>
    <input type="radio" name="rating" id="rating-4" value="4" />
    <label for="rating-4">★</label>
    <input type="radio" name="rating" id="rating-5" value="5" />
    <label for="rating-5">★</label>
  </div>
</body>
</html>

除了使用单选按钮外,开发人员还可以使用其他输入类型,如范围滑块、复选框或文本输入,以允许用户提供更详细的反馈。

对于需要不同评分范围的网站,开发人员可以修改 CSS 样式和 HTML 标记以适应不同的评分选项。JavaScript 可用于为评分条添加更多交互式功能,例如显示当前评分或在用户提交评分后显示消息。

结论

作为评分和反馈条,它们可以用于电子商务网站、移动应用程序、在线产品评论等,以提升用户体验和满意度。我们使用了图标而不是图像,这使得样式和调整大小变得容易,不像图像那样。评分条具有响应式设计,可以在各种设备上使用。

CSS 样式允许自定义评分条的外观以适应任何网站设计。

更新于:2023 年 8 月 18 日

982 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.