Like a lot of people, I'd like to customize the ugly input type=file
, and I know that it can't be done without some hacks and/or javascript
. But, the thing is that in my case the upload file buttons are just for uploading images (jpeg|jpg|png|gif), so I was wondering if I could use a "clickable
" image which would act exactly as an input type file (show the dialog box, and same $_FILE on submitted page).
I found some workaround here, and this interesting one too (but does not work on Chrome =/).
What do you guys do when you want to add some style to your file buttons? If you have any point of view about it, just hit the answer button ;)
The input itself is hidden with CSS visibility:hidden.
Then you can have whatever element you whish - anchor or image.., when the anchor/image is clicked, trigger a click on the hidden input field - the dialog box for selecting a file will appear.
EDIT: Actually it works in Chrome and Safari, I just noticed that is not the case in FF4Beta
Actually it can be done in pure css and it's pretty easy...
HTML Code
CSS Styles
The idea is to position the input absolutely inside your label. set the font size of the input to something large, which will increase the size of the "browse" button. It then takes some trial and error using the negative left / top properties to position the input browse button behind your label.
When positioning the button, set the alpha to 1. When you've finished set it back to 0 (so you can see what you're doing!)
Make sure you test across browsers because they'll all render the input button a slightly different size.
You can see an example here (Add Track button): http://rakmastering.com/upload/
RUN SNIPPET or Just copy the above code and execute. You will get what you wanted. Very simple and effective without javascript. Enjoy!!!
Great solution by @hardsetting, But I made some improvements to make it work with Safari(5.1.7) in windows
I have used "visibility: hidden, width:0" instead of "display: none" for safari issue and added "pointer-events: none" in img tag to make it working if input file type tag is in FORM tag. seems working for me in all major browsers. Hope it helps someone.