自定义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) 但是 ...
随机推荐
- 单例(C#版)
单例: 一个类只有一个实例.巧妙利用了编程语言的一些语法规则:构造函数private, 然后提供一个public的方法返回类的一个实例:又方法和返回的类的实例都是static类型,所以只能被类所拥有, ...
- JavaScript常用代码
页面的按钮全选: function CheckBoxAll() { //得到页面上所有input表单元素:document.getElementsByTagName( ...
- 用Perl编写Apache模块
前言 Apache被许多大流量网站所嫌弃,但很多企业级的场景则更为适用. Apache httpd 从 2.0 之后,已经不仅仅局限于一个 http 的服务器,更是一个完善而强大.灵活而健壮且容易扩展 ...
- Afinal
1.注解功能 1)继承:FinalActivity ( 需要复制 afinal_0.5.1_bin.jar到lib下) 2)@ViewInject() public class AfinalActiv ...
- Java 上传文件到 SFTP 抛异常 java.lang.NoClassDefFoundError: Could not initialize class sun.security.ec.SunEC 的解决办法
最近从 Op 那里报来一个问题,说是SFTP上传文件不成功.拿到的 Exception 如下: Caused by: java.lang.NoClassDefFoundError: Could not ...
- angular $http 请求数据的时候加载loading
1.目录结构 2.页面加载时效果(加载的时候比较难截图,是页面上方出现一条进度条,然后我另加了一个Loading..的提示,请忽略那个table) 3.页面加载完成后效果 4.index.html & ...
- 调用存储过程从EntityFramework
Prerequisites The prerequisite for running these examples are the following sample tables with test ...
- python基础之正则表达式。
简介 就其本质而言,正则表达式是内嵌在python内,由re模块实现,小型的专业化语言,最后由c写的匹配引擎执行.正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来 ...
- .NET的内存限制
之前做点云的.Net程序,经常因为数据量大出现Outofmemory异常,但是看看任务管理器,内存还有好多剩余的,在网上搜了一下发现这样的解释. 不管系统内存多大,目前一个.NET 对象最多只能够使用 ...
- Sizing and Capacity Planning for SharePoint 2013 - Resources
http://blogs.msdn.com/b/sanjaynarang/archive/2013/04/06/sizing-and-capacity-planning-for-sharepoint- ...