自定义文件上传的按钮的样式css+js

核心就是一段css遮住了原生的input框,然后用js将文件的值传入到另一个指定的input框中

 

 

 

原文链接

http://geniuscarrier.com/how-to-style-a-html-file-upload-button-in-pure-css/?utm_source=ourjs.com

 

How to Style a HTML file upload button in Pure CSS

12 June 2013 on css

Styling a html file upload button in pure css could be cumbersome if you've ever tried. Take a look at the following screenshot about how different browsers deal with the upload button. It's pretty obvious that there is a fair amount of variation.

We are aiming for creating a neat file upload button which behaves finely and consistently in pure css cross browsers. And here we go:

Step 1. Create a simple html markup

<div class="fileUpload btn btn-primary">
    <span>Upload</span>
    <input type="file" class="upload" />
</div>

Step 2. CSS: Tricky Part

.fileUpload {
    position: relative;
    overflow: hidden;
    margin: 10px;
}
.fileUpload input.upload {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    padding: 0;
    font-size: 20px;
    cursor: pointer;
    opacity: 0;
    filter: alpha(opacity=0);
}

For simplicity, I am using Bootstrap CSS to style the button (div.file-upload).

Demo:


Upload button with selected file

Unfortunately there is no PURE CSS way to do it. However, if you really want to display the selected file, the following JavaScript snippet could help for this case.

JavaScript:

document.getElementById("uploadBtn").onchange = function () {
    document.getElementById("uploadFile").value = this.value;
};

DOM change

<input id="uploadFile" placeholder="Choose File" disabled="disabled" />
<div class="fileUpload btn btn-primary">
    <span>Upload</span>
    <input id="uploadBtn" type="file" class="upload" />
</div>

Demo:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用如下的 HTML 和 CSS 代码创建一个类似 Element UI 单选框的按样式: HTML 代码: ```html <label class="radio-btn"> <input type="radio" name="radio-group" value="option-1"> <span class="radio-btn-label">Option 1</span> </label> <label class="radio-btn"> <input type="radio" name="radio-group" value="option-2"> <span class="radio-btn-label">Option 2</span> </label> <label class="radio-btn"> <input type="radio" name="radio-group" value="option-3"> <span class="radio-btn-label">Option 3</span> </label> ``` CSS 代码: ```css .radio-btn { display: inline-block; margin-right: 20px; position: relative; } .radio-btn input[type="radio"] { position: absolute; opacity: 0; } .radio-btn-label { display: inline-block; width: 24px; height: 24px; border-radius: 50%; border: 2px solid #ccc; position: relative; cursor: pointer; vertical-align: middle; margin-right: 8px; } .radio-btn input[type="radio"]:checked + .radio-btn-label:after { content: ''; display: block; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 12px; height: 12px; border-radius: 50%; background: #007aff; } ``` 这段代码将会创建 3 个单选框按,每个按都由一个 label 和两个元素 (input 和 span) 组成。其中,input 元素用来获取用户选择,而 span 元素包含了 label 的文本内容。 要自定义样式,你可以修改 .radio-btn-label 和 .radio-btn input[type="radio"]:checked + .radio-btn-label:after 这两个 CSS 类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值