Checkbox, select and textarea in HTML forms
16 Checkbox, select and textarea
fairAdd the three remaining control shapes: an independent yes/no, a list of one-from-many, and a multi-line note. Label all three, and give the select a sensible default.
Stuck? Three hints, smallest first
- 1. A checkbox is independent, so its label goes after it and it does not need a group.
- 2.
<select>holds<option>; mark the default withselected. - 3.
<textarea>has novalueattribute. Its content is the text between the tags.
<p class="row">
<label for="pickup">Pick-up point</label>
<select id="pickup" name="pickup">
<option value="jibowu" selected>Jibowu</option>
<option value="ojota">Ojota</option>
<option value="berger">Berger</option>
</select>
</p>
<p class="check">
<input id="bag" name="bag" type="checkbox" value="yes">
<label for="bag">I am carrying a bag over 20kg</label>
</p>
<p class="row">
<label for="notes">Anything we should know</label>
<textarea id="notes" name="notes" rows="3" maxlength="200"></textarea>
</p>
.row {
display: grid;
gap: 4px;
margin: 0 0 12px;
max-width: 300px;
}
.row label,
.check label { font-size: 0.9rem; }
.row label { font-weight: 600; }
select,
textarea {
padding: 8px 10px;
border: 1px solid #8e8e98;
border-radius: 6px;
font: inherit;
}
textarea {
resize: vertical; /* let it grow downwards, not sideways out of the column */
}
.check {
display: flex;
align-items: flex-start;
gap: 8px;
margin: 0 0 12px;
max-width: 300px;
}
.check input { accent-color: #3055d8; margin-top: 3px; }
select:focus-visible,
textarea:focus-visible,
.check input:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 1px;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('a select with three options', countTag(html, 'select') === 1 && countTag(html, 'option') === 3, true);
test('one option is the default', (html.match(/\bselected\b/g) || []).length, 1);
test('the checkbox is a single independent control', (html.match(/type="checkbox"/g) || []).length, 1);
test('the textarea is empty by content, not by a value attribute', /<textarea[^>]*>\s*<\/textarea>/.test(html), true);
test('and it declares rows and a length cap', /rows="3"/.test(html) && /maxlength="200"/.test(html), true);
test('all three controls are labelled', (html.match(/<label for="/g) || []).length, 3);
test('resize is limited to vertical, so it cannot break the column', /resize\s*:\s*vertical/.test(css), true);
test('every control has a visible focus state', (css.match(/:focus-visible/g) || []).length >= 1, true);
Output
<textarea> is the odd one out: value="..." is silently ignored, and whitespace between the tags becomes part of the content, so <textarea>
</textarea> starts with a newline in it. Write the tags closed up. resize: vertical is the other quiet fix — the default lets a reader drag the box wider than its column and wreck the layout, and there is no reason they would need to.