I find the default HTML file upload button rather ugly. Annoying enough, there seems to be no way to style it directly. Here is how I created a custom file upload button.
<!--default html file upload button-->
<input type="file" id="actual-btn"/>
<!--our custom file upload button-->
<label for="actual-btn">Choose File</label>
By doing this, clicking the label element in the browser toggles the default HTML file upload button (as though we clicked it directly).
The output of the above code is below. As you can see, we only have a Choose File text (from the label element) a few pixels to the right of the actual upload button.
We can click the Choose File text, and it will toggle the upload window (Click it and see)
We hide the default HTML file upload button in the browser by adding the hidden attribute to the tag like so
<input type="file" id="actual-btn" hidden/>
Now we style the label element to look more like a button.
Now we have this beautiful custom button, which actually works like the original file upload button:
At this point, we are done. But there is one more glitch to fix.
With the default file upload button, there is a no file chosen text beside the button (scroll up to the first codepen window), which gets replaced with the name of the file we will be uploading. Unfortunately, we don't get to see that with our custom button. How do we do that?
What I did was to include a span tag (with an id of file-chosen) right after our custom file upload button.
Then I set the text content of the span element(with the id of file-chosen) to the name property of the file object returned. The final result is below. Test it out.
Kindly leave your comments and questions down below