Custom Radio Buttons with CSS

Radio buttons are displayed in a group of options and only one radio button can be selected at the same time. By default, the browser defines a basic style for radio buttons. To make the radio buttons style user-friendly, you can define custom styles for radio buttons.

In this example code snippet, we will create custom radio buttons with CSS. You can use these custom-style radio buttons in HTML form elements.

HTML Code:

<div class="form-radio">
    <input class="form-radio-input" type="radio" name="radio_input" id="radio_input_1">
    <label class="form-radio-label" for="radio_input_1">
        Radio One
    </label>
</div>
<div class="form-radio">
    <input class="form-radio-input" type="radio" name="radio_input" id="radio_input_2" checked>
    <label class="form-radio-label" for="radio_input_2">
        Radio Two (checked)
    </label>
</div>
<div class="form-radio">
    <input class="form-radio-input" type="radio" name="radio_input" id="radio_input_3">
    <label class="form-radio-label" for="radio_input_3">
        Radio Three
    </label>
</div>

CSS Code:

.form-radio {
    display: block;
    min-height: 1.5rem;
    padding-left: 1.5em;
    margin-bottom: 0.125rem;
}
.form-radio-input {
    width: 1em;
    height: 1em;
    margin-top: 0.25em;
    vertical-align: top;
    background-color: #fff;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    border: 1px solid rgba(0,0,0,.25);
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
}
.form-radio .form-radio-input {
    float: left;
    margin-left: -1.5em;
}
.form-radio-input[type=radio] {
    border-radius: 50%;
}
.form-radio-input:focus {
    border-color: #86b7fe;
    outline: 0;
    box-shadow: 0 0 0 0.25rem rgb(13 110 253 / 25%);
}
.form-radio-input:checked {
    background-color: #0d6efd;
    border-color: #0d6efd;
}
.form-radio-input:checked[type=radio] {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='2' fill='%23fff'/%3e%3c/svg%3e");
}

keyboard_double_arrow_up