Home > CSS Tricks > Styling File Upload Input Box in CSS

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:

file_upload.jpg

You will put this HTML code in your page where you want to show custom file input box:

  1.  
  2. <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>
  3.  
  4. </div>

And put this CSS code in your style sheet file:

  1.  
  2. #divinputfile{
  3.  
  4.  background:url(upload_file.gif) no-repeat 100% 1px;
  5.  
  6.  height:28px;
  7.  
  8.  width:385px;
  9.  
  10.  margin:0px;
  11.  
  12. }#divinputfile #filepc{
  13.  
  14.  opacity: 0.0;
  15.  
  16.  -moz-opacity: 0.0;
  17.  
  18.  filter: alpha(opacity=00);
  19.  
  20.  font-size:18px;
  21.  
  22. }
  23.  
  24. #fakeinputfile{
  25.  
  26.  margin-top:-28px;
  27.  
  28. }
  29.  
  30. #fakeinputfile #fakefilepc{
  31.  
  32.  width:265px;
  33.  
  34.  height:22px;
  35.  
  36.  font-size:18px;
  37.  
  38.  font-family:Arial;
  39.  
  40. }

And finally place this upload_file.gif image with your files.

That’s All!

Click here to see demo!

Author: Burhan Khan

  1. April 16th, 2008 at 04:52 | #1

    Very useful information :) I see there are realy good tips. I am going to use some of them

  2. April 24th, 2008 at 10:26 | #2

    Very nice ;) btw very good tips i will use some of them. Some i did not knew.

  3. Marty
    May 9th, 2008 at 06:52 | #3

    thanks just what i was looking for.

  4. Senthil Kumar
    May 13th, 2008 at 06:24 | #4

    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.

  5. David
    May 16th, 2008 at 14:04 | #5

    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

  6. June 17th, 2008 at 09:16 | #6

    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

  7. June 19th, 2008 at 09:20 | #7

    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)

  8. June 19th, 2008 at 13:01 | #8

    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.

  9. December 12th, 2008 at 09:18 | #9

    Your blog is so nice and very interesting with the content. It looking great and describes the most useful things. Thanks for sharing.

  10. Karen
    December 15th, 2008 at 10:48 | #10

    This is easy to follow but I dont know why I still see the default \"browse\" button over top of my graphic. What did I do wrong?

  11. Karen
    December 15th, 2008 at 10:49 | #11

    I’m IE6…. sorry missed that before.

  12. Olle
    June 10th, 2009 at 07:44 | #12

    Thanks man!
    Works like a charm (in FF 3 and safari 4)

    Had some minor changes made though.
    Used position:relative in stead of negative margins, and z-index for consistency. But it works great in newer browsers. (Not tested in IE)

  13. Dinesh
    June 30th, 2009 at 08:52 | #13

    Hi this example is grt, but while using the upload via image , how to get the full path in to the text box
    like “c:/images/test.gif”
    currently it displays only “test.gif”

    thanks

  14. Rick
    August 28th, 2009 at 06:49 | #14

    Brilliant!

    NOTE: In .NET for some reason having the input with runat=server overrides the css. Putting the css inline fixed it for me.

  15. Arunabh Das
    November 10th, 2009 at 17:22 | #15

    Can\’t seem to get it to work. – Arunabh Das

  16. February 25th, 2010 at 04:32 | #16

    Nice,

    It really help me to figure out my input(type=’file’)css problem.

    Thanks,
    WJ

  17. March 21st, 2010 at 01:58 | #17

    Nice. Thanks for the article :)

  18. March 29th, 2010 at 20:31 | #18

    It really help me to figure out my input(type=’file��?css problem.

  19. April 2nd, 2010 at 21:34 | #19

    Nice,

    It really help me to figure out my input

  20. May 11th, 2010 at 22:44 | #20

    I am impressed by reading your article. These are great tips. I really enjoyed the post! thanks for sharing helpful tips. :)

  21. July 15th, 2010 at 04:28 | #21

    Excellent article.

    Greatly helped. Thanks for sharing with us. :)

  22. July 20th, 2010 at 11:35 | #22

    Hey great example, thank you

  23. August 12th, 2010 at 04:29 | #23

    Great help while working on webpage through css or div id. I cleared my doubt. Thanks a ton.

  24. September 13th, 2010 at 02:22 | #24

    set height for the input because it without it it will float when the site size is changed
    Ctrl + Scrol

    use:

    #divinputfile #filepc{
    opacity: 0.0;
    -moz-opacity: 0.0;
    filter: alpha(opacity=00);
    font-size:18px;
    height:28px;
    }

  25. Jack
    October 9th, 2010 at 22:11 | #25

    Nice!
    But how do you upload file,if you use
    input file(not have runat=server)

  26. October 11th, 2010 at 01:44 | #26

    thank you

  27. October 20th, 2010 at 02:29 | #27

    Hey thanks for that tip its working like magic
    Thanks a lot

  28. October 30th, 2010 at 01:51 | #28

    Hermes is the famous fashion brand of France

  29. November 23rd, 2010 at 03:30 | #29

    Very Nice tips

  30. Reese Nova
    November 30th, 2010 at 13:36 | #30

    I have the form working and it looks great, however I do not get the entire file path in my field only the file name.

  31. December 10th, 2010 at 10:32 | #31

    Im glad I came across onto it. Have a good day And Check me out

  32. Erkan
    January 10th, 2011 at 02:02 | #32

    Hi,

    When mouse is over browse button how can I show the Hand? The following (“cursor:pointer;”) did not work in Firefox. Any idea ? (This is probably because, it stayed under other controls)

    #divinputfile #filepc
    {
    opacity: 0.0;
    -moz-opacity: 0.0;
    filter: alpha(opacity=00);
    font-size:18px;
    margin-left:95px;
    cursor:pointer;

    }

  33. January 21st, 2011 at 06:44 | #33

    Good to find this website. I liked optimizing part in CSS in context with HTML. thanks for providing the information.

  34. February 5th, 2011 at 01:44 | #34

    Date Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject

  35. March 8th, 2011 at 09:10 | #35

    A very interesting and informative article.

  36. Ani
    May 10th, 2011 at 07:51 | #36

    Its help me very very much

  37. Ani
    May 10th, 2011 at 07:52 | #37

    Ani :
    Its help me very very much
    Thank You :)

    @Ani

  38. May 30th, 2011 at 06:17 | #38

    Hi! Would you mind if I share your blog with my facebook group? There’s a lot of folks that I think would really appreciate your content. Please let me know. Thank you

  39. June 12th, 2011 at 07:57 | #39

    I am just looking for this, thanks very much!

  40. July 7th, 2011 at 03:47 | #40

    Thanks for the article I will go and try this. I was really surprised when I tried to style the upload box and it didn’t work, and it was very hard to find any information on the web! Just hoping it works now!

  41. July 21st, 2011 at 12:08 | #41

    @Patrick
    is not workinf for IE …

  42. September 1st, 2011 at 06:15 | #42

    Perfect piece of work you have done, this web site is really cool with wonderful info .

  1. No trackbacks yet.