HTML Form How to - Custom radio button with background image








Question

We would like to know how to custom radio button with background image.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
input[type=radio] {<!--from ww w  .  j  a  v a  2  s  .  co  m-->
  display: none;
}

input[type=radio]+label span {
  display: inline-block;
  width: 25px;
  height: 25px;
  margin: -1px 4px 0 0;
  vertical-align: middle;
  cursor: pointer;
  background:url('http://placehold.it/20x20') left top no-repeat;
  background-size: 100px 75px;
}

input[type=radio]:checked+label span {
  background-position: -25px top;
}
</style>
</head>
<body>
  <input id="r1" name="radio" type="radio" value="blah">
  <label for="r1"><span></span>Radio</label>
  <input id="r2" name="radio" type="radio" value="blah2">
  <label for="r2"><span></span>Radio2</label>
</body>
</html>

The code above is rendered as follows: