web前端|html教程
自定义input[type="file"]的样式
web前端-html教程
input[type=”file”]的样式在各个浏览器中的表现不尽相同:
溪谷h5源码下载,vscode 去掉末尾空格,怎样设置ubuntu,tomcat后门,sqlite在哪里写命令,黄黑相间的多足爬虫,php安装gd2,新手学习seo 营销,豆豆钱网站源码,网页源码微信二维码路径,网站模板 博客lzw
1. chrome:
下载系统 源码,vscode可否编译arm,Ubuntu可视桌面,tomcat8.0占用,爬虫 sessionid,php给字符串加引号,宿迁seo推广价格费用,搜索源码网站,好看大气的网站模板免费下载lzw
商业论坛源码,vscode生成c语言,ubuntu 修改ip,tomcat配置部署位置,安卓数据库sqlite3,爬虫恢复,php获取上传文件后缀,雅安seo网络推广方案,全讯网站源码,购票网页模板lzw
2. firefox:
3. opera:
4. ie:
5. edge:
另外,当我们规定 input[type=”file”] 的高度,并把它的行高设置成与其高度相等后,chrome中难看的样式出现了:
“未选择任何文件”这一行并没有竖直居中。
似乎在 firefox 中好看一些,嗯,我比较喜欢用 firefox。但是这些浏览器中的表现不一致,我们必须做兼容处理。
思路:
1. 自定义与其中一个浏览器表现类似的样式,将其放在下层;
2. 将浏览器本身的表现出来的样式“隐藏掉”, opacity: 0; 置于上层,这样我们点击的才是 input[type=”file”] 响应的事件;
3. 选择文件或改变文件后,改变显示 file 的值。
代码:
html:
css: .file {position: relative;height: 40px;line-height: 40px;}.file label {display: inline-block;}.userdefined-file {position: absolute;top: 0;left: 60px;z-index: 2;width: 300px;height: 40px;line-height: 40px;font-size: 0; /*应对子元素为 inline-block 引起的外边距*/}.userdefined-file input[type="text"] {display: inline-block;vertical-align: middle;padding-right: 14px;padding-left: 14px;width: 220px;box-sizing: border-box;border: 1px solid #ccc;height: 40px;line-height: 40px;font-size: 14px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}.userdefined-file button {display: inline-block;vertical-align: middle;width: 80px;text-align: center;height: 40px;line-height: 40px;font-size: 14px;background-color: #f54;border: none;color: #fff;cursor: pointer;}.file input[type="file"] {position: absolute;top: 0;left: 60px;z-index: 3;opacity: 0;width: 300px;height: 40px;line-height: 40px;cursor: pointer;}
js:document.getElementById("file").onchange = function() { document.getElementById("userdefinedFile").value = document.getElementById("file").value;}
这样处理后,就在各个浏览器中表现一致了:
1. 未选择任何文件时,在 chrome 中:
2. 在选择文件后,在 firefox 中: