Styling an input type="file" button
如何设置输入type="file"
按钮的样式。
Styling file inputs is notoriously difficult, as most browsers will not change the appearance from either css or javascript.
Even the size of the input will not respond to the likes of:
<input type="file" style="width:200px">
Instead you will need to use the size attribute:
<input type="file" size="60" />
For any styling more sophisticated than that (eg changing the look of the browse button) you will need to look at the tricksy approach of overlaying a styled button and input box on top of the native file input. The article already mentioned by rm at www.quirksmode.org/dom/inputfile.html is the best one I've seen.
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.
follow these steps then you can create custom styles for your file upload form:
1.) this is the simple html form(please read the html comments i have written here below)
<form action="#type your action here" method="POST" enctype="multipart/form-data">
<div id="yourBtn" style="height: 50px; width: 100px;border: 1px dashed #BBB; cursor:pointer;" onclick="getFile()">Click to upload!</div>
<!-- this is your file input tag, so i hide it!-->
<div style='height: 0px;width:0px; overflow:hidden;'><input id="upfile" type="file" value="upload"/></div>
<!-- here you can have file submit button or you can write a simple script to upload the file automatically-->
<input type="submit" value='submit' >
</form>
2.) then use this simple script to pass the click event to file input tag.
function getFile(){
document.getElementById("upfile").click();
}
now you can use any type of a styling without worrying how to change default styles. i know this very well, because i have been trying to change the default styles for month and a half. believe me it's very hard because different browsers have different upload input tag. So use this one to build your custom file upload forms.Here is the full AUTOMATED UPLOAD code.
<html>
<head>
<style>
#yourBtn{
position: relative;
top: 150px;
font-family: calibri;
width: 150px;
padding: 10px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border: 1px dashed #BBB;
text-align: center;
background-color: #DDD;
cursor:pointer;
}
</style>
<script type="text/javascript">
function getFile(){
document.getElementById("upfile").click();
}
function sub(obj){
var file = obj.value;
var fileName = file.split("");
document.getElementById("yourBtn").innerHTML = fileName[fileName.length-1];
document.myForm.submit();
event.preventDefault();
}
</script>
</head>
<body>
<center>
<form action="#type your action here" method="POST" enctype="multipart/form-data" name="myForm">
<div id="yourBtn" onclick="getFile()">click to upload a file</div>
<!-- this is your file input tag, so i hide it!-->
<!-- i used the onchange event to fire the form submission-->
<div style='height: 0px;width: 0px; overflow:hidden;'><input id="upfile" type="file" value="upload" onchange="sub(this)"/></div>
<!-- here you can have file submit button or you can write a simple script to upload the file automatically-->
<!-- <input type="submit" value='submit' > -->
</form>
</center>
</body>
</html>
enjoy!
Have a Nice Day,
链接地址: http://www.djcxy.com/p/23022.html上一篇: HTML <label>标签中的“for”属性是做什么的?
下一篇: 造型输入类型=“文件”按钮