When labeling a non-input element with no interactivity, such as a preview <img>
, is it better (or correct) to use <span>
instead of <label>
?
e.g.:
<span>Image preview:</span>
<img id="preview">
or this:
<label for="preview">Image preview:</label>
<img id="preview">
Not all inputs need labelsAn input with type="hidden" is also fine without a label. But all other inputs, including <textarea> and <select> elements, are happiest with a label companion.
What happens if there is no label? Input fields without accompanying labels can lead to accessibility issues for those who rely on screen readers. If a screen reader comes across an input field without a label it will try to find some accompanying text to use as the label.
HTML is not about presentation. It is a way of describing data. If you have some text that represents a label for an input, you wrap it in label tags not for presentation but because that's what it is. Without the label tag, that text is almost meaningless.
Effective form labels are required to make forms accessible. The purpose of form elements such as checkboxes, radio buttons, input fields, etcetera, is often apparent to sighted users, even if the form element is not programmatically labeled. Screen readers users require useful form labels to identify form fields.
The <label>
tag defines a label for an <input>
element.
So use <span>
instead.
The for attribute associates the label with a control element, as defined in the description of label in the HTML 4.01 spec. This implies, among other things, that when the label element receives focus (e.g. by being clicked on), it passes the focus on to its associated control. The association between a label and a control may also be used by speech-based user agents, which may give the user a way to ask what the associated label is, when dealing with a control. (The association may not be as obvious as in visual rendering.
HTML specifications do not make it mandatory to associate labels with controls, but Web Content Accessibility Guidelines (WCAG) 2.0 do. This is described in the technical document H44: Using label elements to associate text labels with form controls, which also explains that the implicit association (by nesting e.g. input inside label) is not as widely supported as the explicit association via for and id attributes,
I know this is old and has been answered and accepted - and my solution is the same as one of the comments (@Quentin) - but for the sake of increasing awareness and educating codes to use the semantically correct element - the figure and associated figcaption is the best tool for the job as described here.
Here is a link to documentation and the fugure elements is fully supported as a html5 element across all browesers (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure).
All parts of the figure element can be styled - and the figcaption can either be the first-child or the last-child of hte figure elements (- placing it before or after the image)
figure {
text-align: center;
}
figcaption {
margin-bottom: 4px;
color: blue;
font-weight: bold
}
figure img {
border: solid 1px blue;
border-radius: 8px;
}
<figure>
<figcaption>Image preview:</figcaption>
<img id="preview" src="https://i.pinimg.com/originals/3e/6b/cd/3e6bcdc46881f5355163f9783c44a985.jpg" alt="fluffy-kitten" height="100" />
</figure>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With