A simple and elegant CSS radio button
A Pen by Tristan White on CodePen.
<div class="container"> | |
<div class="radio"> | |
<input id="radio-1" name="radio" type="radio" checked> | |
<label for="radio-1" class="radio-label">Checked</label> | |
</div> | |
<div class="radio"> | |
<input id="radio-2" name="radio" type="radio"> | |
<label for="radio-2" class="radio-label">Unchecked</label> | |
</div> | |
<div class="radio"> | |
<input id="radio-3" name="radio" type="radio" disabled> | |
<label for="radio-3" class="radio-label">Disabled</label> | |
</div> | |
</div> |
<script src="https://codepen.io/triss90/pen/VWWMWN"></script> |
$color1: #f4f4f4; | |
$color2: #3197EE; | |
body { | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
min-height: 100vh; | |
} | |
.radio { | |
margin: 0.5rem; | |
input[type="radio"] { | |
position: absolute; | |
opacity: 0; | |
+ .radio-label { | |
&:before { | |
content: ''; | |
background: $color1; | |
border-radius: 100%; | |
border: 1px solid darken($color1, 25%); | |
display: inline-block; | |
width: 1.4em; | |
height: 1.4em; | |
position: relative; | |
top: -0.2em; | |
margin-right: 1em; | |
vertical-align: top; | |
cursor: pointer; | |
text-align: center; | |
transition: all 250ms ease; | |
} | |
} | |
&:checked { | |
+ .radio-label { | |
&:before { | |
background-color: $color2; | |
box-shadow: inset 0 0 0 4px $color1; | |
} | |
} | |
} | |
&:focus { | |
+ .radio-label { | |
&:before { | |
outline: none; | |
border-color: $color2; | |
} | |
} | |
} | |
&:disabled { | |
+ .radio-label { | |
&:before { | |
box-shadow: inset 0 0 0 4px $color1; | |
border-color: darken($color1, 25%); | |
background: darken($color1, 25%); | |
} | |
} | |
} | |
+ .radio-label { | |
&:empty { | |
&:before { | |
margin-right: 0; | |
} | |
} | |
} | |
} | |
} |