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

在做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定位,也不需要通过事件绑定。

本文效果如下:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,800评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,900评论 25 709
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,687评论 1 92
  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 8,676评论 1 41
  • 信息时代,想要了解点什么新闻已经不像以前非要买份报纸或者坐在电视机旁,只要拿着手机,任何时候全世界最新最热门...
    世子说阅读 1,937评论 0 0