Skip to content

Instantly share code, notes, and snippets.

@fbender
Created October 17, 2024 19:14
Show Gist options
  • Save fbender/706d9c2aa0733da3ca80aee532f4f7e9 to your computer and use it in GitHub Desktop.
Save fbender/706d9c2aa0733da3ca80aee532f4f7e9 to your computer and use it in GitHub Desktop.
Untitled
<form>
<fieldset class="rating">
<legend>Use number keys or click buttons to rate from 0 to 8, or select "Unclear" (number 9)</legend>
<div class="rating-group">
<!-- 0-8 Rating Buttons -->
<input type="radio" id="rate-0" name="rating" value="0">
<label for="rate-0" class="grouped">0</label>
<input type="radio" id="rate-1" name="rating" value="1">
<label for="rate-1" class="grouped">1</label>
<input type="radio" id="rate-2" name="rating" value="2">
<label for="rate-2" class="grouped">2</label>
<input type="radio" id="rate-3" name="rating" value="3">
<label for="rate-3" class="grouped">3</label>
<input type="radio" id="rate-4" name="rating" value="4">
<label for="rate-4" class="grouped">4</label>
<input type="radio" id="rate-5" name="rating" value="5">
<label for="rate-5" class="grouped">5</label>
<input type="radio" id="rate-6" name="rating" value="6">
<label for="rate-6" class="grouped">6</label>
<input type="radio" id="rate-7" name="rating" value="7">
<label for="rate-7" class="grouped">7</label>
<input type="radio" id="rate-8" name="rating" value="8">
<label for="rate-8" class="grouped">8</label>
</div>
<!-- "Unclear" Button -->
<input type="radio" id="rate-unclear" name="rating" value="9">
<label for="rate-unclear" class="unclear">Unclear</label>
</fieldset>
</form>
:root {
font-family: Arial, sans-serif;
--form-border-radius: 0.5em;
--form-border-size: 1px;
--form-border-color: #2e2e2e;
--button-bg-color: #3a3a3a;
--button-bg-color-selected: #007bff;
--button-bg-color-variant: #483d8b;
--button-bg-color-variant-selected: #2e8b57;
--button-focus-size: 0.1875em;
--button-focus-color: #ff00ff;
--button-focus-color-variant: #ffa07a;
--button-group-spacing: calc(var(--form-border-size) * -1);
--button-margin: 0.625em;
--button-v-padding: 0.75em;
--button-h-padding: calc(var(--button-v-padding) * 1.875);
}
fieldset {
padding: 0.35em 0.625em 0.625em 0.625em; /**/
}
.rating {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center; /* Align items vertically centered */
width: auto; /* Automatically fit the content */
min-width: 14.25em;
max-width: max-content;
border: var(--form-border-size) solid var(--form-border-color);
border-radius: var(--form-border-radius);
}
.rating input[type="radio"] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
.rating label {
padding: var(--button-v-padding) var(--button-h-padding);
background-color: var(--button-bg-color);
background-image: linear-gradient(
to bottom,
rgba(255, 255, 255, 0.1),
rgba(0, 0, 0, 0.05)
);
color: white;
cursor: pointer;
border: var(--form-border-size) solid var(--form-border-color);
transition: background-color 0.3s ease-in, box-shadow 0.3s ease-in; /* out transition */
outline-offset: var(--form-border-size);
border-radius: var(--form-border-radius);
margin: var(--button-margin);
}
.rating input[type="radio"]:checked + label {
--button-bg-color: var(--button-bg-color-selected);
}
.rating input[type="radio"]:focus + label {
outline: 2px solid #ff00ff; /* Focus outline */
z-index: 9999;
}
.rating input[type="radio"]:active + label, .rating input[type="radio"]:hover + label /*, .rating input[type="radio"]:checked + label + input + label,
.unclear*/ {
box-shadow: 0 0 3px 3px var(--button-focus-color) inset;
/* in transition */
transition-timing-function: ease-out;
transition-duration: 100ms;
z-index: 9998;
}
.rating-group {
display: contents;
}
/* Grouped buttons (0-8) styling */
.rating-group label:not(:first-of-type) {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
margin-left: var(
--button-group-spacing
); /* Ensures adjacent buttons appear connected */
}
.rating-group label:not(:last-of-type) {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
margin-right: var(
--button-group-spacing
); /* Ensures adjacent buttons appear connected */
}
.rating-group label:first-of-type {
/*border-radius: var(--border-radius) 0 0 var(--border-radius); /* Left-rounded corner for first button */
}
.rating-group label:last-of-type {
/*border-radius: 0 var(--border-radius) var(--border-radius) 0; /* Right-rounded corner for last button in group */
}
/* "Unclear" button styling */
.rating .unclear {
--button-bg-color: var(--button-bg-color-variant);
--button-focus-color: var(--button-focus-color-variant);
/*margin-left: var(--button-margin); /* Creates the visual gap */
}
.rating input[type="radio"]:checked + .unclear {
--button-bg-color: var(--button-bg-color-variant-selected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment