Pure CSS implementation of radio and checkbox effect example

Pure CSS implementation of radio and checkbox effect example

radio-and-checkbox

Pure CSS to achieve radio and checkbox effects

reset-radio

When developing PC projects, radio and checkbox components are often used. However, because the native styles are relatively inconsistent with the designer's design style, we may often reference third-party modules to implement them, or hack them through other methods such as JS. This not only increases the amount of code, but is also very complicated, so there is this pure CSS implementation that relies on native input[radio] and input[checkbox]. The main code is as follows:

HTML main code

<div class="reset-radio">
    <input checked type="radio" id="age1" name="age">
    <span class="real-target"></span>
</div>

CSS code, here mainly through a child node span to cooperate with the input: checked brother selector to modify the style

.reset-radio {
    display: inline-block;
    position: relative;
    width: 16px;
    height: 16px;
}

.reset-radio .real-target {
    z-index: 1;
    width: 100%;
    height: 100%;
    position: absolute;
    display: inline-block;
    background: #ffffff;
    border: 1px solid #dadde0;
    border-radius: 100%;
    top: 0;
    left: 0;
    bottom: 0;
}

.reset-radio input[type=radio] {
    cursor: pointer;
    z-index: 2;
    width: 16px;
    height: 16px;
    opacity: 0;
    position: absolute;
    left: 0;
    top: 0;
    margin: 0;
    right: 0;
    bottom: 0;
}

.reset-radio input:checked+span {
    border-color: #48b4ec;
}

.reset-radio input:checked+span::before {
    content: '';
    position: absolute;
    background: #48b4ec;
    width: 6px;
    height: 6px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 100%;
}

reset-checkbox

The principle of reset-checkbox is the same, so I won't go into details.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Definition and usage of MySQL cursor

>>:  Brief analysis of the introduction and basic usage of Promise

Recommend

Using Vue3 (Part 1) Creating a Vue CLI Project

Table of contents 1. Official Documentation 2. Cr...

Linux kernel device driver virtual file system notes

/******************** * Virtual File System VFS *...

MySQL case when usage example analysis

First we create the database table: CREATE TABLE ...

Solve the problem that ElementUI custom CSS style does not take effect

For example, there is an input box <el-input r...

Vue globally introduces scss (mixin)

Table of contents 1. mixin.scss 2. Single file us...

Solve the problem of ugly blue border after adding hyperlink to html image img

HTML img produces an ugly blue border after addin...

Solve the problem of setting Chinese language pack for Docker container

If you use docker search centos in Docker Use doc...

How to create users and manage permissions in MySQL

1. How to create a user and password 1. Enter the...

Simple usage examples of MySQL custom functions

This article uses examples to illustrate the usag...

Meta viewport makes the web page full screen display control on iPhone

In desperation, I suddenly thought, how is the Sin...

Detailed explanation of JavaScript Reduce

Table of contents map filter some every findIndex...