自定义文件上传的按钮的样式css+js
2017-01-08 12:39
579 查看
核心就是一段css遮住了原生的input框,然后用js将文件的值传入到另一个指定的input框中
原文链接
http://geniuscarrier.com/how-to-style-a-html-file-upload-button-in-pure-css/?utm_source=ourjs.com
Styling a html file upload button in pure css could be cumbersome if you've ever tried. Take a look at the following screenshot about how different browsers deal with the upload button. It's pretty obvious that there is a fair amount of variation.
We are aiming for creating a neat file upload button which behaves finely and consistently in pure css cross browsers. And here we go:
Step 1. Create a simple html markup
Step 2. CSS: Tricky Part
For simplicity, I am using Bootstrap CSS to style the button (div.file-upload).
Demo:
Upload button with selected file
Unfortunately there is no PURE CSS way to do it. However, if you really want to display the selected file, the following JavaScript snippet could help for this case.
JavaScript:
DOM change
Demo:
原文链接
http://geniuscarrier.com/how-to-style-a-html-file-upload-button-in-pure-css/?utm_source=ourjs.com
How to Style a HTML file upload button in Pure CSS
12 June 2013 on cssStyling a html file upload button in pure css could be cumbersome if you've ever tried. Take a look at the following screenshot about how different browsers deal with the upload button. It's pretty obvious that there is a fair amount of variation.
We are aiming for creating a neat file upload button which behaves finely and consistently in pure css cross browsers. And here we go:
Step 1. Create a simple html markup
<div class="fileUpload btn btn-primary"> <span>Upload</span> <input type="file" class="upload" /> </div>
Step 2. CSS: Tricky Part
.fileUpload { position: relative; overflow: hidden; margin: 10px; } .fileUpload input.upload { position: absolute; top: 0; right: 0; margin: 0; padding: 0; font-size: 20px; cursor: pointer; opacity: 0; filter: alpha(opacity=0); }
For simplicity, I am using Bootstrap CSS to style the button (div.file-upload).
Demo:
Upload button with selected file
Unfortunately there is no PURE CSS way to do it. However, if you really want to display the selected file, the following JavaScript snippet could help for this case.
JavaScript:
document.getElementById("uploadBtn").onchange = function () { document.getElementById("uploadFile").value = this.value; };
DOM change
<input id="uploadFile" placeholder="Choose File" disabled="disabled" /> <div class="fileUpload btn btn-primary"> <span>Upload</span> <input id="uploadBtn" type="file" class="upload" /> </div>
Demo:
相关文章推荐
- 将自定义文件上传按钮的样式
- 【CSS】自定义上传文件按钮
- 怎样用纯HTML和CSS更改默认的上传文件按钮样式
- js自定义input文件上传样式
- 自定义文件上传按钮样式
- 怎样用纯HTML和CSS更改默认的上传文件按钮样式
- 自定义文件上传按钮样式
- 自定义 文件上传按钮样式
- CSS自定义文件上传按钮
- 【更改表单元素默认样式】更改自定义文件上传按钮样式
- js实现文件上传自定义样式
- 自定义表单中的文件上传按钮样式
- 自定义上传按钮样式html
- 自定义上传按钮样式html
- 自定义上传按钮样式html
- css实现上传文件,file按钮用图片显示
- 文件上传按钮(改写样式)
- 自定义上传按钮样式的终极解决方案_input透明法
- 自定义上传按钮样式html
- 文件上传控件 自定义样式