Home  >  Article  >  Web Front-end  >  How to change the color of a radio button using CSS?

How to change the color of a radio button using CSS?

王林
王林forward
2023-09-16 08:09:061594browse

如何使用 CSS 更改单选按钮的颜色?

Radio button is a common form element. It allows users to select a single option from a group of options. It is often used in conjunction with a group of labels, each associated with a corresponding radio button. In this article, we will look at how to change the color of radio buttons using CSS.

What is Radio Button?

A radio button is a small round button on a web page or application that allows the user to select an option from a set of options. It is also called an "option button". It is often used in forms, surveys, and quizzes where only one option can be selected at a time.

Change the color of radio buttons using CSS

Customizing the appearance of radio buttons is a common task for web developers and designers. One aspect that can be modified is the color of the radio buttons. This can be done by using the CSS attribute selector, which allows users to select elements based on their attributes. For example, to select all radio buttons, we can use the following CSS code −

input[type="radio"] {
   /* CSS styles go here */
}

After selecting the radio buttons, we use CSS to change their color. This can be achieved by using the color attribute. For example, to change the color of all radio buttons to green, you can use the following CSS code -

input[type="radio"] {
   color: green;
}

Example

This is an example of changing the color of a radio button.

<html> 
   <title>Welcome to Tutorialspoint</title>
   <head>
      <style>
         body{
            text-align:center;
         }
         input[type=radio] {
            accent-color: green;
         }
      </style>
   </head>
   <body>
      <h3>Change the color of radio buttons using CSS </h3>
      <input type="radio" id="RadioButton" name="RadioButton" value="RadioButton">
      <label for="RadioButton">Radio Button</label>
   </body>
</html>

As well, we can change the color of radio buttons by using the background-color property. This can be useful if we want to change the color of the entire button, including the button itself and any space surrounding it. For example , to change the background color of all radio buttons to blue, we can use the following CSS code −

input[type="radio"] {
   background-color: blue; 
}

Example

This is one more example to change the color of the radio button.

<html>
   <style>
      body{
         text-align:center;
      }
      input[type=radio] {
         appearance: none;
         padding: 10px;
         background-color: yellow;
         border-radius:50%;
      }
      input[type=radio]:checked {
         background-color: blue;
      }
   </style>
   <body>
      <h3>Change the color of radio buttons using CSS </h3>
      <form>
         <input type="radio" id="RadioButton" name="Button" value="Button">
         <label for="RadioButton">Radio Button</label>
      </form>
   </body>
</html>

in conclusion

CSS allows easy customization of radio button colors. Select radio buttons by using attribute selectors and make use of attributes such as color and background-color. The use of pseudo-classes also allows the color of radio buttons to be modified in specific states. With these techniques, radio buttons can be customized to provide the best user experience.

The above is the detailed content of How to change the color of a radio button using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete