Styling Forms
- HTML: Keep elements in form-groups. That is a
<div>with the class ofform-group. This would include the label and input, textarea, button etc.. - Set the font of form elements to
font: inheritto get the same font, font-size, font-weight, font-style and line-height etc.. - You may also want to do the same with color:
color: inherit. - Set the
<form>element todisplay: grid. - Set the
form-groupdivs (in 1.) todisplay: gridtoo. - Add a background colour to the
<input>elements. <input>have a defaultmin-widthapplied by default. On the<input>elements affected add awidth: 100%to size them to the grid columns.- Set the accent colours of the form using the CSS
accent-colorproperty on the input selectors:input { accent-color: red }. This sets up checkboxes, radio buttons, range sliders. - On labels use
text-box-trim: bothto remove top and bottom gaps. Not fully supported yet but it’s a progressive enhancement. - Can go further to reduce this gap with
text-box-edge: cap alphabetic. Best use for uppercase text only as it can cut off descenders. You can then usegapon theform-groupclass to fine tune the gap.
On <input> elements you can use the pseudo classes :user-valid and user-invalid to style elements when they’re valid or not. For the email an @ symbol and a dot are required.