How to change the style of radio buttons with CSS
How to change the style of radio buttons with CSS
Radio buttons are common form elements in web development, but the default styles may not meet your design needs. This article will explain how to use CSS to change the style of radio buttons, including color, size, and shape.
1. Changing the Color of Radio Buttons
Radio buttons can easily be changed using CSS. Use the background-color
property to set the background color, and the border-color
property to set the border color.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Color</title>
<style>
input[type="radio"] {
background-color: #ff0000;
border-color: #00ff00;
}
</style>
</head>
<body>
<input type="radio" name="color" value="red"> Red
<input type="radio" name="color" value="blue"> Blue
</body>
</html>
Code Running Results:
2. Changing the Size of Radio Buttons
Radio buttons can be resized using CSS. Use the width
and height
properties to set their width and height.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Size</title>
<style>
input[type="radio"] {
width: 20px;
height: 20px;
}
</style>
</head>
<body>
<input type="radio" name="size" value="small"> Small
<input type="radio" name="size" value="large"> Large
</body>
</html>
Code Running Results:
3. Changing the Shape of Radio Buttons
You can change the shape of radio buttons using CSS. You can use the border-radius
property to set rounded corners.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Shape</title>
<style>
input[type="radio"] {
border-radius: 50%;
}
</style>
</head>
<body>
<input type="radio" name="shape" value="circle"> Circle
<input type="radio" name="shape" value="square"> Square
</body>
</html>
Code Running Results:
4. Changing the Style of Radio Buttons
Radio buttons can be styled using CSS. You can use the appearance
property to set their appearance.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Style</title>
<style>
input[type="radio"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: 2px solid #000;
border-radius: 50%;
width: 20px;
height: 20px;
}
input[type="radio"]:checked {
background-color: #000;
}
</style>
</head>
<body>
<input type="radio" name="style" value="custom"> Custom
<input type="radio" name="style" value="default"> Default
</body>
</html>
Code Running Results:
5. Changing the Radio Button Icon
You can change the icon of a radio button using CSS. You can use the background-image
property to set a background image.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Icon</title>
<style>
input[type="radio"] {
width: 20px;
height: 20px;
background-image: url('radio-icon.png');
background-size: cover;
}
</style>
</head>
<body>
<input type="radio" name="icon" value="star"> Star
<input type="radio" name="icon" value="heart"> Heart
</body>
</html>
Code Running Result:
6. Changing the Animation Effect of Radio Buttons
You can use CSS to animate radio buttons. You can use the @keyframes
rule to define the animation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Animation</title>
<style>
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
input[type="radio"] {
animation: pulse 2s infinite;
}
</style>
</head>
<body>
<input type="radio" name="animation" value="pulse"> Pulse
<input type="radio" name="animation" value="bounce"> Bounce
</body>
</html>
Code Runtime Results:
7. Changing the Layout of Radio Buttons
You can change the layout of radio buttons using CSS. You can use the display
property to set the display mode.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Layout</title>
<style>
input[type="radio"] {
display: block;
margin-bottom: 10px;
}
</style>
</head>
<body>
<input type="radio" name="layout" value="vertical"> Vertical
<input type="radio" name="layout" value="horizontal"> Horizontal
</body>
</html>
Code Running Result:
8. Changing the Border Style of a Radio Button
You can change the border style of a radio button using CSS. Use the border-style
property to set the border style.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Border Style</title>
<style>
input[type="radio"] {
border: 2px dashed #000;
}
</style>
</head>
<body>
<input type="radio" name="border" value="dashed"> Dashed
<input type="radio" name="border" value="dotted"> Dotted
</body>
</html>
Code Running Results:
9. Changing the Shadow Effect of Radio Buttons
You can add a shadow effect to radio buttons using CSS. Use the box-shadow
property to set the shadow.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Shadow</title>
<style>
input[type="radio"] {
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body>
<input type="radio" name="shadow" value="soft"> Soft
<input type="radio" name="shadow" value="hard"> Hard
</body>
</html>
Code Running Result:
10. Changing the Opacity of Radio Buttons
You can change the transparency of radio buttons using CSS. Use the opacity
property to set the transparency.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Radio Button Opacity</title>
<style>
input[type="radio"] {
opacity: 0.5;
}
</style>
</head>
<body>
<input type="radio" name="opacity" value="50%"> 50%
<input type="radio" name="opacity" value="75%"> 75%
</body>
</html>
Code Running Results:
Conclusion
Through this article, we’ve learned how to use CSS to change the style of radio buttons, including color, size, shape, style, icon, animation, layout, border style, shadow effects, and transparency. By skillfully applying CSS, we can create a variety of cool radio button effects, enhancing the user experience and the aesthetics of web design.