HomeCSSCustom Radio Buttons CSS

Custom Radio Buttons CSS

Introduction:

In this tutorial, we will learn how to create custom radio buttons using HTML and CSS. Radio buttons are a common input element in web forms, and by customizing their appearance, we can enhance the overall user experience of our websites. We will go step-by-step through the code and explain the key concepts involved in building these custom radio buttons.

Things You Will Learn:

  1. How to hide the default radio button input using CSS.
  2. How to create custom radio button styles using CSS.
  3. How to toggle the appearance of the radio button when selected.
  4. How to apply custom styles to the label element associated with the radio button.
  5. How to use the “::before” pseudo-element to create a circular shape for the radio button.

Video Tutorial:

To follow along with this tutorial visually, you can watch the corresponding video tutorial on my YouTube channel.

Project Folder Structure:

To implement the custom radio buttons, you need the following file structure:

  • index.html (or any desired HTML file name)
  • styles.css (or any desired CSS file name)

HTML:

In your HTML file, create a form or any section where you want to include the custom radio buttons. Add the following code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Custom Radio Buttons</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <input type="radio" name="payment" id="card" checked="checked" />
    <label for="card">Card</label>

    <input type="radio" name="payment" id="cash" />
    <label for="cash">Cash</label>
  </body>
</html>

 

CSS:

In your CSS file, add the following code to style the radio buttons:

input[type="radio"] {
  display: none;
}
label {
  position: relative;
  color: #4189e0;
  font-family: "Poppins", sans-serif;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.8em;
  border: 3px solid #4189e0;
  padding: 1em 2em;
  border-radius: 0.5em;
}
label:before {
  content: "";
  height: 2em;
  width: 2em;
  border: 3px solid #4189e0;
  border-radius: 50%;
}

input[type="radio"]:checked + label:before {
  height: 1em;
  width: 1em;
  border: 0.65em solid #ffffff;
  background-color: #4189e0;
}

input[type="radio"]:checked + label {
  background-color: #4189e0;
  color: #ffffff;
}

 

Conclusion:

In this tutorial, we learned how to create custom radio buttons using HTML and CSS. We covered the necessary CSS properties and selectors to hide the default radio button and style the associated label element. By applying the concepts discussed here, you can customize the appearance of radio buttons to match the design and aesthetics of your website. Feel free to experiment further and add more styles to make your radio buttons stand out! For a more visual guide, make sure to watch the video tutorial on my YouTube channel.

Previous articleLiquid Text Effect
Next articleSplit and Reveal Text
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4 × two =

Most Popular