How to implement radio button animation effects in CSS3
How to implement radio button animation effects in CSS3? Why do we need to implement radio button animation effects? Let's give you two examples to help you master the use of CSS3 to implement radio button animation effects
<div> <input type="radio" name="radio-1" id="radio-1-1" checked="checked"> <label for="radio-1-1"></label> <input type="radio" name="radio-1" id="radio-1-2"> <label for="radio-1-2"></label> <input type="radio" name="radio-1" id="radio-1-3"> <label for="radio-1-3"></label> </div>
Here, we specify the type value of the input tag as radio, and specify the names of all radios The values are all the same, so that the single selection effect can be achieved. Regarding the for attribute in the label here, I didn’t understand why it was set like this at first. Later, I searched for the definition of this attribute. Anyway, the general meaning is that as long as this attribute is set, when we click on the label element, browse The receiver will automatically shift focus to the radio. Next, use CSS to set effects on HTML.
.radio-1 { width: 900px; padding: 3% 0%; margin: 10px auto; background-color: darkseagreen; text-align: center;
}
.radio-1 label { display: inline-block; position: relative; width: 28px; height: 28px; border: 1px solid #cccccc; border-radius: 100%; cursor: pointer; background-color: #ffffff; margin-right: 10px;
}Here we first take a look at the settings for the label element. I have introduced most of the attributes in previous articles. The only unfamiliar attribute is cursor. This attribute is used to set the mouse style. , after setting it to pointer, when our mouse is placed on the label element, the mouse style becomes a hand (this is the case on my computer). Okay, let’s continue to look at
.radio-1 label:after {
content: ""; position: absolute; width: 20px; height: 20px; top: 4px; left: 4px; background-color: #666; border-radius: 50%; transform: scale(0); transition: transform .2s ease-out;
}
Here we use the after selector. Why set this attribute? Just to set the little black dot as shown in the picture above. First we set the content attribute to empty, which means we don't need to fill it with any content, because we just want to set the background color to black, that's all. Also, at the beginning, we set the scale value of transform to 0. The effect is to hide the small black dots.
.radio-1 [type="radio"]:checked + label { background-color: #eeeeee; transition: background-color .2s ease-in;
}
.radio-1 [type="radio"]:checked + label:after {
transform: scale(1); transition: transform .2s ease-in;
}
Note that the + symbol is used here. What does it mean? Its scientific name is called adjacent sibling selector, which means to select the element immediately after another element, and both have the same parent element. What it means here is to select the label that appears after the radio. Someone wants to ask, Why do we set this up? Just set the label directly. Imagine that in a very large system, we may use the label element many times. In order to avoid confusion, this setting will be more accurate. Here we see the transition attribute, which is used to set the transition effect. Finally, hide our radio and we’re done.
.radio-1 [type="radio"]{ display: none;
}
Action twoThis is our second special effect
demo2.gif
In fact, the first feeling when seeing this animation is that it is exactly the same as the previous one, except that the transform attribute Set it to rotate. I won’t explain it anymore. As long as you combine it with the previous example, you can easily create such an effect. Let’s go directly to the code:
<!DOCTYPE html><html><head>
<meta charset="UTF-8">
<title>Radio</title>
<style>
.radio-2 { width: 900px;padding: 3% 0; margin: 50px auto; background-color: darkseagreen; text-align: center;
}
.radio-2 label { display: inline-block; width: 28px; height: 28px; overflow: hidden; border: 1px solid #eeeeee; border-radius: 100%; margin-right: 10px; background-color: #ffffff; position: relative;cursor: pointer;
}
.radio-2 label:after { content: ""; position: absolute;top: 4px; left: 4px; width: 20px; height: 20px; background-color: #666666; border-radius: 50%; transform: rotate(-180deg);transform-origin: -2px 50%; transition: transform .2s ease-in;
} .radio-2 [type="radio"] { display: none;
}
.radio-2 [type="radio"]:checked + label:after{
transform: rotate(0deg); transition: transform .2s ease-out;
} </style></head><body><div>
<input type="radio" name="radio-2" id="radio-2-1" checked="checked">
<label for="radio-2-1"></label>
<input type="radio" name="radio-2" id="radio-2-2">
<label for="radio-2-2"></label>
<input type="radio" name="radio-2" id="radio-2-3">
<label for="radio-2-3"></label></div></body><ml>I believe everyone has read these two examples. It is now clear how to implement radio button animation effects in CSS3. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Related reading:
How to use css3 to create icon effects
How to use canvas to realize the interaction between the ball and the mouse
The above is the detailed content of How to implement radio button animation effects in CSS3. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
How to write split lines on bootstrap
Apr 07, 2025 pm 03:12 PM
There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.
How to insert pictures on bootstrap
Apr 07, 2025 pm 03:30 PM
There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.
How to resize bootstrap
Apr 07, 2025 pm 03:18 PM
To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
The Roles of HTML, CSS, and JavaScript: Core Responsibilities
Apr 08, 2025 pm 07:05 PM
HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.
How to set up the framework for bootstrap
Apr 07, 2025 pm 03:27 PM
To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.
How to use bootstrap in vue
Apr 07, 2025 pm 11:33 PM
Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.
How to use bootstrap button
Apr 07, 2025 pm 03:09 PM
How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
How to view the date of bootstrap
Apr 07, 2025 pm 03:03 PM
Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.


