|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + |
| 7 | + <title> |
| 8 | + Using CSS Webkit-Appearance To Style Checkbox ::after Pseudo-Element |
| 9 | + </title> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + |
| 13 | + <h1> |
| 14 | + Using CSS Webkit-Appearance To Style Checkbox ::after Pseudo-Element |
| 15 | + </h1> |
| 16 | + |
| 17 | + <style type="text/css"> |
| 18 | + |
| 19 | + label { |
| 20 | + align-items: center ; |
| 21 | + cursor: pointer ; |
| 22 | + display: flex ; |
| 23 | + } |
| 24 | + |
| 25 | + label input { |
| 26 | + margin: 0px 0px 0px 10px ; |
| 27 | + } |
| 28 | + |
| 29 | + /* |
| 30 | + Only apply custom styles to browsers that are going to support them. This is |
| 31 | + important because even old browsers will apply SOME of these CSS properties; |
| 32 | + and, we don't want to create Frankenstein checkboxes. |
| 33 | + */ |
| 34 | + @supports ( appearance: none ) or ( -moz-appearance: none ) or ( -webkit-appearance: none ) { |
| 35 | + |
| 36 | + input.custom { |
| 37 | + appearance: none ; |
| 38 | + -moz-appearance: none ; |
| 39 | + -webkit-appearance: none ; |
| 40 | + border: 2px solid currentcolor ; |
| 41 | + border-radius: 52px 52px 52px 52px ; |
| 42 | + color: #999999 ; |
| 43 | + padding: 2px 2px 2px 2px ; |
| 44 | + transition-duration: 300ms ; |
| 45 | + transition-property: border-color, color ; /* Safari needed border-color. */ |
| 46 | + transition-timing-function: ease ; |
| 47 | + width: 52px ; |
| 48 | + } |
| 49 | + |
| 50 | + input.custom:checked { |
| 51 | + color: #ff3366 ; |
| 52 | + } |
| 53 | + |
| 54 | + /* |
| 55 | + NOTE: The ::after pseudo-selector is being applied to the INPUT itself, |
| 56 | + not a parent element. I had no idea this even works! I can't find any |
| 57 | + documented support on this (just some StackOverflow threads). |
| 58 | + */ |
| 59 | + input.custom::after { |
| 60 | + background-color: currentcolor ; |
| 61 | + border-radius: 10px 10px 10px 10px ; |
| 62 | + content: "" ; |
| 63 | + display: block ; |
| 64 | + height: 18px ; |
| 65 | + transform: translateX( 0% ) ; |
| 66 | + transition: transform 300ms ease ; |
| 67 | + width: 45% ; |
| 68 | + } |
| 69 | + |
| 70 | + input.custom:checked::after { |
| 71 | + transform: translateX( 121% ) ; |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + </style> |
| 77 | + |
| 78 | + <p> |
| 79 | + <label for="checkbox1"> |
| 80 | + Default checkbox: |
| 81 | + <input id="checkbox1" type="checkbox" /> |
| 82 | + </label> |
| 83 | + </p> |
| 84 | + |
| 85 | + <p> |
| 86 | + <label for="checkbox2"> |
| 87 | + Webkit-Appearance checkbox: |
| 88 | + <input id="checkbox2" type="checkbox" class="custom" /> |
| 89 | + </label> |
| 90 | + </p> |
| 91 | + |
| 92 | +</body> |
| 93 | +</html> |
0 commit comments