Custom Radio Buttons using only HTML & CSS

Custom radio buttons are a useful and effective way to create a unique user experience on your website. With their flexibility and versatility, they allow you to create a personalized and interactive design without the need for any external resources or scripts.

Radio buttons, also known as option buttons, are a form element used to allow users to select a single option from a list of choices. They are widely used in web forms, surveys, and polls, where the user must make a single choice.

In web development, HTML and CSS are the main languages for creating customized radio buttons. They are relatively easy to grasp and apply on your website. In this blog post, we will explore the process of crafting custom radio buttons using only HTML and CSS.

How To Create Custom Radio Buttons In HTML & CSS

Create a Folder: Start by creating a folder for your project. You can name it anything you like. Inside this folder, you’ll need to create the following files:

  • index.html (for the structure of your Custom Radio Buttons)
  • style.css (for styling the Custom Radio Buttons)

Create the HTML File:

  • Name your HTML file index.html.

Create the CSS File:

  • Name your CSS file style.css.

HTML structure for our Buttons

To create a set of custom radio buttons, we first need to set up the HTML structure for our Buttons. This will consist of a label, input, and a span element for option.

				
					<!DOCTYPE html>
<!-- NotArena || www.NotArena.com -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Radio Buttons</title>
    <link rel="stylesheet" href="style.css">
<style id="wpr-lazyload-bg-container"></style><style id="wpr-lazyload-bg-exclusion"></style>
<noscript>
<style id="wpr-lazyload-bg-nostyle">.ultp-block-wrapper .slick-loading .slick-list{--wpr-bg-57871047-3fc0-4fed-83a0-fc0ee2757ab5: url('https://notarena.com/wp-content/plugins/ultimate-post/assets/img/loading.gif');}.rll-youtube-player .play{--wpr-bg-4a850234-5b33-4d93-a700-5f53ec753780: url('https://notarena.com/wp-content/plugins/wp-rocket/assets/img/youtube.png');}</style>
</noscript>
<script type="application/javascript">const rocket_pairs = [{"selector":".ultp-block-wrapper .slick-loading .slick-list","style":".ultp-block-wrapper .slick-loading .slick-list{--wpr-bg-57871047-3fc0-4fed-83a0-fc0ee2757ab5: url('https:\/\/notarena.com\/wp-content\/plugins\/ultimate-post\/assets\/img\/loading.gif');}","hash":"57871047-3fc0-4fed-83a0-fc0ee2757ab5","url":"https:\/\/notarena.com\/wp-content\/plugins\/ultimate-post\/assets\/img\/loading.gif"},{"selector":".rll-youtube-player .play","style":".rll-youtube-player .play{--wpr-bg-4a850234-5b33-4d93-a700-5f53ec753780: url('https:\/\/notarena.com\/wp-content\/plugins\/wp-rocket\/assets\/img\/youtube.png');}","hash":"4a850234-5b33-4d93-a700-5f53ec753780","url":"https:\/\/notarena.com\/wp-content\/plugins\/wp-rocket\/assets\/img\/youtube.png"}]; const rocket_excluded_pairs = [];</script></head>
<body>
    <div class="radio-group">
        <input type="radio" id="student" name="role" class="radio-input" checked>
        <label for="student" class="radio-label">
            <span class="radio-circle"></span>
            Student
        </label>

        <input type="radio" id="teacher" name="role" class="radio-input">
        <label for="teacher" class="radio-label">
            <span class="radio-circle"></span>
            Teacher
        </label>
    </div>
</body>
</html>
				
			

In the code above, we have created three options with corresponding labels and input elements. The input elements have a “type” attribute set to “radio” and a “name” attribute to group them. It is essential to have a unique “id” for each input element, as it will be used to connect it with its corresponding label.

Styling with CSS

Second, we will add some CSS properties to give our button a styling.

				
					 * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f0f0f0;
    font-family: 'Arial', sans-serif;
}

.radio-group {
    display: flex;
    gap: 20px;
    background-color: rgb(218, 230, 255);
    padding: 10px;
    border-radius: 12px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.radio-input {
    display: none;
}

.radio-label {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 30px;
    font-size: 16px;
    color: #444;
    background-color: #fff;
    border: 2px solid #ddd;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-align: center;
    width: 150px;
    position: relative;
}

.radio-circle {
    width: 20px;
    height: 20px;
    border: 2px solid #bbb;
    border-radius: 50%;
    display: inline-block;
    position: relative;
    transition: all 0.3s ease;
}

.radio-input:checked + .radio-label .radio-circle {
    background-color: #8a2be2; /* Purple fill when selected */
    border-color: #8a2be2; /* Purple border */
    box-shadow: 0 0 0 4px rgba(138, 43, 226, 0.3); /* Glowing effect */
}

.radio-input:checked + .radio-label {
    background-color: #8a2be2; /* Button background turns purple when selected */
    color: white;
    border-color: #8a2be2;
}

.radio-label:hover {
    border-color: #8a2be2; /* Red-orange on hover */
}

.radio-input:checked + .radio-label .radio-circle:after {
    content: '';
    width: 10px;
    height: 10px;
    background-color: white; /* White center for the checkmark */
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: all 0.3s ease;
}

				
			

We have used the span element to create the circle shape for our radio button. With the use of border-radius, we have given it a circular shape, and with the border property, we have added a white border.

Creating custom radio buttons using HTML and CSS can improve your website’s user experience. You can design unique and interactive radio buttons without using external resources or scripts. It’s important to ensure that custom radio buttons are accessible on your website. Experiment with different styles to find the perfect fit for your design.

If the code doesn’t work or if you run into any issues, please leave a comment below or reach out to us through the contact page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link
Powered by Social Snap