核心就是一段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:

自定义文件上传的按钮的样式css+js的更多相关文章

  1. CSS自定义文件上传按钮样式,兼容主流浏览器

    解决办法:使用text文本框及a链接模拟文件上传按钮,并且把文件上传按钮放在他们上面,并且文件上传按钮显示透明.​1.图片​​2. [代码][HTML]代码 <div class="b ...

  2. CSS自定义文件上传按钮

    今天一同事问我文件上传按钮的问题,情况是这样的,他页面上有3个按钮,分为左中右三个,左边的位按钮甲,右边的位按钮乙,而中间的就是个文件选择按钮,情况大概是这个样子的: 两边的按钮都有了样式,但中间的选 ...

  3. 再springMVC中自定义文件上传处理解决与原spring中MultipartResolve冲突问题

    相信很多朋友再用springmvc时都遇见了一个问题,那就是自带的获取上传的东西太慢,而且不知道如何修改,其实不然,spring框架既然给我们开放了这个接口,就一定遵从了可扩展性的原则,经过查看org ...

  4. 项目二、自定义文件上传函数(js函数)

    /** * 文件上传工具 v1.0 * @param file 要上传的文件 * @param url 要上传到的路径 * @param div 要显示的区域 */ function uploader ...

  5. strut2 自定义文件上传错误信息

    在文件上传过程中我们可以指定拦截器对文件类型.后缀名.大小进行设定,action中的配置: <interceptor-ref name="fileUpload"> &l ...

  6. AntDesign vue学习笔记(九)自定义文件上传

    第七节时提到,上传文件时实际可能需要传输一个token. 1.查看vue antdesign文档https://vue.ant.design/components/upload-cn/ 2.使用cus ...

  7. 文件上传input type="file"样式美化

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. Layui文件上传样式在ng-dialog不显示的问题处理

    1.项目业务改动,在一个弹窗页面加图片上传. 2.页面使用angular框架,图片上传使用layui的文件上传组件. js: layui.upload({ url: '/test/upload.jso ...

  9. ASP.NET MVC5+EF6+EasyUI 后台管理系统(56)-插件---单文件上传与easyui使用fancybox

    系列目录 https://yunpan.cn/cZVeSJ33XSHKZ  访问密码 0fc2 今天整合lightbox插件Fancybox1.3.4,发现1.3.4版本太老了.而目前easyui 1 ...

随机推荐

  1. SGU128 Snake

    SGU128,题意是给定N个点,问说能不能形成一个闭环G,要求G经过每个点,且在每个点处都有90度的转角,且不能出现自交. 没想出来,通过这提供的思路,由于每个点处都需要90度的转弯,因此每个点处必然 ...

  2. fork、vfork、clone区别

    在Linux中主要提供了fork.vfork.clone三个进程创建方法. 问题 在linux源码中这三个调用的执行过程是执行fork(),vfork(),clone()时,通过一个系统调用表映射到s ...

  3. Java [Leetcode 273]Delete Node in a Linked List

    题目描述: Write a function to delete a node (except the tail) in a singly linked list, given only access ...

  4. JAVA方法和本地方法(转载)

    转载自:http://blog.sina.com.cn/s/blog_5b9b4abe01016zw0.html JAVA中有两种方法:JAVA方法和本地方法   JAVA方法是由JAVA编写的,编译 ...

  5. CentOS 7 安装 tomcat7.0

    安装tomcat: [root@admin local]# cd /usr/local[root@admin local]# tar -zxv -f apache-tomcat-7.0.29.tar. ...

  6. 第一个MVC模式的程序

    数据库 是一个SQL sever数据库,结构很简单,创建名为firstMVC的数据库,只包含一个数据表(名称为Persons),其中共有三列,分别用于保存人员(persons)的ID.姓名以及创建日期 ...

  7. Javascript 知识点简介

    如何在HTML中引入JS? 所有重定向的HTML标签内都可以嵌入javascript代码. 浮点数不要用 == 来进行判断 var num=0;    for(var i=0;i<10;i++) ...

  8. pomelo环境搭建

    在ubuntu上搭建pomelo环境 一 安装node.js 不要直接安装nodejs, 因为ubuntu上默认的nodejs版本是0.6的太老千万不要下tar包自己安装, 问题很多正确方法如下:   ...

  9. codeforce 603B - Moodular Arithmetic

    题意:给出方程 f(kx%p)=kf(x)%p ,f:A->B,不同的映射函数f有几种,其中f,A,B值域为{0,1,2..p-1},p为素数(除了2),k为小于p的一个常数. 思路:明显是求循 ...

  10. Mac安装wget的两种方法

    ​ 第一种.传统的安装包 A - 从ftp://ftp.gnu.org/gnu/wget/下载到最新的wget安装包到本地 B - 然后通过终端tar -zxvf命令解压到我们某个目录 C - 然后依 ...