input type=file 上传文件样式美化(转载)
input type=file 上传文件样式美化
来源:https://www.jianshu.com/p/6390595e5a36
在做input文本上传时,由于html原生的上传按钮比较丑,需要对其进行美化,radio和checkbox类似
主要想到以下几种方法,欢迎大家补充
1. 通过position和opacity实现
- input设置:透明度为0,position为绝对定位,font-size足够大
- input外面套一层a或div等标签(此处以a举例),a设置:position为相对定位,
overflow: hidden
(为了避免在非a区域点击打开选择文件)
代码如下:
<html>
<head>
<style>
.ui-upload {
font-size: 14px;
width: 80px;
height: 30px;
line-height: 30px;
text-align: center;
position: relative;
cursor: pointer;
color: #fff;
background: #00abff;
border-radius: 3px;
overflow: hidden;
display: inline-block;
text-decoration: none;
}
.ui-upload input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
filter: alpha(opacity=0);
cursor: pointer
}
</style>
</head>
<body>
<a href="javascript:;" class="ui-upload">
<input type="file" />upload
</a>
</body>
</html>
2. 通过label标签的for属性实现
for 属性规定了label与哪个表单元素进行绑定,包含显式联系和隐式联系两种
- 显式联系:
以显式形式和控件联系起来,for属性的值和控件的id要保持一致
<label for="upload">upload</label>
<input type="file" id="upload" />
- 隐式联系:
在 <label>
标签中放入控件隐式地连接起来
<label>uplaod<input type="file" /></label>
代码如下:
<html>
<head>
<style>
.ui-upload {
height: 30px;
width: 80px;
background-color: #00abff;
font-size: 14px;
line-height: 30px;
cursor: pointer;
display: inline-block;
text-align: center;
color: #fff;
border-radius: 3px;
margin-left: 20px;
}
</style>
</head>
<body>
<label for="upload" class="ui-upload">upload</label>
<input type="file" id="upload" style="display: none;" />
<label class="ui-upload">upload<input type="file" style="display: none;" /></label>
</body>
</html>
3. 通过onclick事件获取input操作
代码如下:
<html>
<head>
<style>
.ui-upload {
height: 30px;
width: 80px;
background-color: #00abff;
font-size: 14px;
line-height: 30px;
cursor: pointer;
display: inline-block;
text-align: center;
color: #fff;
border-radius: 3px;
border: 0px;
margin-left: 20px;
}
</style>
</head>
<body>
<button class="ui-upload" onclick="document.getElementById('upload').click()">upload</button>
<input type="file" id="upload" style="display:none;" />
</body>
</html>
结论
本文推荐大家使用第二种label标签实现,因为它不需要繁琐css定位,也不需要通过事件绑定。

input type=file 上传文件样式美化(转载)的更多相关文章
- 自定义type='file'上传文件样式
改变默认的上传文件样式: 用label作为替代 <input id="file_-1" type="file" name="file" ...
- HTML <input type="file">上传文件——结合asp.net的一个文件上传示例
HTML的代码:(关键是要在form里设置enctype="multipart/form-data",这样才能在提交表单时,将文件以二进制流的形式传输到服务器) 一. <fo ...
- input type file上传文件之后清空内容。
上次写过如何上传文件,上传成功之后,会出现一些问题. 当我打开上传的文件,但是没有点击上传,然后关闭弹窗,接着继续上传刚才的那个文件.为了满足产品组的要求,我们一般都会把样式进行一定的覆盖. 但这就会 ...
- jquery判断 input type="file"上传文件是否为空
要想获取type="file"的input内容,用var file = $("id").val();肯定是不行的,下面是代码: html上传按钮为: <i ...
- <input type="file">上传文件并添加路径到数据库
注:这里是用的mvc所以没法用控件 html代码 <form method="post" enctype="multipart/form-data"> ...
- input type='file' 上传文件 判断图片的大小是否合格与witdh 和 height 是否合格
function CheckFiles(obj) { var array = new Array('gif', 'jpeg', 'png', 'jpg'); //可以上传的文件类型 if (obj.v ...
- onclick事件触发 input type=“file” 上传文件
添加按钮: <input type="button" name="button" value="浏览" onclick="j ...
- input[type=file]上传文件(格式判断、文件大小、上传成功后操作)
var isUploadImg = false; //在input file内容改变的时候触发事件******************上传图片 $('#filed').change(function( ...
- 使用 input[type=file]上传文件
var $file = $('#file'); $('#btn').click(function() { var data = new FormData(); data.append('file', ...
随机推荐
- maven项目打包成war包发布到tomcat下...
分为两种情况: 1.当你的项目中没有前端页面时,直接右键项目--Export--选择web下的warFile--选择目录到tomcat安装目录下的webapps下即可访问. 2.当项目中有前端页面时 ...
- [smf]smf论坛也很漂亮
smf论坛代码国内使用的比较少,可能是大家习惯了dz论坛的互动方式吧!但是,smf论坛系统不失为一款好的论坛系统,也是目前为数不多的开源代码.使用起来之后,你会体会到它强大的后台操作.(例如:插件的安 ...
- 华为SSH认证配置
[设备型号]华为switch-S5700s ————1: [SW1] rsa/dsa local-key-pair create #创建本地密钥对 The key name will be: SW1_ ...
- Windows-WMI 事件 ID 10或0x80041003 死机 解药
最近笔记本重复了好几次奇怪的现象,重启后进入桌面,然后死机,木有蓝屏. 后来在安全模式里查了事件,如下 日志名称: Application 来源: Micros ...
- Vipe-技术选型
1.AOP,IOC框架-Spring 选择Spring是最不需要考虑的.应该90%以上的JAVA项目都有用Spring. 2.ORM框架-Mybatis Mybatis入门比较简单,并且对SQL语法的 ...
- Python编程练习:简单的闹钟提醒
问题详情:当前时间为下午2点,你在手机上设置了一个闹钟提醒,10000秒后触发该闹钟,请问闹钟铃声响起时的具体时间?请用print打印出时间 源码: a = 10000 h,m,s=2,0,0 if ...
- Javascript高级编程学习笔记(19)—— 对象属性
面向对象的语言有一个标志,那就是语言中都有类的概念 前面的文章中我提到过ECMAScript中没有类的概念(ES6之前) 所以JS中的对象和其他语言中的对象存在着一些区别 ECMA中对对象的定义如下: ...
- 从 0 开始手写一个 Mybatis 框架,三步搞定!
阅读本文大概需要 3 分钟. MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码. 本文完成的Mybatis功能比较简单,代码还有许 ...
- Eclipse 启动报错 An internal error occurred during: "Initializing Java Tooling"
如图所示,我的Eclispe版本是Oxygen,启动的时候turnaround弹出这种错误. 多种情况会导致这种报错.通过[重置窗口布局],可解决大部分情况: 解决办法:点击菜单导航栏的Window ...
- 【spring】aop切面通知,日志处理
1.spring的切面编程 概念原理可以看这里:http://blog.csdn.net/moreevan/article/details/11977115 2.所需要的jar包 maven引入jar ...