Skip to content
Advertisement

How to show colour change when customised radio button is selected?

I am new to coding generally, and have been working on a quiz program on HTML and CSS (data is from PostgreSQL, framework SpringBoot on Eclipse. PHP and JQuery not included in syllabus).

Here’s my problem:

  1. Now I have a list of answers where the user will have to select from.
  2. Was hoping to have the colours of the button-like radio input? change colour when the user clicks on it.
  3. Managed to create the buttons and the cursor when it hovers over the selections, but there’s no change despite my CSS.

Can someone tell me where I did wrong? Big thanks in advance.

※Updated HTML and CSS according to the advices in the comments + more code:

This is the HTML code:

the screenshot of the id: https://i.stack.imgur.com/4IeWK.png

JavaScript

CSS:

JavaScript

Advertisement

Answer

UPDATE: Added live test at the end

Unlike class, each id property must be unique in the whole document.

More about id

Matching id and for also need to be assigned for each pair of input and label to make the styles work.

The input[type="radio"]:checked + label selector is not targeting the label properly.

This is because it uses adjacent sibling selector +, but input and label are not adjacent due to a <br> in between.

More about CSS combinators

To solve this, you could use a general sibling combinator ~ to target the label:

JavaScript

OR remove the <br> in between the input and label, and keep the original CSS:

JavaScript

Both ways should make label change color when :checked.

Hope this will help!

LIVE TEST (run with button below)

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement