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', ...
随机推荐
- IDEA的Database表的基本操作
1.创建表 方法一:直接创建:右键-new-table 方法2: 参考别的表,直接用语句,右键-DDL and Sources- 然后直接在控制台修改 修改后直接运行,表就建好了 2.备份表 先用上面 ...
- 可参数化的带优先级的数据选择器的FPGA实现方式探讨
在FPGA设计中,大部分情况下我们都得使用到数据选择器.并且为了设计参数化,可调,通常情况下我们需要一个参数可调的数据选择器,比如M选1,M是可调的参数. 如果,数据选择器是不带优先级的,我们可以使用 ...
- PMP:6.项目进度管理
项目管理包括为项目管理项目按时完成所需的各个过程:
- 剑指offer面试题23:从上到下打印二叉树(树的层序遍历)
题目:从上往下打印出二叉树的每个节点,同一层的结点按照从左往右的顺序打印. 解题思路:二叉树的层序遍历,在打印一个节点的时候,要把他的子节点保存起来打印第一层要把第二层的节点保存起来, 打印第二层要把 ...
- 手工检测SQL注入漏洞
SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,具体来说,它是利用现有应用程序将(恶意的)SQL命令注入到后台数据库引擎执 ...
- SQL参数放在where前后的区别
本博客记录一个细节,在使用sql left join时候,参数放在left join后面当条件,还是放在where后的区别 给出两条SQL: tt.book_type = 'TIPS_TYPE',放在 ...
- C语言中assert()断言函数的概念及用法
断言函数的格式如下所示: void assert (int expression);如果参数expression等于零,一个错误消息将会写入到设备的标准错误集并且会调用abort函数,就会结束程序的执 ...
- ubuntu双网卡配置,实现内网外网同时访问!
我们假定内网IP为:10.35.0.58,内网网关为:10.35.0.254:外网IP为222.76.250.4,外网网关为:222.76.250.1.其中局域名网需要连接:10.35.0.X,10. ...
- tensorflow 1.0 学习:池化层(pooling)和全连接层(dense)
池化层定义在 tensorflow/python/layers/pooling.py. 有最大值池化和均值池化. 1.tf.layers.max_pooling2d max_pooling2d( in ...
- Kubernetes 基于 Metrics Server 与 HPA 的使用
在 Kubernetes 中可以手动通过 kubectl scale 命令或通过修改 replicas 数量,可以实现 Pod 的扩容或缩容.Kubernetes 中还提供了 HPA(Horizont ...