如何使用 Bootstrap 5 更改“选择文件”文本

Mor*_*ahi 12 html css file-upload bootstrap-5

是否不可能像 Bootstrap 4 一样使用 CSS 更改 Bootstrap 5 中输入文件的选择文件文本?

如何为“未选择文件”添加边距?

在此输入图像描述

Gle*_*sky 21

Bootstrap 5 + 一些 CSS

  1. 使用自定义标签编写 Bootstrap 的自定义文件输入
  2. 使用伪元素隐藏默认Choose file按钮。对于旧版本的 Chrome/Opera/Safari 和 Edge/IE 分别::file-selector-button有伪元素 和::-webkit-file-upload-button::-ms-browsebootstrap.css仅使用::file-selector-button::-webkit-file-upload-button。所以我建议也这样做。
  3. :hover为效果以及标签和输入字段之间的细边框添加更多 CSS 。

在 Chrome、Firefox 和 Edge 中进行了测试。

https://codepen.io/glebkema/pen/VwMQWGE

.custom-file-button input[type=file] {
  margin-left: -2px !important;
}

.custom-file-button input[type=file]::-webkit-file-upload-button {
  display: none;
}

.custom-file-button input[type=file]::file-selector-button {
  display: none;
}

.custom-file-button:hover label {
  background-color: #dde0e3;
  cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container py-3">

  <div class="input-group custom-file-button">
    <label class="input-group-text" for="inputGroupFile">Your Custom Text</label>
    <input type="file" class="form-control" id="inputGroupFile">
  </div>

</div>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css">
Run Code Online (Sandbox Code Playgroud)


Zim*_*Zim 0

Bootstrap 5 beta 不再提供像 Bootstrap 4 这样的自定义文件输入,使用伪元素来覆盖浏览器的文件输入默认值。因此,您必须使用 JS 或制作自己的 psuedo 元素来隐藏Choose file输入的 .. 区域...

#formFile::before {
  content: "Pick file";
  position: absolute;
  z-index: 2;
  display: block;
  background-color: #eee;
  width: 80px;
}
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="container min-vh-100 py-2">
  <div class="row">
    <div class="col">
      <div class="mb-3">
        <input class="form-control" type="file" id="formFile">
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)