Skip to content

Styling forms (front end)

laminas-form produces HTML. How it looks is up to your CSS. These examples use one small SCSS stylesheet of our own, with no CSS framework and nothing loaded from a CDN.

You do not need Node to use it

The compiled file is included in each example application. Copy it to public/css/app.css in yours and link it from your layout:

mezzio/templates/layout/default.phtml
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= $this->headTitle() ?>
    <link rel="stylesheet" href="/css/app.css">
</head>
<body>
    <a class="skip-link" href="#main">Skip to main content</a>
    <?= $this->partial('demo::intro') ?>
    <main id="main" tabindex="-1">
        <?= $this->content ?>
    </main>
</body>
</html>
mvc/module/Application/view/layout/layout.phtml
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= $this->headTitle() ?>
    <link rel="stylesheet" href="/css/app.css">
</head>
<body>
    <a class="skip-link" href="#main">Skip to main content</a>
    <?= $this->partial('demo::intro') ?>
    <main id="main" tabindex="-1">
        <?= $this->content ?>
    </main>
</body>
</html>
mezzio/public/css/app.css
:root {
  color-scheme: light dark;
  --text: #1b1f23;
  --background: #ffffff;
  --muted: #4f5963;
  --control-border: #5c6670;
  --accent: #1a5fb4;
  --on-accent: #ffffff;
  --error: #b00020;
}

@media (prefers-color-scheme: dark) {
  :root {
    --text: #e8eaed;
    --background: #16181b;
    --muted: #b7bec6;
    --control-border: #9aa3ad;
    --accent: #8ab4f8;
    --on-accent: #10131a;
    --error: #ff9c9c;
  }
}
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 1rem;
  font: 100%/1.5 system-ui, sans-serif;
  color: var(--text);
  background: var(--background);
  overflow-wrap: break-word;
}

main {
  max-width: 36rem;
  margin-inline: auto;
}

