How do you style an input type="file"
button?
<input type="file" />
Use a label tag and point its for attribute to the id of the default HTML file upload button. By doing this, clicking the label element in the browser toggles the default HTML file upload button (as though we clicked it directly).
A style attribute on an <input> tag assigns a unique style to the input control. Its value is CSS that defines the appearance of the input element.
The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!
See this example! - It works in Chrome/FF/IE - (IE10/9/8/7)
The best approach would be to have a custom label element with a for
attribute attached to a hidden file input element. (The label's for
attribute must match the file element's id
in order for this to work).
<label for="file-upload" class="custom-file-upload"> Custom Upload </label> <input id="file-upload" type="file"/>
As an alternative, you could also just wrap the file input element with a label directly: (example)
<label class="custom-file-upload"> <input type="file"/> Custom Upload </label>
In terms of styling, just hide1 the input element using the attribute selector.
input[type="file"] { display: none; }
Then all you need to do is style the custom label
element. (example).
.custom-file-upload { border: 1px solid #ccc; display: inline-block; padding: 6px 12px; cursor: pointer; }
1 - It's worth noting that if you hide the element using display: none
, it won't work in IE8 and below. Also be aware of the fact that jQuery validate doesn't validate hidden fields by default. If either of those things are an issue for you, here are two different methods to hide the input (1, 2) that work in these circumstances.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With