Web Front-end
CSS Tutorial
How to use pure CSS3 to beautify the radio button sample code sharing
How to use pure CSS3 to beautify the radio button sample code sharing
This pure CSS3 method of beautifying the radio button radio is suitable for the following situations:
1. It is compatible with IE9 and above. If you need to be compatible with IE8, you need to write an IE hack. Remove the style
2. Only the radio button radio is supported, because the circle in the radio button selection style can be made with CSS, but the check button checkbox selection effect requires an image or icon font library
3. No need for JS to support switching effects
The picture below is the final rendering:

HTML code:
<label for="man" class="radio">
<span class="radio-bg"></span>
<input type="radio" name="sex" id="man" value="男" checked="checked" /> 男
<span class="radio-on"></span>
</label>
<label for="woman" class="radio">
<span class="radio-bg"></span>
<input type="radio" name="sex" id="woman" value="女" /> 女
<span class="radio-on"></span>
</label>
CSS code:
.radio{
display: inline-block;
position: relative;
line-height: 18px;
margin-right: 10px;
cursor: pointer;
}
.radio input{
display: none;
}
.radio .radio-bg{
display: inline-block;
height: 18px;
width: 18px;
margin-right: 5px;
padding: 0;
background-color: #45bcb8;
border-radius: 100%;
vertical-align: top;
box-shadow: 0 1px 15px rgba(0, 0, 0, 0.1) inset, 0 1px 4px rgba(0, 0, 0, 0.1) inset, 1px -1px 2px rgba(0, 0, 0, 0.1);
cursor: pointer;
transition: all 0.2s ease;
}
.radio .radio-on{
display: none;
}
.radio input:checked + span.radio-on{
width: 10px;
height: 10px;
position: absolute;
border-radius: 100%;
background: #FFFFFF;
top: 4px;
left: 4px;
box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.3), 0 0 1px rgba(255, 255, 255, 0.4) inset;
background-image: linear-gradient(#ffffff 0, #e7e7e7 100%);
transform: scale(0, 0);
transition: all 0.2s ease;
transform: scale(1, 1);
display: inline-block;
}
The most important thing in this method is the selection effect The class name: .radio input:checked + span.radio-on
+ is a pseudo-class of CSS2, its meaning is: p+p selects all
element ; element.
That is, find the selected (:checked) input, and the span element with the class name radio-on after it will have a selected circle effect.
There are many ways to beautify the label on the Internet by making the label into a circle, but in this case, the single-selected text must be placed outside the label, which results in a problem when clicking on the text. , the radio effect cannot be switched.
So I added a span with the class name radio-bg to the label to specifically make the large circle at the back, and then used a span with the class name radio-on to make the selected small circle in the front.
In this way, the text in the clicked label is retained, and the effect of radio selection can also be switched.
The above is the detailed content of How to use pure CSS3 to beautify the radio button sample code sharing. 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
1379
52
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.
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 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-
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 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.


