让原有的input file样式消失以后,怎么美化
简单一点,谢谢各位大神
参考GPT和自己的思路:
要实现 input file 的 CSS 美化,可以使用伪元素和背景图片来模拟 input file 框和按钮。具体操作如下:
input[type="file"] {
display: none;
}
input[type="file"] + .upload-btn:before {
content: "选择文件";
display: inline-block;
padding: 5px 10px;
border: 1px solid #ccc;
border-radius: 3px;
background-color: #fff;
background-image: url(upload.png);
background-repeat: no-repeat;
background-position: center;
cursor: pointer;
}
input[type="file"] + .upload-btn:after {
content: "没有选择文件";
display: inline-block;
margin-left: 10px;
padding: 5px 10px;
border: 1px solid #ccc;
border-radius: 3px;
background-color: #fff;
cursor: pointer;
}
input[type="file"] + .upload-btn:before,
input[type="file"] + .upload-btn:after {
vertical-align: middle;
}
input[type="file"]:focus + .upload-btn:before,
input[type="file"]:focus + .upload-btn:after {
outline: none;
border-color: #4d90fe;
}
input[type="file"]:focus + .upload-btn:before {
border-color: #4d90fe;
}
input[type="file"] + .upload-btn input[type="file"] {
display: none;
}
input[type="file"] + .upload-btn:before {
content: "上传文件";
}
input[type="file"] + .upload-btn:after {
content: "未选择文件";
}
input[type="file"] + .upload-btn input[type="file"]:hover,
input[type="file"] + .upload-btn input[type="file"]:focus {
cursor: pointer;
}
最终的 HTML 代码如下:
<div class="upload-btn">
<input type="file" name="file" id="file" />
</div>
其中 upload.png 是自定义的图片,可以替换成自己的图片。