首页 > 其他分享 >使用纯css实现一个rate评分的功能

使用纯css实现一个rate评分的功能

时间:2024-12-02 09:22:33浏览次数:3  
标签:rating hover checked 评分 label color rate display css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Rating</title>
    <style>
        .rating {
            display: inline-block;
            unicode-bidi: bidi-override; /* fix for RTL languages */
            direction: rtl; /* put stars in reverse order */
        }
        .rating > input {
            display: none;
        }
        .rating > label {
            position: relative;
            display: inline-block;
            font-size: 2em;
            color: #ccc; /* default color */
            cursor: pointer;
        }
        .rating > label::before {
            content: "\2605"; /* full star */
            position: absolute;
            right: 0;
        }
        .rating > input:checked ~ label,
        .rating > label:hover,
        .rating > label:hover ~ label {
            color: gold; /* color when checked or hovered */
        }
        .rating > input:checked + label:hover,
        .rating > input:checked + label:hover ~ label,
        .rating > input:checked ~ label:hover,
        .rating > input:checked ~ label:hover ~ label {
            color: #e6c300; /* slightly darker gold on hover when checked */
        }
    </style>
</head>
<body>

    <div class="rating">
        <input type="radio" id="star5" name="rating" value="5" /><label for="star5" title="5 stars"></label>
        <input type="radio" id="star4" name="rating" value="4" /><label for="star4" title="4 stars"></label>
        <input type="radio" id="star3" name="rating" value="3" /><label for="star3" title="3 stars"></label>
        <input type="radio" id="star2" name="rating" value="2" /><label for="star2" title="2 stars"></label>
        <input type="radio" id="star1" name="rating" value="1" /><label for="star1" title="1 star"></label>
    </div>

</body>
</html>

Explanation:

  1. HTML Structure: We use radio inputs for each star rating and labels associated with them. The labels are what the user clicks on.

  2. CSS Styling:

    • display: inline-block allows the rating to sit inline with other elements.
    • unicode-bidi: bidi-override; and direction: rtl; are crucial. They visually reverse the order of the stars so they display correctly even though we're using the ~ (general sibling combinator) which selects following siblings.
    • display: none; hides the default radio buttons.
    • ::before creates the star character (★) using the Unicode escape sequence \2605.
    • :checked ~ label styles the labels following a checked radio button (and their siblings).
    • :hover styles the label being hovered over and its following siblings.
    • The combined :checked and :hover selectors handle the slightly darker gold color when hovering over already-selected stars.

Key improvements over simpler solutions:

  • Accessibility: The title attribute on each label provides a tooltip for screen readers, improving accessibility.
  • Correct Visual Order: The unicode-bidi and direction properties ensure the stars display in the correct order, regardless of the reading direction of the user's browser/OS.
  • Hover Effects: The CSS handles hover effects correctly, including the slightly darker color when hovering over already-selected stars. This provides better user feedback.
  • Semantic HTML: Uses radio buttons which are semantically correct for selecting a single rating.

This improved version provides a more robust and user-friendly rating system. You can easily customize the star size, colors, and number of stars by modifying the CSS. You can also use JavaScript to capture the selected rating value if needed for further processing.

标签:rating,hover,checked,评分,label,color,rate,display,css
From: https://www.cnblogs.com/ai888/p/18580919

相关文章

  • 使用css3画一个扇形
    Youcancreateapie-chart-likesector/segmentusingpureCSSinseveralways.Hereareacoupleofcommontechniquesandhowtheywork:1.Usingclip-path(Moststraightforwardforsinglesectors):Thisisgenerallythecleanestandmostrecommendedappr......
  • 使用css3绘制一个QQ小企鹅的LOGO
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>QQPenguin</title>......
  • HTML-css样式
    HTML样式-csscss样式的使用方法:内部样式:在HTML元素中使用“style”属性内部样式表:在HTML文档头部中<head>区域中使用<style>元素来包含css外部引用:使用外部css文件,使用link元素包含最好使用外部引用css文件内部样式:<pstyle="color:blue;margin-left:20px;">这是一个段落......
  • 前端css实例
    前端css实例一、带条纹的表格<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>条纹样式的表格&l......
  • [CSS] Containing Block
    IdentifyingthecontainingblockTheprocessforidentifyingthecontainingblockdependsentirelyonthevalueoftheelement's position property:Ifthe position propertyis static, relative,or sticky,thecontainingblockisformedbytheedge......
  • 从 HTML 到 CSS:开启网页样式之旅(五)—— CSS盒子模型
    从HTML到CSS:开启网页样式之旅(五)——CSS盒子模型前言一、盒子模型的组成margin(外边距):border(边框):padding(内边距):content(内容):二、盒子内容区(content)关于默认宽度三、盒子内边距(padding)四、盒子边框(border)五、盒子外边距(margin)1.margin属性设置2.margin注意事项:3.marg......
  • 【CSS】我将选择器发挥到极致
    复合选择器后代选择器(空格分隔)概念后代选择器用于选择一个元素内部的所有指定后代元素,这些后代元素可以是子元素、孙元素或者更深层次嵌套的元素。它基于元素在文档结构中的嵌套关系来选择。祖先元素后代元素,中间用空格隔开。例如,“divp”<div><p>这是一个段落......
  • Educational Codeforces Round 169 (Rated for Div2)
    EducationalCodeforcesRound169(RatedforDiv.2)-CodeforcesProblem-A-Codeforces构造签到题,明显只有\(n\leq2\)的时候有解#include<bits/stdc++.h>usingnamespacestd;constintN=2e5+10;typedefpair<int,int>pii;intn,m;inta[N];voidsolve(......
  • HTML、CSS 和 JavaScript :它们是如何构成网页的
    ......
  • 使用css3绘制一个圆形动态的时钟
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>CSSClock</title>......