这是一个纯 CSS、无 Javascript、无引导、100% 跨浏览器的解决方案!只需剪切和粘贴一组样式,然后测试您的文件上传控件。
此解决方案不会像这里的其他帖子那样尝试隐藏然后重新创建原始 HTML 元素。它使用plain CSS 没有任何马戏团技巧或第三方工具来为所有主要浏览器设置原始文件上传表单控件的样式。您甚至不需要更改 HTML 代码!
这是使用下面的 CSS 在 Firefox、Chrome 和 Edge 中文件上传控件的外观。这是一个非常简单干净的设计。您可以将其更改为您喜欢的任何外观:
Internet Explorer 为您提供了有限的设计控制,但至少您可以使用 CSS 来操作控制以更改一些内容,包括圆角边框和颜色:
<style>
/* Note: This CSS will style all instances of
<input type=file /> controls in your website. */
input[type="file"],
input[type="file"]:visited,
input[type="file"]:hover,
input[type="file"]:focus,
input[type="file"]:active {
margin:0;
padding: 0em 0em;
padding: 0rem 0rem;
overflow: hidden; /* long file names overflow so just hide the end */
background: #ffffff;
border-radius: .2em;
border-radius: .2rem;
outline: none;
border: 2px solid #bbb;
cursor: pointer;
-webkit-appearance: textfield;
-moz-appearance: textfield;
}
input[type="file"]:hover {
background: #f9f9ff; /* I am using a light blue to indicate an interaction */
border: 2px solid #999;
}
input[type="file"]:visited,
input[type="file"]:focus,
input[type="file"]:active {
background: #fff; /* Default back to white when focused. */
border: 2px solid #999;
}
/* Note: Firefox flags the file name box as a *readonly* input. So that attribute selector was added below. Note: These selectors blow up in IE so have to be separated from the same styles above. */
input[type="file"]:disabled,
input[type="file"]:read-only {
margin: 0;
padding: 0em 0em;
padding: 0rem 0rem;
overflow: hidden; /* long file names overflow so just hide the end */
background: #ffffff;
border-radius: .2em;
border-radius: .2rem;
outline: none;
border: 2px solid #bbb;
cursor: pointer;
-webkit-appearance: textfield;
-moz-appearance: textfield;
}
input[type="file"]:disabled:hover,
input[type="file"]:read-only:hover {
background: #f9f9ff; /* I am using a light blue to indicate an interaction */
border: 2px solid #999;
}
input[type="file"]:disabled:visited,
input[type="file"]:disabled:focus,
input[type="file"]:disabled:active,
input[type="file"]:read-only:visited,
input[type="file"]:read-only:focus,
input[type="file"]:read-only:active {
background: #fff; /* Default back to white when focused. */
border: 2px solid #999;
}
/* IE UPLOAD BUTTON STYLE: This attempts to alter the file upload button style in IE. Keep in mind IE gives you limited design control but at least you can customize its upload button.*/
::-ms-browse { /* IE */
display: inline-block;
margin: 0;
padding: .2em .5em;
padding: .2rem .5rem;
text-align: center;
outline: none;
border: none;
background: #fff;
white-space: nowrap;
cursor: pointer;
}
/* FIREFOX UPLOAD BUTTON STYLE */
::file-selector-button {/* firefox */
display: inline-block;
margin: 0rem 1rem 0rem 0rem;
padding: .18em .5em;
padding: .18rem .5rem;
-webkit-appearance: button;
text-align: center;
border-radius: .1rem 0rem 0rem .1rem;
outline: none;
border: none;
border-right: 2px solid #bbb;
background: #eee;
white-space: nowrap;
cursor: pointer;
}
/* CHROME AND EDGE UPLOAD BUTTON STYLE */
::-webkit-file-upload-button { /* chrome and edge */
display: inline-block;
margin: 0rem 1rem 0rem 0rem;
padding: .19em .5em;
padding: .19rem .5rem;
-webkit-appearance: button;
text-align: center;
border-radius: .1rem 0rem 0rem .1rem;
outline: none;
border: none;
border-right: 2px solid #bbb;
background: #eee;
white-space: nowrap;
cursor: pointer;
}
</style>
我的解决方案的优点是:
- 您坚持使用简单的 CSS 来设置原始 HTML 输入控件的样式
- 您可以在文件输入文本框中看到一个或多个文件名
- 屏幕阅读器和支持 ARIA 的设备可以与您的文件上传控件正常交互
- 您可以在 HTML 元素上设置
tabindex,使其成为 Tab 键顺序的一部分
- 因为您使用的是纯 HTML 和 CSS,您的 文件输入按钮在新旧浏览器中都能完美运行
- 需要零 JavaScript!
- 即使在最古老的浏览器中也能快速运行和加载
- 由于您没有使用“display:none”来隐藏控件,因此在任何已知的旧或新浏览器版本中都不会禁止其文件块流数据到达服务器
您不需要 goofy JavaScript 技巧、Bootstrap 或尝试隐藏/重新创建文件输入控件。这只会破坏在线每个人的可用性。为原始 HTML 控件设置样式意味着您的文件上传控件保证在 25 年的新旧网络浏览器中都能正常工作。
这就是为什么您不能相信所有这些脚本化的黑客攻击,它们会擦除、重写或破坏 HTML,只是为了尝试重新创造一些视觉体验。这表明您不了解 HTML 是如何使用的,或者为什么它存在 30 年几乎没有变化。您永远不应该尝试重写 HTML 的原生表单控制功能。为什么?在网站中使用自然 HTML 不仅仅是为了一些强制的视觉体验而操纵标记。这些被替换的 HTML 元素中有限的视觉设计的权衡是出于某种原因而设计的。
我的建议:坚持使用简单的 HTML 和 CSS 解决方案,作为 Web 开发人员,您将遇到零问题。