1
The titles of <input>
, <select>
, <textarea>
and others are known as label (Etiquette, in free translation into Portuguese).
Can be declared in HTML in the following ways:
Using for=" " : We referenced the
id
of the element within the attributefor
of the element label:<label for="name">Nome:</label> <input type="text" id="name">
Using Element inside label : In this second way, you don’t need to use the
for
and not even have aid
defined for the input (or select, textarea, etc). We just add the element within the label:<label>Nome: <input type="text"> </label>
Tips and Tricks:
In addition to the semantics added to the code, another positive point regarding the use of Labels is the improvement of usability. When the user clicks on the text of label the cursor is automatically placed inside the element by most browsers, which is very useful for input type=radio for example that usually have small clickable area.
In addition to the visual usability described above, another point that can also be improved is portability for screen readers. There are the properties of WAI-ARIA which are very interesting at this point and for the case of Abels we can use the attribute Aria-labelledby to reference the
id
of label at the element:<label for="input-name" id="label-name">Name:</label> <input labelledby="label-name" id="input-name" type="text">
A last interesting point to highlight is the line breaking between the text of label and the element. Preferably avoid using the tag
<br>
, this is a tag used to break text (text Node) according to MDN documentation. To make this "break" using CSS just add display: block to the input, as in the example below:input { display: block; }
<label>Nome: <input type="text"> </label>
Dude, you’re gonna have to use CSS to do this. To create the "title" of inputs you could use a tag label and manipulate it with css to stay the size and spacing you want, the spaces between inputs as well. I recommend reading some posts and handouts that teach the basics of CSS, it is very easy to learn. Read it here: http://www.linhadecodigo.com.br/artigo/3557/customizando-formularios-com-css.aspx
– user30043