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 we use a span to trigger input of type file and we simply customized that span, so we can add any styling using this way.
Note that we use input tag with visibility:hidden option and trigger it in the span.
Reference
HTML
CSS
jQuery
Fiddle: http://jsfiddle.net/M7BXC/
You can reach your goals too without jQuery with normal JavaScript.
Now the newBtn is linkes with the html_btn and you can style your new btn like you want :D
In case you're looking for a javascript library - out of the box solution, jquery-fileinput works fine.
ONLY CSS
Use this very simple and EASY
Html:
Css:
You can wrap your input type="file" inside of a label for the input. Style the label however you'd like and hide the input with display: none;
Working example here with native Drag and drop support : https://jsfiddle.net/ms3bbazv/4/
When styling a file input, you shouldn't break any of native interaction the input provides.
The
display: none
approach breaks the native drag and drop support.To not break anything, you should use the
opacity: 0
approach for the input, and position it using relative / absolute pattern in a wrapper.Using this technique, you can easily style a click / drop zone for the user, and add custom class in javascript on
dragenter
event to update styles and give user a feedback to let him see that he can drop a file.HTML :
CSS :
Here is a working example (with additional JS to handle
dragover
event and dropped files).https://jsfiddle.net/ms3bbazv/4/
Hope this helped !