Forms are everywhere on the web: sign-ups, contact forms, search bars, checkouts. The browser's default form styles work, but they look plain and vary from browser to browser. In this tutorial we'll build up a clean, modern, accessible form step by step, using CSS.
Start with good HTML
Good styling starts with good HTML. Here's a simple contact form:
<form>
<div class="field">
<label for="name">Name</label>
<input type="text" id="name" name="name" autocomplete="name" required>
</div>
<div class="field">
<label for="email">Email</label>
<input type="email" id="email" name="email" autocomplete="email" required>
</div>
<button type="submit">Send message</button>
</form>
A few details make a big difference:
- Every field has a
<label>, linked by matching itsforattribute to the field'sid. Screen readers (software that reads pages aloud) announce the label, and clicking the label puts the cursor in the field. - Every
idis unique on the page. If two fields share anid, labels connect to the wrong field. - The right
type, such asemail, brings up a suitable keyboard on phones and gives you free validation. autocompletelets browsers fill in saved details, saving people time.- A
<button>element is easier to style than<input type="submit">, and can contain icons or other HTML. - Each label and field is wrapped in a
<div>(we've given it the classfield), which makes spacing them easy.
Laying out the form
First, let's give the form a container and some spacing. CSS Grid with gap spaces everything evenly, with no margins needed:
form {
display: grid;
gap: 16px;
max-width: 400px;
margin: 0 auto; /* centre the form */
padding: 24px;
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 12px;
}
.field {
display: grid;
gap: 6px; /* space between each label and its field */
}
label {
font-weight: 600;
}
Using max-width rather than a fixed width means the form shrinks to fit on small screens instead of overflowing.
Styling text fields
Now the text fields:
input,
select,
textarea {
box-sizing: border-box; /* include padding and border in the width */
width: 100%;
padding: 10px 12px;
font: inherit; /* use the page's font */
color: inherit;
background-color: #ffffff;
border: 1px solid #6b7280;
border-radius: 8px;
}
Three of these declarations fix common problems:
box-sizing: border-box: by default,width: 100%sets the width of the content only, so the padding and border are added on top, and the field sticks out of the form. Withborder-box, the padding and border fit inside the width.font: inherit: form controls don't use your page's font by default. They use a smaller system font, which often looks out of place.- A border with enough contrast: a field's border needs a contrast ratio of at least 3:1 against the background, so people can see where to type. The original post's
#cccborder was only about 1.6:1 on white. The grey#6b7280used here is around 4.8:1.
Tip: don't use placeholder text instead of a label. Placeholders disappear as soon as someone starts typing, are often low in contrast, and aren't reliably announced by screen readers. Use them, if at all, for short example values. Our post on styling placeholder text covers how to style them.
Focus styles
When someone clicks or tabs into a field, it should be obvious where they are. The original post only changed the field's background colour, which is easy to miss. A clear outline works much better:
input:focus,
select:focus,
textarea:focus {
border-color: #2563eb;
}
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 2px; /* a small gap between the field and the outline */
}
The :focus-visible pseudo-class matches when the browser decides a focus indicator is helpful. Text fields always show one when focused, while buttons only show it when reached with the keyboard, not when clicked with a mouse. Click into a field, or press Tab to move through the form below:
Never remove focus outlines with outline: none unless you replace them with something just as visible. Keyboard users rely on them to know where they are on the page.
Styling the button
The button in the form above uses these styles:
button {
padding: 12px 16px;
font: inherit;
font-weight: 600;
color: #ffffff;
background: #1d4ed8;
border: 0;
border-radius: 8px;
cursor: pointer;
}
button:hover {
background: #1e40af; /* a darker shade on hover */
}
button:active {
transform: translateY(1px); /* a subtle press effect */
}
button:disabled {
color: #4b5563;
background: #d1d5db;
cursor: not-allowed;
}
:hovergives feedback when the pointer is over the button.:activeapplies while it's being pressed, so a tiny movement makes it feel clickable.:disabledstyles a button that can't be used yet.
Because the form uses grid, the button stretches to the full width automatically. Add justify-self: start to the button if you'd prefer it to be only as wide as its text.
Tip: the original button colour, #007BFF, gives white text a contrast ratio of only about 3.9:1, which is below the 4.5:1 needed for normal-sized text. The darker #1d4ed8 used here is around 6.7:1.
Showing errors with :user-invalid
Attributes like required and type="email" let the browser check fields for you. CSS can then style fields that fail the check. The obvious choice, :invalid, has a problem: it matches straight away, so an empty required field shows an error before the person has typed anything.
The newer :user-invalid and :user-valid pseudo-classes wait until the person has interacted with the field, such as by typing and then moving to the next field, or trying to submit the form. They work in all major browsers.
input:user-invalid {
border-color: #b91c1c;
background-color: #fef2f2;
}
input:user-valid {
border-color: #15803d;
}
/* Hide the error message until the field is invalid */
.error {
display: none;
color: #b91c1c;
}
.field:has(:user-invalid) .error {
display: block;
}
Type something that isn't an email address into the field below, then press Tab or click outside it:
The :has() selector shows the message only when the field inside the same wrapper is invalid. The HTML for the field links the hint and the error to the input with aria-describedby, so screen readers read them out too:
<div class="field">
<label for="email">Email</label>
<p id="email-hint" class="hint">We'll only use this to reply to you.</p>
<input type="email" id="email" name="email" required
aria-describedby="email-hint email-error">
<p id="email-error" class="error">Please enter a valid email address, such as name@example.com.</p>
</div>
Tip: don't rely on a red border alone to show an error, as some people can't tell red from green. A written message explains what's wrong and how to fix it.
Checkboxes, radios, selects and more
Other controls need slightly different approaches:
form {
accent-color: #1d4ed8; /* colours checkboxes, radios and range sliders */
}
input[type="checkbox"],
input[type="radio"] {
width: 1.25em;
height: 1.25em;
}
select {
appearance: none; /* remove the browser's own arrow */
padding-right: 40px;
background-image: url("chevron.svg");
background-repeat: no-repeat;
background-position: right 12px center;
background-size: 18px;
}
textarea {
min-height: 4lh; /* at least 4 lines tall */
resize: vertical;
field-sizing: content; /* grow as the person types */
}
accent-coloris the quickest way to brand checkboxes, radio buttons and range sliders, with a single line. For fully custom designs, try our CSS Checkbox and Radio Generator.- Group related radio buttons in a
<fieldset>with a<legend>, so screen readers announce the question ("Preferred contact") along with each option. appearance: noneon a<select>removes the browser's arrow, so you can add your own as a background image. In the demo, the arrow is a small SVG written directly into the CSS.field-sizing: contentmakes the text area grow as you type more lines. It works in all major browsers, and older ones simply keep the fixed size.4lhuses thelhunit, which equals one line of text, so the text area starts four lines tall.
Coming soon: appearance: base-select lets you style the drop-down list of a <select> itself, including the options, with CSS. It currently works in Chrome and Edge (135 onwards). Other browsers simply show a normal select, so you can start using it as an enhancement.
Putting it all together
Here's a complete contact form using every technique from this post: grid layout, contrasting borders, clear focus styles, friendly validation messages and branded controls.
Checklist for styling forms
- Give every field a visible, linked
<label>and a uniqueid. - Use
box-sizing: border-boxandfont: inheriton form controls. - Make field borders contrast at least 3:1 with the background.
- Show a clear focus outline, and never remove it without a replacement.
- Use
:user-invalidfor error styles, with a written message. - Use
accent-colorfor quick branding of checkboxes, radios and sliders.
Wrapping up
Styling forms is about more than looks. A few lines of CSS make fields easier to read, easier to find and easier to fix when something goes wrong. Start with solid HTML, add clear borders and focus styles, and use modern features like :user-invalid, accent-color and field-sizing to finish the job with very little code.