Styling forms
A form is created with form_new (API reference §3.4) and its fields are defined by a form schema. This page covers the HTML a form publishes as, and how to style it.
A form is placed on a page as <cms-form id="4"></cms-form> and rendered at publish time into plain HTML. Everything it emits carries a cms- prefix, so a site stylesheet can target it without guessing and without colliding with the CMS.
/cms/css/form.css is the stylesheet the preview uses, and the one the submission endpoint falls back to for the pages it renders itself. It is a reasonable starting point — copy it into a site stylesheet and change the custom properties rather than starting from nothing.
A page carrying a form also loads /cms/js/form.js.
1. Shape of the markup
<form class="cms-form" id="cms-form-4" method="post" action="/form">
<input type="hidden" name="_form" value="4" />
<input type="hidden" name="_version" value="11" />
<input type="hidden" name="_token" value="…" />
<input type="hidden" name="_page" value="/contact/" />
<div class="cms-form-trap" aria-hidden="true">…</div>
<div class="cms-form-field cms-form-field-email cms-form-field-half">
<label for="cms-form-4-your_email">Your email<span class="cms-form-required">*</span></label>
<span class="cms-form-help" id="cms-form-4-your_email-help">…</span>
<input type="email" name="your_email" id="cms-form-4-your_email" required="required" />
</div>
<fieldset class="cms-form-section">
<legend>About your company</legend>
<div class="cms-form-field …">…</div>
</fieldset>
<div class="cms-form-actions"><button type="submit">Send</button></div>
</form>
Fields are siblings. A section is a fieldset containing its own fields, nested one level and no deeper.
Checkboxes, radio buttons and the required marker
In the HTML, every radio button and checkbox is an <input> followed by its <label>, both wrapped in a span. That includes the choices in a list, the points of a scale or star rating, and the anti-robot field:
<span class="cms-form-option"><input type="checkbox" name="topics[]" id="cms-form-4-topics-sales" value="sales" /><label for="cms-form-4-topics-sales">Sales</label></span>
In a list of choices, these spans sit inside .cms-form-options, in a fieldset whose legend is the question. The anti-robot field is a div.cms-form-field-robot that contains the spans directly, with no .cms-form-options around them.
<fieldset class="cms-form-field cms-form-field-checkbox-group cms-form-field-full">
<legend>Topics<span class="cms-form-required">*</span></legend>
<span class="cms-form-help" id="cms-form-4-topics-help">…</span>
<span class="cms-form-options cms-form-options-vertical" data-required="1">
<span class="cms-form-option">…</span>
<span class="cms-form-option">…</span>
</span>
</fieldset>
A single checkbox is a field of its own. In the HTML, its <input> comes before its <label>, and the label is the text shown beside the checkbox, not a heading above it:
<div class="cms-form-field cms-form-field-checkbox cms-form-field-full">
<span class="cms-form-help" id="cms-form-4-agree-help">…</span>
<input type="checkbox" name="agree" id="cms-form-4-agree" value="1" required="required" />
<label for="cms-form-4-agree">I agree to the terms<span class="cms-form-required">*</span></label>
</div>
The asterisk that marks a required field is a span.cms-form-required inside the label or legend, right after the text with no space between. Keep it inline, so it stays right after the last word even when the label wraps. Don't position it absolutely, or push it to the far edge with a flex row.
Two common rules misplace checkboxes and radio buttons. A rule for every input, such as a width, padding or display: block, also stretches checkboxes and radio buttons, so select text fields by type instead, such as input[type="email"]. A rule that makes every label a block puts each label below its checkbox or radio button. Labels above text fields are fine, so leave out the labels of checkboxes and radio buttons. These rules line checkboxes and radio buttons up with their labels:
/* Select text fields by type, not every input. */
.cms-form input[type="text"], .cms-form input[type="email"], .cms-form input[type="tel"],
.cms-form input[type="url"], .cms-form input[type="number"], .cms-form input[type="date"],
.cms-form select, .cms-form textarea { width: 100%; }
/* Each checkbox or radio button and its label on one line, lined up on the text's baseline. */
.cms-form-option, .cms-form-field-checkbox { display: flex; align-items: baseline; gap: 0.4rem; }
.cms-form-option { display: inline-flex; }
.cms-form-field-checkbox { flex-flow: row wrap; }
.cms-form-field-checkbox > .cms-form-help, .cms-form-field-checkbox > .cms-form-error { flex: 1 1 100%; }
.cms-form-option > input, .cms-form-field-checkbox > input { flex: none; margin: 0; }
.cms-form-option > label, .cms-form-field-checkbox > label { display: inline; margin: 0; font-weight: normal; }
/* The marker follows its text. */
.cms-form-required { margin-left: 0.15rem; color: …; }
Lining up on the baseline puts the bottom of each checkbox and radio button on the baseline of its label's first line. At their default size, they are about as tall as a capital letter, so they sit level with the text. A larger one reaches above the capital letters. If you enlarge them, also move them down by half the difference between their height and a capital letter's:
/* Larger checkboxes and radio buttons, still level with the first line of their labels. */
.cms-form-option > input, .cms-form-field-checkbox > input {
font: inherit;
width: 1.15em; height: 1.15em;
position: relative; top: calc((1.15em - 1cap) / 2);
}
Use the same size in the width, the height and the calc(). Centering them with align-items: center doesn't work: if a label wraps onto a second line, its checkbox or radio button ends up between the two lines.
Target .cms-form-option on its own, not .cms-form-options .cms-form-option, so the anti-robot checkboxes match the rest. /cms/css/form.css already handles all of this, so adapting a copy of it is the simplest way to start.
2. Classes
| Class | On | Meaning |
|---|---|---|
cms-form |
form |
The whole form. Also on the closed-form box. |
cms-form-field |
div or fieldset |
One field, with its label, help text and control. A fieldset when the control is a group of choices. |
cms-form-field-<type> |
as above | The element type: text, textarea, email, tel, url, number, select, radio, checkbox-group, checkbox, date. |
cms-form-field-full / -half |
as above | Requested width. Two consecutive halves are meant to share a row. |
cms-form-section |
fieldset |
A group of fields; its legend is the group heading. |
cms-form-text |
p |
Explanatory prose between fields. |
cms-form-help |
span |
Help text under a label. |
cms-form-required |
span |
The required marker inside a label. |
cms-form-options |
span |
Wrapper around a set of radios or checkboxes. |
cms-form-options-vertical / -horizontal |
as above | The layout the author chose. |
cms-form-option |
span |
One radio or checkbox with its label. |
cms-form-scale |
span |
A number rendered as a row of choices. |
cms-form-scale-label |
span |
The wording at either end of a scale. |
cms-form-stars |
span |
A number rendered as a star rating. |
cms-form-unit |
span |
Unit shown after a number, e.g. "employees". |
cms-form-field-robot |
div |
The anti-robot checkboxes. Must stay visible and usable. |
cms-form-actions |
div |
Holds the submit button. |
cms-form-summary |
div |
Appears above the fields when a submission comes back with errors: what to fix, each entry linking to its field. |
cms-form-error |
span |
The message on one field that was refused. |
cms-form-field-error |
on cms-form-field |
Marks a field that was refused, for styling its control. |
cms-form-trap |
div |
Spam trap. See §5. |
cms-form-closed |
div |
Shown in place of a form that is not accepting submissions. |
cms-form-errors |
div |
Preview only: why a schema cannot be rendered yet. |
cms-time |
time |
A moment the CMS wrote, in UTC in the datetime attribute. On a response page a script restates it in the reader's own timezone and conventions. Only tags carrying this class are touched. |
3. Identifiers
Built from the form id and the element key, so they are stable across publishes and unique when two forms share a page:
| Pattern | Example |
|---|---|
cms-form-<form> |
cms-form-4 |
cms-form-<form>-<key> |
cms-form-4-your_email |
cms-form-<form>-<key>-help |
cms-form-4-your_email-help |
cms-form-<form>-<key>-<option> |
cms-form-4-topics-sales |
cms-form-<form>-<key>-error |
cms-form-4-your_email-error |
cms-form-<form>-summary |
cms-form-4-summary |
cms-form-<form>-robot, -human, -trap |
cms-form-4-robot |
Style by class, not by id. A relabeled field keeps its key, but a field the author replaces gets a new one.
4. Attributes worth styling against
| Attribute | On | Use |
|---|---|---|
data-when-field, data-when-op, data-when-value |
a field or section | The field only applies when that condition holds. Present whether or not it is currently met. |
required |
the control | :required and :invalid work normally. In a required radio group, scale or star rating, every radio carries it. |
data-required, data-minchecked, data-maxchecked |
a checkbox list's .cms-form-options |
How many checkboxes must be ticked. required on a checkbox would mean that one checkbox must be ticked, so a checkbox list carries its rules here instead; the page script checks them and reports a problem on the list's first checkbox. |
aria-describedby |
a group's fieldset: radio buttons, a checkbox list, a scale or stars |
Names the group's help text and error message, which a screen reader reads on entering the group. |
data-no-past, data-no-future |
a date input | Bounds relative to the day of submission, which a static page cannot express as min/max. |
data-mindigits, data-maxdigits |
a phone input | Digit bounds, counted after punctuation is stripped. |
Two rules worth having:
.cms-form :focus-visible { outline: 2px solid …; } /* keyboard users */
.cms-form :user-invalid { border-color: …; } /* only after an attempt */
5. Do not restyle
.cms-form-trap must stay off screen and out of reach — it exists to be ignored by people and filled in by robots:
.cms-form-trap { position: absolute; left: -9999px; width: 1px; height: 1px; overflow: hidden; }
Deleting that rule puts a field labeled "Leave this field empty" in the middle of the form. Using display: none instead is weaker, since it is the first thing a robot checks for.
.cms-form-field-robot must stay visible. Hiding it breaks the form's spam protection.
6. Without CSS, and without JavaScript
The markup is ordered and labeled so that an unstyled form is still usable: every control has a label, groups are fieldset and legend, and the submit button comes last.
Conditional fields are rendered visible. With no script to apply data-when-*, a visitor sees every field, and the server decides what counted — a field whose condition is false is neither required nor stored. Do not write CSS that hides [data-when-field] outright; that would hide fields the visitor may need to fill in.
Where the script does run, it hides them with the hidden attribute rather than a class, so a stylesheet that sets display on those elements will fight it.
Without the script, a checkbox list's minimum and maximum are checked only by the server, which shows the form again with the message beside the list.