Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for empty file input

I know that styling file input is pretty minimal, which is not a bad thing.

Is there some way of selecting file input which is empty?

The plan is to show or hide a submit button depending on whether the file input is empty.

like image 648
Manngo Avatar asked Aug 31 '17 02:08

Manngo


People also ask

How do I empty an input field in CSS?

In modern browsers you can use :placeholder-shown to target the empty input (not to be confused with ::placeholder ).

Can I use CSS empty?

The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.


1 Answers

If you are comfortable marking the input as required, you could do it with just css:

input:invalid ~ .chosen {
    display: none;
}
input:valid ~ .empty {
    display: none;
}
<input type="file" required><br>

<span class="empty">Empty</span>
<span class="chosen">Chosen</span>
like image 167
Moishe Lipsker Avatar answered Sep 28 '22 02:09

Moishe Lipsker