输入类型文件-包装长文件名文本

时间:2018-06-28 08:56:43

标签: html css

是否可以包装浏览按钮旁边显示的文件名文本?

我有一个简单的上传按钮

<input type="file" />

我正在上传一个文件名很长的文件,您可以看到出现了一个水平滚动条,可以读取全文:

enter image description here

我已经尝试过修改CSS,但是它添加了省略号,而且我想不出一种不用水平滚动条就能显示完整文件名的方法。

input[type="file"] {
    border: 1px solid red;
    white-space: normal;
    word-wrap: break-word;
    width: 200px;
    overflow: auto;
}

enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

这仅适用于JavaScript。一个解决方案可能看起来像这样:

function update(e) {
  fileName.textContent = file.files[0].name;
}

const file = document.querySelector('#file'),
      fileName = document.querySelector('.file-name');

file.addEventListener('change', update);
.file-button {
  background: #eee;
  border: 1px solid #aaa;
  border-radius: 3px;
  display: inline-block;
  padding: 2px 8px;
}

.file-name {
  width: 200px;
  border: 1px solid #aaa;
  overflow-wrap: break-word;
  padding: 2px;
}

input[type='file'] {
  display: none;
}
<label class="file-label" for="file">Profile picture:
  <div class="file-button">Browse&hellip;</div>
</label>
<p class="file-name">No file selected.</p>
<input id="file" type="file">

相关问题