Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input type file - wrap long file name text

Tags:

html

css

Is it possible to wrap the file name text that appears beside the browse button?

I have a simple upload button

<input type="file" />

I'm uploading a file with a very long name, you can see that a horizontal scrollbar has appeared to enable reading the full text:

enter image description here

I've tried tinkering with the css but it adds an ellipsis and i can't figure out a way to show the full file name without horizontal scrollbars.

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

enter image description here

thanks

like image 788
Scott Avatar asked Jun 28 '18 08:06

Scott


People also ask

How do you insert a file in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do I change the file upload button in CSS?

Use a label tag and point its for attribute to the id of the default HTML file upload button. By doing this, clicking the label element in the browser toggles the default HTML file upload button (as though we clicked it directly).


1 Answers

This is only possible with JavaScript. A solution might look like this:

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">
like image 143
mfluehr Avatar answered Oct 21 '22 12:10

mfluehr