内容简介:It’s entirely possible to build custom checkboxes, radio buttons, and toggle switches these days, while staying semantic and accessible. We don’t even need a single line of JavaScript or extra HTML elements! It’s actually gotten easier lately than it has b
It’s entirely possible to build custom checkboxes, radio buttons, and toggle switches these days, while staying semantic and accessible. We don’t even need a single line of JavaScript or extra HTML elements! It’s actually gotten easier lately than it has been in the past. Let’s take a look.
Here’s where we’ll end up:
Things sure have gotten easier than they were!
The reason is that we can finally style the ::before
and ::after
pseudo-elements on the <input>
tag itself. This means we can keep and style an <input>
and won’t need any extra elements. Before, we had to rely on the likes of an extra <div>
or <span>
, to pull off a custom design.
Let’s look at the HTML
Nothing special here. We can style our inputs with just this HTML:
<!-- Checkbox --> <input type="checkbox"> <!-- Radio --> <input type="radio"> <!-- Switch --> <input type="checkbox">
That’s it for the HTML part, but of course it’s recommended to have name and id attributes, plus a matching <label>
element:
<!-- Checkbox --> <input type="checkbox" name="c1" id="c1"> <label for="c1">Checkbox</label> <!-- Radio --> <input type="radio" name="r1" id="r1"> <label for="r1">Radio</label> <!-- Switch --> <input type="checkbox" name="s1" id="s1"> <label for="s1">Switch</label>
Getting into the styling
First of all, we check for the support of appearance: none;
, including it’s prefixed companions. The appearance
property is key because it is designed to remove a browser’s default styling from an element. If the property isn’t supported, the styles won’t apply and default input styles will be shown. That’s perfectly fine and a good example of progressive enhancement at play.
@supports(-webkit-appearance: none) or (-moz-appearance: none) { input[type='checkbox'], input[type='radio'] { -webkit-appearance: none; -moz-appearance: none; } }
As it stands today, appearance is a working draft, but here’s what support looks like:
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
82* | 74* | No | 79* | TP* |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
79* | 68* | 76* | 13.3* |
Like links, we’ve gotta consider different interactive states with form elements. We’ll consider these when styling our elements:
:checked :hover :focus :disabled
For example, here’s how we can style our toggle input, create the knob, and account for the :checked
state:
/* The toggle container */ .switch { width: 38px; border-radius: 11px; } /* The toggle knob */ .switch::after { left: 2px; top: 2px; border-radius: 50%; width: 15px; height: 15px; background: var(--ab, var(--border)); transform: translateX(var(--x, 0)); } /* Change color and position when checked */ .switch:checked { --ab: var(--active-inner); --x: 17px; } /* Drop the opacity of the toggle knob when the input is disabled */ .switch:disabled:not(:checked)::after { opacity: .6; }
We are using the <input>
element like a container. The knob inside of the input is created with the ::after
pseudo-element. Again, no more need for extra markup!
If you crack open the styles in the demo, you’ll see that we’re defining some CSS custom properties because that’s become such a nice way to manage reusable values in a stylesheet:
@supports(-webkit-appearance: none) or (-moz-appearance: none) { input[type='checkbox'], input[type='radio'] { --active: #275EFE; --active-inner: #fff; --focus: 2px rgba(39, 94, 254, .25); --border: #BBC1E1; --border-hover: #275EFE; --background: #fff; --disabled: #F6F8FF; --disabled-inner: #E1E6F9; } }
But there’s another reason we’re using custom properties — they work well for updating values based on the state of the element! We won’t go into full detail here, but here’s an example how we can use custom properties for different states.
/* Default */ input[type='checkbox'], input[type='radio'] { --active: #275EFE; --border: #BBC1E1; border: 1px solid var(--bc, var(--border)); } /* Override defaults */ input[type='checkbox']:checked, input[type='radio']:checked { --b: var(--active); --bc: var(--active); } /* Apply another border color on hover if not checked & not disabled */ input[type='checkbox']:not(:checked):not(:disabled):hover, input[type='radio']:not(:checked):not(:disabled):hover { --bc: var(--border-hover); }
For accessibility, we ought to add a custom focus style. We are removing the default outline because it can’t be rounded like the rest of the things we’re styling. But a border-radius along with a box-shadow can make for a rounded style that works just like an outline.
input[type='checkbox'], input[type='radio'] { --focus: 2px rgba(39, 94, 254, .25); outline: none; transition: box-shadow .2s; } input[type='checkbox']:focus, input[type='radio']:focus { box-shadow: 0 0 0 var(--focus); }
It’s also possible to align and style the <label>
element which directly follows the <input>
element in the HTML:
<input type="checkbox" name="c1" id="c1"> <label for="c1">Checkbox</label>
input[type='checkbox'] + label, input[type='radio'] + label { display: inline-block; vertical-align: top; /* Additional styling */ } input[type='checkbox']:disabled + label, input[type='radio']:disabled + label { cursor: not-allowed; }
Here’s that demo again:
Hopefully, you’re seeing how nice it is to create custom form styles these days. It requires less markup, thanks to pseudo-elements that are directly on form inputs. It requires less fancy style switching, thanks to custom properties. And it has pretty darn good browser support, thanks to @supports
.
All in all, this is a much more pleasant developer experience than we’ve had to deal with in the past!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。