Star Rating in Pure CSS
Html
<form>
<fieldset class="star-container">
<legend class="star-legend">Rate this rating</legend>
<input type="radio" name="rating" value="1" id="star-1" class="star-radio visuhide">
<input type="radio" name="rating" value="2" id="star-2" class="star-radio visuhide">
<input type="radio" name="rating" value="3" id="star-3" class="star-radio visuhide">
<input type="radio" name="rating" value="4" id="star-4" class="star-radio visuhide">
<input type="radio" name="rating" value="5" id="star-5" class="star-radio visuhide">
<label class="star-item" for="star-1"><span class="visuhide">1 star</span></label>
<label class="star-item" for="star-2"><span class="visuhide">2 stars</span></label>
<label class="star-item" for="star-3"><span class="visuhide">3 stars</span></label>
<label class="star-item" for="star-4"><span class="visuhide">4 stars</span></label>
<label class="star-item" for="star-5"><span class="visuhide">5 stars</span></label>
</fieldset>
</form>
Css
@charset "UTF-8";
html {
--ctx-bg: #1d122f;
--ctx-color: #f0f1f4;
--accent: #ffe4c2;
--highlight: #ff0196;
height: 100%;
background-color: var(--ctx-bg);
background-image: linear-gradient(-30deg, var(--ctx-bg), #560e49);
}
body {
display: flex;
flex-direction: column;
gap: 1em;
justify-content: center;
align-items: center;
min-height: 100%;
color: var(--ctx-color);
}
*,
*::before,
*::after {
box-sizing: border-box;
}
.visuhide {
position: absolute !important;
overflow: hidden;
width: 1px;
height: 1px;
clip: rect(1px, 1px, 1px, 1px);
}
.star-item .star-container:hover, .star-radio:checked ~ .star-item {
filter: grayscale(0);
}
.star-item:hover ~ .star-item, .star-item, .star-container:not(:hover) > .star-radio:nth-of-type(5):checked ~ .star-item:nth-of-type(5) ~ .star-item, .star-container:not(:hover) > .star-radio:nth-of-type(4):checked ~ .star-item:nth-of-type(4) ~ .star-item, .star-container:not(:hover) > .star-radio:nth-of-type(3):checked ~ .star-item:nth-of-type(3) ~ .star-item, .star-container:not(:hover) > .star-radio:nth-of-type(2):checked ~ .star-item:nth-of-type(2) ~ .star-item, .star-container:not(:hover) > .star-radio:nth-of-type(1):checked ~ .star-item:nth-of-type(1) ~ .star-item {
filter: grayscale(1) opacity(0.75);
}
.star-container {
position: static;
padding: 1em;
text-align: center;
font-size: 2rem;
font-variant-numeric: tabular-nums;
border: 0;
border-radius: 0.25em;
box-shadow: inset 0 1px 0 rgba(255, 228, 194, 0.25), inset 0 0.25em 1em rgba(86, 14, 73, 0.5), 0 4px 4px rgba(0, 0, 0, 0.3828125), 0 8px 8px rgba(0, 0, 0, 0.28125), 0 12px 12px rgba(0, 0, 0, 0.1953125), 0 16px 16px rgba(0, 0, 0, 0.125), 0 20px 20px rgba(0, 0, 0, 0.0703125), 0 24px 24px rgba(0, 0, 0, 0.03125), 0 28px 28px rgba(0, 0, 0, 0.0078125);
background: linear-gradient(to bottom, #2b1136, var(--ctx-bg)) var(--ctx-bg);
background-origin: border-box;
}
.star-legend {
position: static;
display: block;
margin: auto;
-webkit-padding-after: 1em;
padding-block-end: 1em;
text-align: center;
font-size: 1.25rem;
font-weight: 100;
letter-spacing: 0.125em;
line-height: 1;
text-transform: uppercase;
text-shadow: 0 1px 2px black;
translate: 0 100%;
}
.star-radio:nth-of-type(1):checked ~ .star-item:nth-of-type(1)::before {
transform: scale(1.5);
transition-timing-function: cubic-bezier(0.5, 1.5, 0.25, 1);
}
.star-radio:nth-of-type(2):checked ~ .star-item:nth-of-type(2)::before {
transform: scale(1.5);
transition-timing-function: cubic-bezier(0.5, 1.5, 0.25, 1);
}
.star-radio:nth-of-type(3):checked ~ .star-item:nth-of-type(3)::before {
transform: scale(1.5);
transition-timing-function: cubic-bezier(0.5, 1.5, 0.25, 1);
}
.star-radio:nth-of-type(4):checked ~ .star-item:nth-of-type(4)::before {
transform: scale(1.5);
transition-timing-function: cubic-bezier(0.5, 1.5, 0.25, 1);
}
.star-radio:nth-of-type(5):checked ~ .star-item:nth-of-type(5)::before {
transform: scale(1.5);
transition-timing-function: cubic-bezier(0.5, 1.5, 0.25, 1);
}
.star-item {
display: inline-flex;
width: 1.25em;
height: 1.5em;
cursor: pointer;
transition: filter 0.1s ease-out;
}
.star-item::before {
content: "💜";
display: inline-block;
margin: auto;
font-size: 0.75em;
vertical-align: top;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transform-origin: 50% 33.3%;
transition: transform 0.3s ease-out;
will-change: transform;
}
Js
ثبت نظر خود
برای ثبت نظر باید ابتدا وارد حساب کاربری خود شوید.
ورود یا ثبت نامنظری ثبت نشده است