a {
  color: var(--accent);
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* Present for the browser, but not shown */
.visually-hidden {
  position: absolute;
  left: -999rem;
}

/* Off screen until a keyboard user reaches it */
.skip-link {
  position: absolute;
  left: -999rem;
}
.skip-link:focus {
  position: static;
}

.field {
  margin-block-end: 1.5rem;
}

label {
  display: block;
  font-weight: 600;
}

.hint {
  margin: 0 0 0.25rem;
  color: var(--muted);
}

/* A font size of at least 1rem stops iOS zooming in on focus */
input:not([type=checkbox], [type=radio]),
textarea,
select {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.5rem;
  font: inherit;
  color: inherit;
  background: var(--background);
  border: 2px solid var(--control-border);
  border-radius: 0.25rem;
}

textarea {
  resize: vertical;
}

/* Safari ignores the height of a native select, so draw our own arrow instead */
select {
  appearance: none;
  padding-inline-end: 2.5rem;
  background-image: linear-gradient(45deg, transparent 50%, var(--text) 50%), linear-gradient(135deg, var(--text) 50%, transparent 50%);
  background-repeat: no-repeat;
  background-position: right 1.35rem center, right 1rem center;
  background-size: 0.35rem 0.35rem;
}

/* Background images are removed in forced colours, so use the native control there */
@media (forced-colors: active) {
  select {
    appearance: auto;
  }
}
.checkbox {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 0.75rem;
  align-items: center;
}
.checkbox .error {
  grid-column: 1/-1;
}
.checkbox input[type=checkbox] {
  width: 1.5rem;
  height: 1.5rem;
  margin: 0;
  accent-color: var(--accent);
}
.checkbox {
  /* The label is part of the target, so it is as tall as the other controls */
}
.checkbox label {
  display: flex;
  align-items: center;
  min-height: 2.75rem;
  font-weight: 400;
}

/* A group of radio buttons or checkboxes: the fieldset and legend name the question */
.radios,
.checkboxes {
  min-inline-size: 0;
  margin-inline: 0;
  padding: 0;
  border: 0;
}
.radios legend,
.checkboxes legend {
  padding: 0;
  font-weight: 600;
}
.radios label,
.checkboxes label {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  min-height: 2.75rem;
  font-weight: 400;
}
.radios input[type=radio],
.radios input[type=checkbox],
.checkboxes input[type=radio],
.checkboxes input[type=checkbox] {
  width: 1.5rem;
  height: 1.5rem;
  margin: 0;
  accent-color: var(--accent);
}

/* A repeating group of fields, such as one line of an order */
.line {
  min-inline-size: 0;
  margin: 0 0 1.5rem;
  padding: 1rem;
  border: 2px solid var(--control-border);
  border-radius: 0.25rem;
}
.line legend {
  padding-inline: 0.5rem;
  font-weight: 600;
}

button {
  min-height: 2.75rem;
  padding: 0.5rem 1.5rem;
  font: inherit;
  border: 2px solid var(--accent);
  border-radius: 0.25rem;
  cursor: pointer;
}

input[type=submit] {
  width: auto;
  padding-inline: 1.5rem;
  font-weight: 600;
  color: var(--on-accent);
  background: var(--accent);
  border-color: var(--accent);
  cursor: pointer;
}

/* A button that changes the form rather than sending it */
button.secondary,
input[type=submit].secondary {
  font-weight: 600;
  color: var(--accent);
  background: var(--background);
  border-color: var(--accent);
}

.error-summary {
  padding: 1rem;
  margin-block-end: 1.5rem;
  border: 4px solid var(--error);
  border-radius: 0.25rem;
}
.error-summary h2 {
  margin-block-start: 0;
}
.error-summary a {
  color: var(--error);
}
.error-summary:focus {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* Errors are marked by a bar and by text, so colour is never the only signal */
.field:has([aria-invalid=true]) {
  padding-inline-start: 0.75rem;
  border-inline-start: 4px solid var(--error);
}

.error {
  margin: 0 0 0.25rem;
  padding: 0;
  list-style: none;
  font-weight: 600;
  color: var(--error);
}

[aria-invalid=true] {
  border-color: var(--error);
}

/* Only for the running demos: leave this file out of your own stylesheet */
.demo-note {
  max-width: 36rem;
  margin: 0 auto 1rem;
  padding: 0 1rem;
  color: var(--muted);
  border: 2px dashed var(--control-border);
  border-radius: 0.25rem;
}

The demo::intro line in each layout prints the note on the running examples. Leave it out of yours.

Changing the styles

To edit the SCSS you need Node. From examples/forms/assets:

npm ci
npm run watch   # rebuild while you edit
npm run build   # build once for both applications

Edit the .scss files, never the compiled CSS. The sources use @use, because Sass has deprecated @import.

assets/scss/app.scss
@use "tokens";
@use "base";
@use "forms";
@use "errors";
@use "demo";
assets/scss/_tokens.scss
$space: 1rem;
$radius: 0.25rem;
$border: 2px;
$measure: 36rem;
$target: 2.75rem;

:root {
    color-scheme: light dark;
    --text: #1b1f23;
    --background: #ffffff;
    --muted: #4f5963;
    --control-border: #5c6670;
    --accent: #1a5fb4;
    --on-accent: #ffffff;
    --error: #b00020;
}

@media (prefers-color-scheme: dark) {
    :root {
        --text: #e8eaed;
        --background: #16181b;
        --muted: #b7bec6;
        --control-border: #9aa3ad;
        --accent: #8ab4f8;
        --on-accent: #10131a;
        --error: #ff9c9c;
    }
}
assets/scss/_base.scss
@use "tokens" as *;

*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: $space;
    font: 100%/1.5 system-ui, sans-serif;
    color: var(--text);
    background: var(--background);
    overflow-wrap: break-word;
}

main {
    max-width: $measure;
    margin-inline: auto;
}

a {
    color: var(--accent);
}

:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}

/* Present for the browser, but not shown */
.visually-hidden {
    position: absolute;
    left: -999rem;
}

/* Off screen until a keyboard user reaches it */
.skip-link {
    position: absolute;
    left: -999rem;

    &:focus {
        position: static;
    }
}
assets/scss/_forms.scss
@use "tokens" as *;

.field {
    margin-block-end: $space * 1.5;
}

label {
    display: block;
    font-weight: 600;
}

.hint {
    margin: 0 0 $space * 0.25;
    color: var(--muted);
}

/* A font size of at least 1rem stops iOS zooming in on focus */
input:not([type="checkbox"], [type="radio"]),
textarea,
select {
    width: 100%;
    min-height: $target;
    padding: $space * 0.5;
    font: inherit;
    color: inherit;
    background: var(--background);
    border: $border solid var(--control-border);
    border-radius: $radius;
}

textarea {
    resize: vertical;
}

/* Safari ignores the height of a native select, so draw our own arrow instead */
select {
    appearance: none;
    padding-inline-end: $space * 2.5;
    background-image:
        linear-gradient(45deg, transparent 50%, var(--text) 50%),
        linear-gradient(135deg, var(--text) 50%, transparent 50%);
    background-repeat: no-repeat;
    background-position:
        right $space * 1.35 center,
        right $space center;
    background-size: $space * 0.35 $space * 0.35;
}

/* Background images are removed in forced colours, so use the native control there */
@media (forced-colors: active) {
    select {
        appearance: auto;
    }
}

