原生HTML5 input type=file按钮UI自定义

时间:2020-11-28 22:11:27

原生<input type="file" name="file" />长得太丑

原生HTML5 input type=file按钮UI自定义

提升一下颜值

原生HTML5 input type=file按钮UI自定义

实现方案一、设置input[type=file]透明度为0,使用绝对定位遮罩在自定义的按钮标签层的之上.
 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>原生HTML5 input type=file按钮UI自定义</title>
<style>
.file_upload_box {
display: inline-block;
width: 200px;
height: 60px;
position: relative;
overflow: hidden;
}
.file_upload_box input[type=file] {
position: absolute;
left: 0;
top: 0;
width: 100%;
line-height: 60px;
opacity: 0;
cursor: pointer;
}
.file_upload_box a {
display: inline-block;
width: 100%;
line-height: 45px;
text-align: center;
font-family: "Microsoft yahei";
background-color: #f60;
color: #FFF;
font-weight: 700;
text-decoration: none;
}
</style>
</head>
<body>
<div class="file_upload_box">
<input type="file" name="file" />
<a href="#none">上传文件</a>
</div>
</body>
</html>
推荐实现方案二、利用label的for属性(for 属性规定 label 绑定到哪个表单元素)
 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>原生HTML5 input type=file按钮UI自定义</title>
<style>
.ui_button {
display: inline-block;
line-height: 45px;
padding: 0 70px;
color: #FFFFFF;
font-family: "微软雅黑";
font-weight: 700;
cursor: pointer;
}
.ui_button_primary {
background-color: #FF6600;
}
label.ui_button:hover {
background-color: #d46216;
}
</style>
</head>
<body>
<label class="ui_button ui_button_primary" for="xFile">上传文件</label>
<form><input type="file" id="xFile" style="position:absolute;clip:rect(0 0 0 0);"/></form>
</body>
</html>