About this snippet

Easily create beautiful custom styled radio buttons using pure CSS. This snippet shows how to replace default browser styles for a modern and consistent look in your forms.

Size
1.9 KB
Uses
HTML, CSS
Loads
Nothing external
Views
308
<div class="form-group">
  <label class="custom-radio">
    <input type="radio" name="option" value="1" checked>
    <span class="radio-style"></span> Option One
  </label>
  <label class="custom-radio">
    <input type="radio" name="option" value="2">
    <span class="radio-style"></span> Option Two
  </label>
</div>
body {
  font-family: Arial, sans-serif;
  padding: 20px;
  background-color: #f0f0f0;
  display: flex; justify-content: center; align-items: center; min-height: 100vh;
}
.form-group {
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.custom-radio {
  display: block;
  position: relative;
  padding-left: 30px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 16px;
  user-select: none;
  color: #333;
}

/* Hide the browser's default radio button */
.custom-radio input[type="radio"] {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

/* Create a custom radio button */
.radio-style {
  position: absolute;
  top: 0;
  left: 0;
  height: 20px;
  width: 20px;
  background-color: #eee;
  border: 1px solid #ccc;
  border-radius: 50%;
  transition: background-color 0.2s ease;
}

/* On mouse-over, add a grey background color */
.custom-radio:hover input ~ .radio-style {
  background-color: #ddd;
}

/* When the radio button is checked, add a blue background */
.custom-radio input:checked ~ .radio-style {
  background-color: #2196F3;
  border-color: #2196F3;
}

/* Create the indicator (the dot/circle - hidden when not checked) */
.radio-style:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the indicator (dot/circle) when checked */
.custom-radio input:checked ~ .radio-style:after {
  display: block;
}

/* Style the indicator (dot/circle) */
.custom-radio .radio-style:after {
  top: 6px;
  left: 6px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: white;
}
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save