.checkbox {
    display: grid;
    grid-template-columns: auto 1fr;
    column-gap: $space * 0.75;
    align-items: center;

    .error {
        grid-column: 1 / -1;
    }

    input[type="checkbox"] {
        width: 1.5rem;
        height: 1.5rem;
        margin: 0;
        accent-color: var(--accent);
    }

    /* The label is part of the target, so it is as tall as the other controls */
    label {
        display: flex;
        align-items: center;
        min-height: $target;
        font-weight: 400;
    }
}

/* A group of radio buttons or checkboxes: the fieldset and legend name the question */
.radios,
.checkboxes {
    min-inline-size: 0;
    margin-inline: 0;
    padding: 0;
    border: 0;

    legend {
        padding: 0;
        font-weight: 600;
    }

    label {
        display: flex;
        gap: $space * 0.75;
        align-items: center;
        min-height: $target;
        font-weight: 400;
    }

    input[type="radio"],
    input[type="checkbox"] {
        width: 1.5rem;
        height: 1.5rem;
        margin: 0;
        accent-color: var(--accent);
    }
}

/* A repeating group of fields, such as one line of an order */
.line {
    min-inline-size: 0;
    margin: 0 0 $space * 1.5;
    padding: $space;
    border: $border solid var(--control-border);
    border-radius: $radius;

    legend {
        padding-inline: $space * 0.5;
        font-weight: 600;
    }
}

button {
    min-height: $target;
    padding: $space * 0.5 $space * 1.5;
    font: inherit;
    border: $border solid var(--accent);
    border-radius: $radius;
    cursor: pointer;
}

input[type="submit"] {
    width: auto;
    padding-inline: $space * 1.5;
    font-weight: 600;
    color: var(--on-accent);
    background: var(--accent);
    border-color: var(--accent);
    cursor: pointer;
}

/* A button that changes the form rather than sending it */
button.secondary,
input[type="submit"].secondary {
    font-weight: 600;
    color: var(--accent);
    background: var(--background);
    border-color: var(--accent);
}
assets/scss/_errors.scss
@use "tokens" as *;

.error-summary {
    padding: $space;
    margin-block-end: $space * 1.5;
    border: 4px solid var(--error);
    border-radius: $radius;

    h2 {
        margin-block-start: 0;
    }

    a {
        color: var(--error);
    }

    &:focus {
        outline: 3px solid var(--accent);
        outline-offset: 2px;
    }
}

/* Errors are marked by a bar and by text, so colour is never the only signal */
.field:has([aria-invalid="true"]) {
    padding-inline-start: $space * 0.75;
    border-inline-start: 4px solid var(--error);
}

.error {
    margin: 0 0 $space * 0.25;
    padding: 0;
    list-style: none;
    font-weight: 600;
    color: var(--error);
}

[aria-invalid="true"] {
    border-color: var(--error);
}

Only for the running examples. Remove it and its @use line in your project.

assets/scss/_demo.scss
@use "tokens" as *;

/* Only for the running demos: leave this file out of your own stylesheet */
.demo-note {
    max-width: $measure;
    margin: 0 auto $space;
    padding: 0 $space;
    color: var(--muted);
    border: $border dashed var(--control-border);
    border-radius: $radius;
}

Using the SCSS in your own project

Copy the scss/ directory to the root of your project, then run these commands there. You need Node and a package.json (npm init -y creates one).

assets/bin/install.sh
#!/usr/bin/env bash
set -euo pipefail

npm install --save-dev sass@^1.108.2
npx sass --no-source-map scss/app.scss public/css/app.css

Run the second command again after each change, or add --watch to rebuild automatically:

npx sass --watch --no-source-map scss/app.scss:public/css/app.css

Which markup the styles hook into

Selector Comes from
.field The field partial from chapter 0
.hint The element's hint option, printed by the field partial
.error formElementErrors, given this class by the field partial
.checkbox Added to .field by the checkbox partial, first used in chapter 2
[aria-invalid="true"] Set on the element by the field partial
.radios The radio group partial from chapter 4
.checkboxes The checkbox group partial from chapter 8
.line The repeated fieldset in chapter 6
.secondary Buttons that change a form without sending it, in chapter 6
.visually-hidden The extra save button in chapter 6
.error-summary The error summary partial
.skip-link The layout

Because the error styles follow aria-invalid, what people see and what assistive technology announces stay in step.

Accessibility choices

  • Text, borders and errors have their own colours for light and dark mode.
  • Keyboard focus always has a visible outline.
  • Inputs and buttons are at least 44 pixels tall. Checkboxes and radio buttons are 24 pixels, with labels 44 pixels tall.
  • Invalid fields have a bar and a message, not only a colour change.
  • The layout fits a 320 pixel wide screen.