自定义文件输入样式

Avatar of Chris Coyier
Chris Coyier

如果你对 Webkit/Blink/Chrome 特定的样式感兴趣,有一个专有的伪元素可以隐藏,然后使用一个同样非标准的伪元素-on-an-input

<input class="custom-file-input" type="file">
.custom-file-input::-webkit-file-upload-button {
  visibility: hidden;
}
.custom-file-input::before {
  content: 'Select some files';
  display: inline-block;
  background: linear-gradient(top, #f9f9f9, #e3e3e3);
  border: 1px solid #999;
  border-radius: 3px;
  padding: 5px 8px;
  outline: none;
  white-space: nowrap;
  -webkit-user-select: none;
  cursor: pointer;
  text-shadow: 1px 1px #fff;
  font-weight: 700;
  font-size: 10pt;
}
.custom-file-input:hover::before {
  border-color: black;
}
.custom-file-input:active::before {
  background: -webkit-linear-gradient(top, #e3e3e3, #f9f9f9);
}

演示

公平警告:它不会向您显示所选的文件名,但您可能可以对其进行调整以实现这一点。我发现通常情况下,这些天您会在文件选择后触发一个事件,并以这种方式获取数据。

WTF 表单

始终值得查看 WTF 表单 的实现方式

您可能还对 如何使用 CSS 样式化常见表单元素表单输入验证 在 DigitalOcean 上感兴趣。