CSS Property Value How to - Create pure CSS image toggle








Question

We would like to know how to create pure CSS image toggle.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
input[type="checkbox"] {
  content: url('http://placehold.it/200x200');
  display: block;
  width: 200px;
  height: 200px;
}<!--   www .j a  v a 2 s  .  co m-->

input[type="checkbox"]:checked {
  content: url('http://placehold.it/210x210');
}

.native-hidden {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}
</style>
</head>
<body>
  <input class="native-hidden" type="checkbox" />
  Click image to see the change
</body>
</html>

The code above is rendered as follows: