I am currently working on a website which incorporates heavily styled inputs, for example using the <label>
-element to style checkboxes:
<label class="checkbox">
<input type="checkbox">
<span></span>
</label>
And where the standard <input>
types just can't be reliably styled, We have recreated the neccessary functionality with other element types, for example <input type="range">
has been replaced with the following (and a bit of javascript):
<div class="rangeinput">
<div class="rangeinput-thumb></div>
<div class="rangeinput-slider"></div>
</div>
Now, the question is how to semantically add labels to these elements.
For regular inputs the <label>
-element would work, but for the provided examples neither <label>
nor <div>
are allowed content of a <label>
.
The checkbox-case can be solved by assigning an id to the checkbox and using the for
-attribute of the label element, so that it does not have to be placed around the other label. But this does not work for any custom elements since they are not labelable, and it would be preferable to use a single solution for every input.
Currently I am wrapping every input-like element in a <fieldset>
with a <legend>
, but this feels more like a hack than actually helping semantics:
<fieldset>
<legend>A styled rangeinput:</legend>
<div class="rangeinput">
<div class="rangeinput-thumb></div>
<div class="rangeinput-slider"></div>
</div>
</fieldset>
This question is not about the click-to-focus effect of the element, but rather how to semantically associate a short, descriptive text with an element that takes a user input.