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
Here is a solution which doesn't really style the
<input type="file" />
element but instead uses a<input type="file" />
element on top of other elements (which can be styled). The<input type="file" />
element is not really visible hence, the overall illusion is of a nicely styled file upload control.I came across this problem recently and despite the plethora of answers on Stack Overflow, none really seemed to fit the bill. In the end, I ended up customizing this so as to have a simple and an elegant solution.
I have also tested this on Firefox, IE (11, 10 & 9), Chrome and Opera, iPad and a few android devices.
Here's the JSFiddle link -> http://jsfiddle.net/umhva747/
Hope this helps!!!
This week I also needed to custom the button and display the selected file name aside it, so after reading some of the answers above (Thanks BTW) I came up with the following implementation:
HTML:
CSS
Javascript (Angular)
Basically I'm working with ng-file-upload lib, Angular-wise I'm binding the filename to my $scope and giving it the initial value of 'No file chosen', I'm also binding the onFileSelect() function to my scope so when a file gets selected I'm getting the filename using ng-upload API and assign it to the $scope.filename.
Don't be fooled by "great" CSS-only solutions that are actually very browser-specific, or that overlay the styled button on top of the real button, or that force you to use a
<label>
instead of a<button>
, or any other such hack. JavaScript IS necessary to get it working for general usage. Please study how gmail and DropZone do it if you don't believe me.Just style a normal button however you want, then call a simple JS function to create and link a hidden input element to your styled button.
Notice how the above code re-links it after every time the user chooses a file. This is important because "onchange" is only called if the user changes the filename. But you probably want to get the file every time the user provides it.
All rendering engines automatically generate a button when an
<input type="file">
is created. Historically, that button has been completely un-styleable. However, Trident and WebKit have added hooks through pseudo-elements.Trident
As of IE10, the file input button can be styled using the
::-ms-browse
pseudo-element. Basically, any CSS rules that you apply to a regular button can be applied to the pseudo-element. For example:This displays as follows in IE10 on Windows 8:
WebKit
WebKit provides a hook for its file input button with the
::-webkit-file-upload-button
pseudo-element. Again, pretty much any CSS rule can be applied, therefore the Trident example will work here as well:This displays as follows in Chrome 26 on OS X:
This is a nice way to do it with material / angular file upload. You could do the same with a bootstrap button.
Note I used
<a>
instead of<button>
this allows the click events to bubble up.Here is a solution, that also shows the chosen file name: http://jsfiddle.net/raft9pg0/1/
HTML:
JS:
CSS: