How to Change Selected Radio Button Background Color Using CSS?

How to Change Selected Radio Button Background Color Using CSS?

  • CSS
  • 4 mins read

To change the background color of a selected radio button, you can use the CSS :checked pseudo-class and the background-color property. Here is an example:

input[type="radio"]:checked {
  background-color: #333;
}

This will change the background color of the selected radio button to a dark gray color. You can change the color to any other color by using the appropriate color code or name.

Here is an example of how you can use this CSS code to style a group of radio buttons:

<form>
  <input type="radio" id="red" name="color" value="red">
  <label for="red">Red</label>

  <input type="radio" id="green" name="color" value="green">
  <label for="green">Green</label>

  <input type="radio" id="blue" name="color" value="blue">
  <label for="blue">Blue</label>
</form>
input[type="radio"] {
  /* Style for unselected radio buttons */
}

input[type="radio"]:checked {
  /* Style for selected radio button */
  background-color: #333;
}

This will apply the selected radio button style to the selected radio button in the group, while the other buttons will have the default unselected style.

Here are some more examples of using the :checked pseudo-class to style selected radio buttons:

/* Use a custom font color for selected radio buttons */
input[type="radio"]:checked {
  color: #00f;
}

/* Add a border around selected radio buttons */
input[type="radio"]:checked {
  border: 1px solid #333;
}

/* Change the size of selected radio buttons */
input[type="radio"]:checked {
  width: 20px;
  height: 20px;
}

You can also use the :checked pseudo-class in combination with other CSS selectors to target specific groups of radio buttons. For example:

/* Style only radio buttons with the "fruits" class */
.fruits input[type="radio"]:checked {
  background-color: #333;
}

/* Style only radio buttons inside a "vegetables" element */
#vegetables input[type="radio"]:checked {
  background-color: #333;
}

These examples demonstrate some of the ways that you can use the :checked pseudo-class and the background-color property to style selected radio buttons with CSS. You can use these techniques to create more sophisticated and user-friendly forms and interfaces in your web applications.

To use an accent color for selected radio buttons, you can use the :checked pseudo-class and the background-color property, as well as the accent-color variable. Here is an example:

:root {
  --accent-color: #333;
}

input[type="radio"]:checked {
  background-color: var(--accent-color);
}

This will set the background color of selected radio buttons to the accent color defined in the :root pseudo-class. The accent-color variable can be changed to any other color to update the background color of selected radio buttons.

Here is an example of how you can use this CSS code to style a group of radio buttons:

<form>
  <input type="radio" id="red" name="color" value="red">
  <label for="red">Red</label>

  <input type="radio" id="green" name="color" value="green">
  <label for="green">Green</label>

  <input type="radio" id="blue" name="color" value="blue">
  <label for="blue">Blue</label>
</form>
:root {
  --accent-color: #333;
}

input[type="radio"] {
  /* Style for unselected radio buttons */
}

input[type="radio"]:checked {
  /* Style for selected radio button */
  background-color: var(--accent-color);
}

This will apply the selected radio button style to the selected radio button in the group, using the accent color defined in the :root pseudo-class. The other buttons will have the default unselected style.

You can also use the accent-color variable in combination with other CSS selectors to target specific groups of radio buttons. For example:

:root {
  --accent-color: #333;
}

/* Style only radio buttons with the "fruits" class */
.fruits input[type="radio"]:checked {
  background-color: var(--accent-color);
}

/* Style only radio buttons inside a "vegetables" element */
#vegetables input[type="radio"]:checked {
  background-color: var(--accent-color);
}

These examples demonstrate how you can use the :checked pseudo-class, the background-color property, and the accent-color variable to style selected radio buttons with an accent color in CSS. You can use these techniques to create more customizable and consistent designs in your web applications.

Related: