Styling File Upload Input Box in CSS
CSS is a very important part in optimizing your HTML source code as well as Search Engine Optimization, because it cut down most of your HTML source code and make it very clear.
Some days ago i was styling my HTML file input box in CSS, but strange it was not working. Although CSS works very well on other input boxes, like text, password, checkboxes and radio buttons. So i started searching on google but shocked to know that “The ‘Browse’ button, especially, is completely inaccessible to CSS manipulation”. I found some methods to style File Upload Box but all of them are using lot of Javascript.
Then i started brainstorming and after 5 hours of continues struggle make me successfull in making my own method with very little javascript (only single line) and working on almost all browsers. You can see final result of that input box in following image:

You will put this HTML code in your page where you want to show custom file input box:
-
-
<div id="divinputfile"> <input name="filepc" type="file" size="30" id="filepc" onchange="document.getElementById(’fakefilepc’).value = this.value;"/><div id="fakeinputfile"><input name="fakefilepc" type="text" id="fakefilepc" /></div>
-
-
</div>
And put this CSS code in your style sheet file:
-
-
#divinputfile{
-
-
background:url(upload_file.gif) no-repeat 100% 1px;
-
-
height:28px;
-
-
width:385px;
-
-
margin:0px;
-
-
}#divinputfile #filepc{
-
-
opacity: 0.0;
-
-
-moz-opacity: 0.0;
-
-
filter: alpha(opacity=00);
-
-
font-size:18px;
-
-
}
-
-
#fakeinputfile{
-
-
margin-top:-28px;
-
-
}
-
-
#fakeinputfile #fakefilepc{
-
-
width:265px;
-
-
height:22px;
-
-
font-size:18px;
-
-
font-family:Arial;
-
-
}
And finally place this upload_file.gif image with your files.
That’s All!
Author: Burhan Khan
Posted in: CSS Tricks |
April 16th, 2008 at 4:52 am
Very useful information
I see there are realy good tips. I am going to use some of them
April 24th, 2008 at 10:26 am
Very nice
btw very good tips i will use some of them. Some i did not knew.
May 9th, 2008 at 6:52 am
thanks just what i was looking for.
May 13th, 2008 at 6:24 am
I dont need the textbox length much more. How to reduce the length. The length is adjusted but distance between textbox and upload_file.gif did not differ.
May 16th, 2008 at 2:04 pm
For people worried about the ever increasing Mac user base.
This solution doesn’t seem to work on
Mac FF 2.0.0.14
Works but doesnt look right on
Mac Opera 9
Works Fine
Mac Safari 3.1.1
June 17th, 2008 at 9:16 am
this is a very good script u have take 5 hour to make it i also have take 5 hours to reach it and test it because i test frist alot of anther scripts which are very bad
June 19th, 2008 at 9:20 am
Hi, thanks for the simple script! I couldn’t get it to work in ie6 at first, until i put the real input below the fake one in the html:
i tweaked the css a bit to get things back in place again- didn’t have time to do it completely right, but you get the idea:
#divinputfile{
background:url(file_upload.jpg) no-repeat 100% 1px;
height:22px;
width:309px;
margin:0px;
position: relative;
overflow: hidden;
}
#divinputfile:hover {
background-position: 100% -20px;
}
#divinputfile #filepc{
opacity: 0.8;
-moz-opacity: 0.8;
filter: alpha(opacity=80);
position: absolute;
right: 0;
top: 0;
height: 22px;
width: 291px;
}
#fakeinputfile{
margin-top:18px;
}
#fakeinputfile #fakefilepc {
font-family:Arial;
margin-top: -17px;
}
My own version (in a unpublished website) is working in FF2.x, IE6, IE7, Opera and Safari (all on pc)
June 19th, 2008 at 1:01 pm
For some reason FF on Mac won’t work unless you use:
onchange=”document.getElementById(’fakefilepc’).value = document.getElementById(’filepc’).value;”
It seems like the onchange event is losing the scope of “this” otherwise.