This Blog is about writing HTML forms in an elegant way, concerning the label
for a field, and the attribute "required
". It is about structure and DRY source-code, not about layout.
Fields need labels to be understood. Fields are required
(mandatory) sometimes, and should be identifiable as such right from start, not just after validation. The traditional way is to put an asterisk (star) on the label. Would be nice if labels of required fields also were bold. Further labels should be chained to their fields by id
reference, this enables the user to focus the field by clicking onto the label.
Here is a raw login-form, assuming "User" is required but "Password" is optional:
This looks ugly, but forget layout and styling for now. I want to think about the HTML structure of forms. Let's review the source-code of this "Login" form.
Here is CSS defining a basic table-layout, allowing table-cells being other than div
elements:
<style>
.table
{
display: table;
}
.table > div
{
display: table-row;
}
.table > div > label,
.table > div > input,
.table > div > select,
.table > div > textarea,
.table > div > div,
.table > div > fieldset
{
display: table-cell;
}
</style>
HTML, using label
and input
as table cells:
<form>
<div class="table">
<div>
<label for="username-id"><b>* User:</b></label>
<input name="username" required id="username-id">
</div>
<div>
<label for="password-id">Password:</label>
<input name="password" id="password-id">
</div>
</div>
<div>
<input type="submit" value="Login">
</div>
</form>
Why is this HTML source not elegant?
First, the "User" field has been marked three times as required:
required
attribute (browser validation)This would have to be repeated for all required fields. Can't we do better using CSS? I would suggest a ::before
pseudo-element for the asterisk, and a rule for the bold markup. Also the trailing colon could be done by an ::after
pseudo-element.
Second, I would like to avoid the handwritten id-chaining via <label for="username-id">. The label should be automatically chained to the field after it (or, in case of radio-buttons, before it).
Thus the table inside the form above should look like the following:
<div class="table">
<div>
<label>User</label>
<input name="username" required>
</div>
<div>
<label>Password</label>
<input name="password">
</div>
</div>
required
once on input
. The associated label should then know by CSS rules how to look like. id
. Let's generate an artificial id
out of the name
attribute and chain the label
to the input
that way automatically. Why not enclose the field into label?
<label>User
<input name="username" required>
</label>
I admit, the id-chaining would not be necessary then!
But: the field being a child element of the label would it make impossible to arrange them into a table layout. Further I plan to optionally move the label above the input via CSS, which is much easier when the label is not parent but sibling.
Not an easy way, beside CSS we will have to use JavaScript.
The basic problem here is that we can not write a CSS selector pointing to the label of a required field. Styles inside the rule-set will be applied to the last element in the selector. It is impossible to write a CSS selector that designates a <label>
followed by an <input required>
:
label + input[required] {
/* this selector would style the INPUT, not the LABEL! */
}
So, why not put the label behind the input-field? This leads to other problems that are not easy to solve either, the label structurally being right of the field. Moreover it would result in HTML that is not intuitive. So let's implement some CSS and a JavaScript that solves the problems.
/* Set asterisk before required fields. */
form label[required]::before
{
content: "* ";
}
/* Add colon after any label. */
form label::after
{
content: ": ";
}
/* Make labels for required fields better visible. */
form label[required]
{
font-weight: bold;
}
This CSS puts the * (asterisk) before any label that holds an attribute required
, and it puts a : (colon) behind. It displays the label text in a bold font.
Two of these CSS selectors are built on the fact that the label
element itself holds the required
, which is not true for our goal-HTML above! So the JavaScript needs to copy that attribute from the input
(select
, textarea
) to the label
to make this CSS work.
1 | /** |
This script installs a document-load listener that will execute the function manageIdAndRequired()
when called. That means, this function will be executed just once when the document is ready.
The manageIdAndRequired()
is a function that encapsulates two other internal functions. It loops over all forms and the elements contained in them, searching for label
elements. When it found one, it calls chainLabelToIdAndCopyRequired()
with the label as parameter.
The chainLabelToIdAndCopyRequired()
function looks at the next sibling element. In case it's none of "INPUT", "SELECT", "TEXTAREA", "DIV" or "FIELDSET" it will do nothing (mind that tagName
values are uppercase!). Else it will check whether the next sibling has an attribute required
. When yes, it copies it to the label, this will make the CSS above work.
Next it will check whether there is an id
in next sibling (previous sibling for radio buttons). When no, it uses the sibling's name
to generate a unique id
for it. That id
is then also written to the label's for
attribute.
The containsRequired()
function is quite simple. It returns true in case the given element holds an attribute named required
, else it searches in all child elements for that, recursively.
Mind that the chainLabelToIdAndCopyRequired()
function strongly depends on the HTML structure. In case you have label
and input
separated by td
or div
elements, it won't work. You would have to introduce a findSibling()
function then.
This script has been written to cope with much more than the fields used in the login-form above. It also can handle nested fieldset
boxes and radio buttons inside div
wrappers.
Here is how the HTML source looks after the script has run:
<div class="table">
<div>
<label required="" for="username_1">User</label>
<input name="username" required="" id="username_1">
</div>
<div>
<label for="password_2">Password</label>
<input name="password" id="password_2">
</div>
</div>
For each input
, the required
has been copied to the label
when present, and an id
has been generated and copied into the label
. No need any more to write these things explicitly into the HTML source!
A simple HTML structure helps to keep CSS and JavaScript code simple. I prefer a DIV-table to a traditional TABLE, because it is more flexible. I want to use this flexibility in a following Blog where I will introduce a form layout proposal, using all native input fields that HTML-5 browsers provide.
Main subject are the labels again. They need to be sometimes above, sometimes left of the fields. We would like to decide this through a CSS class on the DIV-table. Or maybe let the user control it through a toggle button?
ɔ⃝ Fritz Ritzberger, 2018-12-29