Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style "input file" with CSS3 / Javascript? [duplicate]

I would like to style <input type="file" /> using CSS3.

Alternatively, I would like user to press on a div (that I will style) and this will open the Browse window.

Is that possible to do that using HTML, CSS3, and Javascript / jQuery only ?

like image 232
Misha Moroshko Avatar asked Jul 12 '10 05:07

Misha Moroshko


People also ask

How do I change the color of my upload button in CSS?

Set the position to "relative" for the wrapper so as the element is placed relative to its normal position. Set the display of the "button" class to "inline-block" so as the element displays as an inline-level block container. Set the color of the text and the background-color of the button. Set the padding.

How do I customize the upload button in HTML?

Use a label tag and point its for attribute to the id of the default HTML file upload button. By doing this, clicking the label element in the browser toggles the default HTML file upload button (as though we clicked it directly).

How do I create a Choose file button in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

I have this rough example that you might want to get some idea...

html​

<div id="file">Chose file</div> <input type="file" name="file" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

CSS

#file {     display:none; }​ 

jQuery

var wrapper = $('<div/>').css({height:0,width:0,'overflow':'hidden'}); var fileInput = $(':file').wrap(wrapper);  fileInput.change(function(){     $this = $(this);     $('#file').text($this.val()); })  $('#file').click(function(){     fileInput.click(); }).show(); 

demo

​​​​​​​​​​​​​​

like image 164
Reigel Avatar answered Sep 21 '22 23:09

Reigel


After checking Reigels idea, and this one, I wrote this simple solution to the common problem of styling a type="file" input field (tested it on Firefox, Safari and Chrome).

<div style="position:relative;"> <div id="file" style="position:absolute;">Click here to select a file</div> <input type="file" name="file" style="opacity:0; z-index:1;" onchange="document.getElementById('file').innerHTML = this.value;"> </div> 

Then you can of course style the "file" div as you want.

And if you want to use a type="text" input instead of a div, simply change innerHTML for value:

<div style="position:relative;"> <input type="text" id="file" style="position:absolute;" placeholder="Click here to select a file"> <input type="file" name="file" style="opacity:0; z-index:1;" onchange="document.getElementById('file').value = this.value;"> </div> 

Here is my original answer using jQuery:

<div style="position:relative;"> <div id="file" style="position:absolute;">Click here to select a file</div> <input type="file" name="file" style="opacity:0; z-index:1;" onchange="$('#file').text($(this).val());"> </div> 
like image 21
Sophivorus Avatar answered Sep 22 '22 23:09

Sophivorus