Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most semantically-correct HTML element for a form field hint/note?

The label and the field are easy; we have <label> and then the relevant input field. But what is the most semantically-correct HTML element to use for the smaller informational text that goes under the field?

enter image description here

like image 993
Nathan Ridley Avatar asked Jun 28 '11 06:06

Nathan Ridley


People also ask

What is semantically correct HTML?

Semantic HTML is the correct use of HTML to reinforce the meaning of content on a web page, rather than merely define its appearance. Semantically correct HTML helps search engines, screen readers, and other user devices determine the significance and context of web content.

What is an example of a semantic element HTML?

Semantics in HTML In HTML, for example, the <h1> element is a semantic element, which gives the text it wraps around the role (or meaning) of "a top level heading on your page."

Is form tag a semantic element?

Yes. A semantic tag is simply an element that defines itself semanticly to the developer. Using an <article> tag is no different than using a block level element such as a <div> tag except it is semantically sound. The tag specifies independent, self-contained content.


1 Answers

I don't know of a specific element or attribute to connect them, but you can do so using the ARIA attribute aria-describedby:

<label for="firstname">First Name</label>
<input type="text" id="firstname" aria-describedby="firstname-explanation" />
<p id="firstname-explanation">e.g. John</p>

But including everything in the label seems good to me either (also gives good styling abilities, as you have a - semantic - container):

<label>
    <span class="form-item-title">First Name</span>
    <input type="text" id="firstname" />
    <span class="form-item-description">e.g. John</span>
</label>

Or you could even mix the two.

Another approach could be to put the description in the title (or placeholder as suggested by @Alohci) attribute of the input element (semantically this is the one describing it), but in this case you have to insert it to the markup through Javascript (or CSS using input:after { content : "e.g. " attr(placeholder) } - suggested by @Alohci).

like image 63
kapa Avatar answered Nov 12 '22 17:11

kapa