Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling an input type="file" button

Tags:

html

css

How do you style an input type="file" button?

<input type="file" />
like image 842
Shivanand Avatar asked Feb 21 '09 10:02

Shivanand


People also ask

How do I customize my upload button?

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).

Can we use style in input tag?

A style attribute on an <input> tag assigns a unique style to the input control. Its value is CSS that defines the appearance of the input element.

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!


1 Answers

You don't need JavaScript for this! Here is a cross-browser solution:

See this example! - It works in Chrome/FF/IE - (IE10/9/8/7)

The best approach would be to have a custom label element with a for attribute attached to a hidden file input element. (The label's for attribute must match the file element's id in order for this to work).

<label for="file-upload" class="custom-file-upload">     Custom Upload </label> <input id="file-upload" type="file"/> 

As an alternative, you could also just wrap the file input element with a label directly: (example)

<label class="custom-file-upload">     <input type="file"/>     Custom Upload </label> 

In terms of styling, just hide1 the input element using the attribute selector.

input[type="file"] {     display: none; } 

Then all you need to do is style the custom label element. (example).

.custom-file-upload {     border: 1px solid #ccc;     display: inline-block;     padding: 6px 12px;     cursor: pointer; } 

1 - It's worth noting that if you hide the element using display: none, it won't work in IE8 and below. Also be aware of the fact that jQuery validate doesn't validate hidden fields by default. If either of those things are an issue for you, here are two different methods to hide the input (1, 2) that work in these circumstances.

like image 148
Josh Crozier Avatar answered Oct 12 '22 11:10

Josh Crozier