Customize radiobutton appearance in EJ2 TypeScript Radio button control

8 Aug 20237 minutes to read

You can customize the appearance of the RadioButton component by using the CSS rules. Define own CSS rules according to your requirement and assign the class name to the cssClass property.

The background and border color of the RadioButton is customized through the custom classes to create the primary, success, info, warning, and danger type of RadioButton.

import { RadioButton } from '@syncfusion/ej2-buttons';

// To customize RadioButton appearance
// Refer the 'e-primary' class details in 'style.css'.
let radiobutton: RadioButton = new RadioButton({ label: 'Primary', name: 'custom', cssClass: 'e-primary' });
radiobutton.appendTo('#radiobutton1');

// Refer the 'e-success' class details in 'style.css'.
radiobutton = new RadioButton({ label: 'Success', name: 'custom', cssClass: 'e-success' });
radiobutton.appendTo('#radiobutton2');

// Refer the 'e-info' class details in 'style.css'.
radiobutton = new RadioButton({ label: 'Info', name: 'custom', cssClass: 'e-info', checked: true });
radiobutton.appendTo('#radiobutton3');

// Refer the 'e-warning' class details in 'style.css'.
radiobutton = new RadioButton({ label: 'Warning', name: 'custom', cssClass: 'e-warning' });
radiobutton.appendTo('#radiobutton4');

// Refer the 'e-danger' class details in 'style.css'.
radiobutton = new RadioButton({ label: 'Danger', name: 'custom', cssClass: 'e-danger' });
radiobutton.appendTo('#radiobutton5');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 RadioButton</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/24.2.3/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/24.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="styles.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
        <ul>
            <li><input type="radio" id="radiobutton1" /></li>
            <li><input type="radio" id="radiobutton2" /></li>
            <li><input type="radio" id="radiobutton3" /></li>
            <li><input type="radio" id="radiobutton4" /></li>
            <li><input type="radio" id="radiobutton5" /></li>
        </ul>
    </div>
</body>

</html>
#container {
  visibility: hidden;
}

#loader {
color: #008cff;
height: 40px;
width: 30%;
position: absolute;
top: 45%;
left: 45%;
}

li {
list-style: none;
}

.e-radio-wrapper {
margin-top: 18px;
}

.e-success .e-radio:checked + label::after { /* csslint allow: adjoining-classes */
background-color: #689f38;
border-color: #689f38;
}

.e-success .e-radio:checked:focus + label::after, .e-success .e-radio:checked + label:hover::after { /* csslint allow: adjoining-classes */
background-color: #449d44;
border-color: #449d44;
}

.e-success .e-radio:checked + label::before {
border-color: #689f38;
}

.e-success .e-radio:checked:focus + label::before, .e-success .e-radio:checked + label:hover::before { /* csslint allow: adjoining-classes */
border-color: #449d44;
}
.e-success .e-radio +label:hover::before {
border-color: #b1afaf
}
.e-info .e-radio:checked + label::after { /* csslint allow: adjoining-classes */
background-color: #2196f3;
border-color: #2196f3;
}

.e-info .e-radio:checked:focus + label::after, .e-info .e-radio:checked + label:hover::after { /* csslint allow: adjoining-classes */
background-color: #0b7dda;
border-color: #0b7dda;
}

.e-info .e-radio:checked + label::before {
border-color: #2196f3;
}

.e-info .e-radio:checked:focus + label::before, .e-info .e-radio:checked + label:hover::before {
border-color: #0b7dda;
}

.e-info .e-radio + label:hover::before {
border-color: #b1afaf
}

.e-warning .e-radio:checked + label::after { /* csslint allow: adjoining-classes */
background-color: #ef6c00;
border-color: #ef6c00;
}

.e-warning .e-radio:checked:focus + label::after, .e-warning .e-radio:checked + label:hover::after { /* csslint allow: adjoining-classes */
background-color: #cc5c00;
}

.e-radio:checked + .e-warning::before {
border-color: #ef6c00;
}

.e-warning .e-radio:checked:focus + label::before, .e-warning .e-radio:checked + label:hover::before {
border-color: #cc5c00;
}

.e-warning .e-radio + label:hover::before {
border-color: #b1afaf
}

.e-danger .e-radio:checked + label::after { /* csslint allow: adjoining-classes */
background-color: #d84315;
border-color: #d84315;
}
.e-danger .e-radio:checked:focus + label::after, .e-danger .e-radio:checked + label:hover::after { /* csslint allow: adjoining-classes */
background-color: #ba330a;
border-color: #ba330a;
}

.e-danger .e-radio:checked + label::before {
border-color: #d84315;
}

.e-danger .e-radio:checked:focus + label::before, .e-danger .e-radio:checked + label:hover::before {
border-color: #ba330a;
}

.e-danger .e-radio + label:hover::before {
border-color: #b1afaf
}