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
the only way i can think of is to find the button with javascript after it gets rendered and assign a style to it
you might also look at this writeup
I would normally use simple javascript to customize the file input tag.A hidden input field,on click of button,javascript call the hidden field,simple solution with out any css or bunch of jquery.
If you are using Bootstrap 3, this worked for me:
See http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/
Which produces the following file input button:
Seriously, check out http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/
This is simple with jquery. To give a code example of Ryan's suggestion with a slight modification.
Basic html:
Be sure to set the styling on the input when you're ready:
opacity: 0
You can't setdisplay: none
because it needs to be clickable. But you can position it under the "new" button or tuck in under something else with z-index if you prefer.Setup some jquery to click the real input when you click the image.
Now your button is working. Just cut and paste the value when changed.
Tah dah! You may need to parse the val() to something more meaningful but you should be all set.
I've found a very easy method to switch the file button to a picture. You just label a picture and place it on top of the file button.
When clicking on the labeled image, you select the file button.
These answers are nice, and they all work for very specific use cases. That is to say, they are opinionated.
So, here's an answer that assumes nothing, but will work no matter how you modify it. You can change design with css, add javascript to maybe show a file name on change, etc. it will still always work.
Code:
Here is the core css
and the core html:
As you can see, we are forcing any pointer event(click) that happens on the .file-input element, or any of its children, to be proxied to the file input. This is because the file input is positioned absolute and will consume the width/height of the container always. You can therefore customize to fit your need. style the wrapper into a button, use some js to display file name on select, etc. nothing will break so long as the above core code remains intact.
As you will see in the demo, i have added a
span
with text "Select file" and a class with extra styles to style the.file-input
div. This should be the canonical starting point for anyone intending to create a custom file upload element.Demo:JSFIDDLE