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: inherit
to 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-group
divs (in 1.) todisplay: grid
too. - Add a background colour to the
<input>
elements. <input>
have a defaultmin-width
applied 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-color
property on the input selectors:input { accent-color: red }
. This sets up checkboxes, radio buttons, range sliders. - On labels use
text-box-trim: both
to 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 usegap
on theform-group
class 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.