DEV Community

Cover image for How to create a custom radio button
Stackfindover
Stackfindover

Posted on • Updated on

How to create a custom radio button

Hello, guys in this tutorial we will create a custom radio button using HTML & CSS

What radio button means?

In HTML, a radio button is a graphical control element that allows the user to choose only one of a predefined set of mutually exclusive options. The radio button is also known as the option button

A radio button in HTML can be defined using the <input> tag, like below example

<input type="radio" name="answer">
Enter fullscreen mode Exit fullscreen mode

Note: The input tag only creates a radio button. if you want to add a label also then you have to insert the label yourself.

Common Query

  1. How to create a radio button
  2. How to create a custom radio button
  3. How to create a radio button in HTML

Hello, guys In this tutorial we will try to solve above mention query. and also we will learn how to create a custom radio button using HTML & CSS

First, we need to create three files index.html and style.css then we need to do code for it.

Step:1

Add below code inside index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Custom Radio Buttons</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="style.css" />
  <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@500&display=swap" rel="stylesheet">
</head>
<body>
  <div class="wrapper">
    <input type="radio" name="select" id="one" checked>
    <input type="radio" name="select" id="two">

    <label for="one" class="option option-1">
      <span>One</span>
    </label>
    <label for="two" class="option option-2">
      <span>Two</span>
    </label>
  </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step:2

Then we need to add code for style.css which code I provide in the below screen.

* {
  padding: 0;
  margin: 0;
  outline: 0;
  color: #444;
  box-sizing: border-box;
  font-family: 'IBM Plex Sans', sans-serif;
}
html, body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: #180f2f;
}
.wrapper {
  display: inline-flex;
  background: #fff;
  height: 100px;
  width: 250px;
  align-items: center;
  justify-content: space-evenly;
  border-radius: 5px;
  padding: 20px 15px;
  box-shadow: 5px 5px 30px rgb(0 0 0 / 20%);
}
.wrapper .option {
  background: #fff;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  cursor: pointer;
  border-radius: 5px;
  padding: 0 10px;
  border: 2px solid #180f2f;
  transition: all 0.5s ease;
  margin: 0 10px;
}
input[type="radio"] {
  display: none;
}
input#one:checked ~ .option-1,
input#two:checked ~ .option-2 {
  background: #180f2f;
  border-color: #180f2f;
}

input#one:checked ~ .option-1 span,
input#two:checked ~ .option-2 span {
  color: #fff;
}
.wrapper .option span {
  font-size: 20px;
}
Enter fullscreen mode Exit fullscreen mode

How to create a custom radio button Video output:

How to create a custom radio button codepen output:

Top comments (0)