I’m going to leave a slightly more semantic approach with a focus on accessibility. It starts from the approach suggested by Bootstrap. Using display:none
there is no way to guarantee that the element will be accessible to screen readers
Briefly enough you create a class, called sr-only
, and that you will use in all the elements that you want to hide from the screen, but want to make accessible to the screen readers
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
After that you need to label the element so that the screen reader identifies what it is, for that you will use the attributes aria-label
and aria-labelledby
.
What this labelling does is something like ALT
used in the images, but the aria
vc can use in any element. The aria-label
will tell the discretion of the label
, and in the label
you put a id
that will be an "anchor" of aria-labelledby
of input
to say that he is the input
of label
with the aria-label
<label for="teste" aria-label="incluir arquivo" id="arquivo">
texto antes de chamar input
<input class="sr-only" type="file" id="teste" name="image" aria-labelledby="arquivo"/><br>
texto depois do input
</label>
Here in this Google documentation you can read more about aria-label
and aria-labelledby
https://developers.google.com/web/fundamentals/accessibility/semantics-aria/aria-labels-and-relationships?hl=pt-br
And this is the end result, see that there is no space between one line and another.
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
<label for="teste" aria-label="incluir arquivo" id="arquivo">
texto antes de chamar input
<input class="sr-only" type="file" id="teste" name="image" aria-labelledby="arquivo"/><br>
texto depois do input
</label>
I don’t understand... what I want is to click anywhere within that label area and later it call the input to select the file
– Caio Lourençon
input file is not?
– Caio Lourençon
The question is this, that in the other browsers it calls the same input with display None
– Caio Lourençon
would have somehow given call this input by clicking on something only I need it with display:None, would have? already tried with jquery also but does not work
– Caio Lourençon
Because I’ve tried it with position:Absolute, and visibility:Hidden and Buga my layout!
– Caio Lourençon