How to style the input type="file"
button.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
- Adding a timeout to a render function in ReactJS
You don't need JavaScript for this! Here is a cross-browser solution:
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'sfor
attribute must match the file element'sid
in order for this to work).As an alternative, you could also just wrap the file input element with a label directly: (example)
In terms of styling, just hide1 the input element using the attribute selector.
Then all you need to do is style the custom
label
element. (example).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.Styling file inputs is notoriously difficult, as most browsers will not change the appearance from either css or javascript.
Even the size of the input will not respond to the likes of:
Instead you will need to use the size attribute:
For any styling more sophisticated than that (e.g. changing the look of the browse button) you will need to look at the tricksy approach of overlaying a styled button and input box on top of the native file input. The article already mentioned by rm at www.quirksmode.org/dom/inputfile.html is the best one I've seen.
Hide it with css and use a custom button with $(selector).click() to activate the the browse button. then set an interval to check the value of the file input type. the interval can display the value for the user so the user can see whats getting uploaded. the interval will clear when the form is submitted [EDIT] Sorry i have been very busy was meaning to update this post, here is an example
Maybe a lot of awnsers. But I like this in pure CSS with fa-buttons:
Fiddle: https://jsfiddle.net/zoutepopcorn/v2zkbpay/1/
css can do a lot here... with a little trickery...
::reference::http://site-o-matic.net/?viewpost=19
-abbey
Update Nevermind, this doesn't work in IE or it's new brother, FF. Works on every other type of element as expected, but doesn't work on file inputs. A much better way to do this is to just create a file input and a label that links to it. Make the file input display none and boom, it works in IE9+ seamlessly.
Warning: Everything below this is crap!
By using pseudo elements positioned/sized against their container, we can get by with only one input file (no additional markup needed), and style as per usual.
Demo
Enjoy guys!
Old Update
Turned this into a Stylus mixin. Should be easy enough for one of you cool SCSS cats to convert it.
Usage: