Using CSS to design star ratings (with each radio button and label contained within a div)

Looking to create a custom star rating system using CSS. I found inspiration from this link: https://codepen.io/andreacrawford/pen/NvqJXW

However, my DOM structure is different.

input {
  display: none;
}

.radio-div label {
  visibility: hidden;
}

.wrapper {
  display: flex;
  align-items: center;
}

.radio-div>label:before {
  content: '★ ';
  visibility: visible;
}

.radio-div>input:checked~label {
  color: green;
}
<div class="wrapper">
  <div class="radio-div">
    <input type="radio" name="rating" id="star_1">
    <label for="star_1">1</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_2">
    <label for="star_2">2</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_3">
    <label for="star_3">3</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_4">
    <label for="star_4">4</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_5">
    <label for="star_5">5</label>
  </div>
</div>

Seeking assistance in implementing star rating functionality within this specific DOM structure. Any help would be appreciated.

Answer №1

Have you heard about the new :has() CSS pseudo-class? It allows you to select elements that contain child elements matching another selector. This is particularly useful for targeting specific .radio-div elements that contain a checked radio input. To see if your browser supports this feature, check out this link: https://caniuse.com/css-has

In addition, there is code for handling colors in the snippet:

  • All stars are initially active (green).
  • If no radio input is selected, all stars become inactive (black).
  • Stars after the selected or hovered one also become inactive.

input {
  display: none;
}

.wrapper {
  display: flex;
  align-items: center;
}

.radio-div>label {
  cursor: pointer;
  font-size: 0;
}

.radio-div>label:before {
  content: '★';
  font-size: 16px;
  padding-right: 14px;
}

.radio-div>label {
  color: green;
}

.wrapper:not(:has(input:checked)) .radio-div>label {
  color: black;
}

.radio-div:has(input:checked)~.radio-div>label {
  color: black;
}

.wrapper:has(label:hover)>div.radio-div>label {
  color: green;
}

.wrapper:hover>div.radio-div:hover~.radio-div>label {
  color: black;
}
<div class="wrapper">
  <div class="radio-div">
    <input type="radio" name="rating" id="star_1">
    <label for="star_1">1</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_2">
    <label for="star_2">2</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_3">
    <label for="star_3">3</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_4">
    <label for="star_4">4</label>
  </div>
  <div class="radio-div">
    <input type="radio" name="rating" id="star_5">
    <label for="star_5">5</label>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to showcase an image that goes beyond the top and bottom boundaries of a div using CSS

Attempting to extend an image beyond the top and bottom of a div. Successful in extending it below, but how can I achieve this effect above? Check out the online demo here. .about { background-image: linear-gradient(100deg, #483dec, #4074eb); } .abo ...

Implementing CSS transitions across several elements simultaneously can enhance the user experience

There seems to be an issue with animating two elements simultaneously using max-height. The transition always occurs one after the other, with one element completing its transition before the other starts. setTimeout(function () { $(".b").css( ...

Making the most of Material Design icons in MaterializeCSS: A step-by-step guide

I am looking to customize my mdi icons by adding my own set of icons into custom classes. The existing mdi classes do not match my requirements. Is there a way for me to add these icons and use them just like the default ones? <i class="mdi-image-face ...

The PHP code encountered a parsing error due to an unexpected end of file

I encountered an issue: Parse error: syntax error, unexpected end of file in the line This is the code that triggered the error: <html> <?php function login() { // Code for login function } if (lo ...

Creating a calendar view in React and rendering the elements of an array in the following manner: How can it be accomplished?

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 This data is crucial for my calendar project as I need to switch lines every week cycle. ...

Unexpected problem arises when using OpenCart with the select element

Currently, I am facing an issue with a plugin that I am working on for OpenCart. The problem arises when I attempt to use the selected function in a select dropdown menu to ensure that a specific option is chosen. Instead of selecting the desired option, ...

Organizing CSS elements for input into a database

SCENARIO: In my current project, I am working on a CMS website where each user's custom CSS is stored in a database table with specific fields: int id (primary key) int site_id (index) string selector string property strin ...

Using Material-UI with ReactJS: Implementing color alternation in Material-UI <Table/>'s <TableRow/>

I am currently utilizing Material-UI's <Table/> component and I would like to alternate row colors between blue and purple. The first row will be blue, the second row will be purple, and so on for every additional row added. How can I dynamical ...

Accessing a JavaScript function from a file stored on the SD card using Android Studio

I'm currently developing an Android application that writes an HTML file to the SD card and then loads it in a web view. The app is able to load the HTML file, but the JavaScript functions within the file are not functioning correctly. Here is the cod ...

What is the best way to fill a single row with a solid color and no border?

It's table time! <table class="custom-table"> <tr> <td>231232131</td> <td>INTERNATIONAL FEDERATION O</td> </tr> </table> I'm aiming for a row with some color, li ...

Combining two XPaths into a single XPath can be achieved by using various

HTML <li class="wdappchrome-ai" data-automation-id="searchInputAutoCompleteResult" tabindex="-2" aria-posinset="1" aria-setsize="3"> <span data-automation-id="searchInputAutoCompleteResultFullText"> <span style="font-weight:500"> ...

Overlay jQuery Accordion

I am currently working on an accordion to display old blog posts. However, when using a tab, the section below either moves or jumps around. I want the tab to lay over the rows below it without affecting their position. I have tried various solutions in ...

Having trouble assigning an active class to the selected button in Vuetify's v-btn-toggle component

In my project, I have implemented a toggle feature using Vuetify's v-button-toggle with three buttons. Each button is supposed to have a different active class that changes when clicked. However, currently the respective active class is not being set ...

Challenge with CSS Box Shadow

Hey there, I've been attempting to add a shadow effect to a box inside another box but I'm struggling to achieve the desired outcome. <div class="box effect2"> <div class="box effect2"> Box inside a box <div> &l ...

When the Add button in AngularJS is clicked, a new popup page should appear by concealing the parent page

I am currently working on a project that involves using the AngularJS/Breeze framework within the HotTowel Template. One of the requirements I have is to include a button/link labeled "Add" on the parent.html page. When this button is clicked, it should t ...

Tips for utilizing Vue.js scoped styles with components that are loaded through view-router

I want to customize the styling of Vue.js components loaded through <view-router> using scoped styles. This is the code I have: <template> <div id="admin"> <router-view></router-view> </div> </template> ...

Issue with div element not stretching to 100% width

I am currently working on a fluid layout design where the template includes a header, menu, and body section. <div class="w100 h100"> <div id="headerBox" style="height: 10%;" class="w100"> <div style="width: 80%;" class="lfloat ...

Pick the item when the checkbox is selected

I am currently attempting to toggle the visibility of a select element based on whether a checkbox is checked or not, but it doesn't seem to be working as expected. My desired functionality is for the select element to be hidden upon page load and th ...

What is the best way to wrap text and move the lines up without affecting the bottom margin?

When the text length increases in the first image, it shifts upwards similar to what you see in this image. How can I achieve this effect? The use of "word-wrap" causes the line to break and move to the next line, which is not what I want. In the code sn ...

What is the best way to ensure that images appear in a square format?

Lately, I've been experimenting with the alpha version of bootstrap 4 for a project. No matter how hard I try, I can't seem to get uploaded images of different sizes to display as squares when displayed. Do you remember how Instagram used to onl ...