首頁  >  文章  >  web前端  >  使用CSS建立一個五星級技能評分欄

使用CSS建立一個五星級技能評分欄

WBOY
WBOY轉載
2023-08-25 14:17:141029瀏覽

使用CSS建立一個五星級技能評分欄

5 星技能評分欄是任何作品集網站展示評分和成就的基本要素。評級欄響應靈敏,可在各種設備上使用。在這裡,我們使用單選按鈕來建立評級欄。

演算法

  • 建立一個包含頭部和正文部分的 HTML 文件。

  • 使用 CSS 設定背景顏色並將正文內容置中。

  • 使用字體大小、方向和顯示屬性來設定評級元素的樣式。

  • 隱藏單選按鈕並設定標籤元素的樣式以將其顯示為區塊。

  • 使用 CSS 透過 :hover、:checked 和 ~ 選擇器為標籤元素添加互動性。

  • 在正文部分建立一個帶有評級類別的 div 元素。

  • 新增五個具有不同值和 ID 的無線輸入元素。

  • 新增五個標籤元素,其中包含星號的文字內容以及與對應的單選輸入 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 樣式允許自訂評級欄的外觀以適應任何網站設計。

以上是使用CSS建立一個五星級技能評分欄的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除