自定义input[type="file"]的样式
input[type="file"]的样式在各个浏览器中的表现不尽相同:
1. chrome:
2. firefox:
3. opera:
4. ie:
5. edge:
另外,当我们规定 input[type="file"] 的高度,并把它的行高设置成与其高度相等后,chrome中难看的样式出现了:
“未选择任何文件”这一行并没有竖直居中。
似乎在 firefox 中好看一些,嗯,我比较喜欢用 firefox。但是这些浏览器中的表现不一致,我们必须做兼容处理。
思路:
1. 自定义与其中一个浏览器表现类似的样式,将其放在下层;
2. 将浏览器本身的表现出来的样式“隐藏掉”, opacity: 0; 置于上层,这样我们点击的才是 input[type="file"] 响应的事件;
3. 选择文件或改变文件后,改变显示 file 的值。
代码:
<form action="" class="activityForm">
<div class="file">
<label for="file">文件:</label>
<div class="userdefined-file">
<input type="text" name="userdefinedFile" id="userdefinedFile" value="未选择任何文件">
<button type="button">上传</button>
</div>
<input type="file" name="file" id="file">
</div>
</form>
.file {
position: relative;
height: 40px;
line-height: 40px;
}
.file label {
display: inline-block;
}
.userdefined-file {
position: absolute;
top:;
left: 60px;
z-index:;
width: 300px;
height: 40px;
line-height: 40px;
font-size:; /*应对子元素为 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:;
left: 60px;
z-index:;
opacity:;
width: 300px;
height: 40px;
line-height: 40px;
cursor: pointer;
}
document.getElementById("file").onchange = function() {
document.getElementById("userdefinedFile").value = document.getElementById("file").value;
}
这样处理后,就在各个浏览器中表现一致了:
1. 未选择任何文件时,在 chrome 中:
2. 在选择文件后,在 firefox 中:
自定义input[type="file"]的样式的更多相关文章
- html中,文件上传时使用的<input type="file">的样式自定义
Web页面中,在需要上传文件时基本都会用到<input type="file">元素,它的默认样式: chrome下: IE下: 不管是上面哪种,样式都比较简单,和很多 ...
- 自定义input[type="radio"]的样式
对于表单,input[type="radio"] 的样式总是不那么友好,在不同的浏览器中表现不一. 为了最大程度的显示出它们的差别,并且为了好看,首先定义了一些样式: <fo ...
- 自定义input[type="radio"]的样式(支持普通浏览器,IE8以上)
对于表单,input[type="radio"] 的样式总是不那么友好,在不同的浏览器中表现不一. 对单选按钮自定义样式,我们以前一直用的脚本来实现,不过现在可以使用新的伪类 :c ...
- input[type="file"]的样式以及文件名的显示
如何美化input[type="file"] 基本思路是: (1)首先在 input 外层套一个 div : (2)将 div 和 input 设置为一样大小(width和heig ...
- html中input type=file 改变样式
<style> #uploadImg{ font-size:12px; overflow:hidden; position:absolute} #file{ position:absolu ...
- html input[type=file] css样式美化【转藏】
相信做前端的都知道了,input[type=file]和其他输入标签不一样,它的事件必须是在触发自身元素上,而不能是其他元素.所以不能简单的把input隐藏,放个button上去. <a hre ...
- input[type='file']默认样式
<input type="file" name="" id="" value="" /> 当input的ty ...
- 自定义input[type="checkbox"]的样式
对复选框自定义样式,我们以前一直用的脚本来实现,不过现在可以使用新的伪类 :checkbox 来实现. 如果直接对复选框设置样式,那么这个伪类并不实用,因为没有多少样式能够对复选框起作用.不过,倒是可 ...
- 对input type=file 修改样式
效果图先给: 在html中涉及到文件选择的问题,文件选择使用 input(class="filter_input form-control" type="file) 但是 ...
随机推荐
- Fluent Nhibernate and Stored Procedures
sql:存储过程 DROP TABLE Department GO CREATE TABLE Department ( Id INT IDENTITY(1,1) PRIMARY KEY, DepNam ...
- 小白学Linux(四)--系统常用命令
这里记录一下基础的系统常用命令,都是日常可能用到的,需要记住的一些命令.主要分为5个模块:关于时间,输出/查看,关机/重启,压缩归档和查找. 时间: date :查看设置当前系统时间,dat ...
- php多版本管理phpenv
曾经有试过phpbrew的童鞋应该知道有多复杂 虽然这个好久没更新了,还是可以用的-- github:phpenv/phpenv 它的原理就是处理PATH变量,将你要求的php版本的路径加到PATH的 ...
- [python学习笔记]Day1
初识python 第一个python程序: print('Hello,Python') >>>Hello,Python python2与python3的一些主要的区别: 1.在pyt ...
- Python基础(一),Day1
python的安装 python2.x与3.x的部分区别 第一个python程序 变量 字符编码 注释 格式化字符串 用户输入 常用的模块初始 if判断 循环语句 作业 1.python的安装 可以在 ...
- centos/rhel 6.5下rabbitmq安装(最简单方便的方式)
wget -c http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.r ...
- c# Sqlite帮助类
最近有WPF做客户端,需要离线操作存储数据,在项目中考虑使用Sqlite嵌入式数据库,在网上找了不少资料,最终整理出一个公共的帮助类. Sqlite是一个非常小巧的数据库,基本上具备关系型数据库操作的 ...
- jQuery高级技巧——DOM操作篇
页面加载之DOMReady事件 所谓domReady,也就是文档就绪,我们都知道,在操作dom时必须要在dom树加载完成后才能进行操作.如何检测DOM树已经构建完成,以下是一些实现的方式: 1.使 ...
- 流媒体一些server
(1)darwin stream server (2)red5 (3)nginx rtmp
- 安卓开发_浅谈OptionsMenus(选项菜单)
Android平台下所提供的菜单大体上可分为三类:选项菜单.上下文菜单和子菜单. 当Activity在前台运行时,如果用户按下手机上的Menu键,此时就会在屏幕低端弹出相应的选项菜单.但这个功能需要开 ...