Uploading files using a web form is a common task that is often hampered by the unfriendly and rigid nature of the HTML element. input
of type files
. However, with some CSS tricks and a little JavaScript, you can transform a boring and generic file input field into an attractive component consistent with the style of your website.
Table of Contents
ToggleGetting started with the Basic File Type
When you enter an element input
with the attribute type="file"
in your HTML form, you get a predefined user experience that varies by browser, but typically includes a 'Browse…' button. and a text field for the name of the uploaded file. This default appearance, although functional, usually clashes with the personalized design of your page.
Creating a Stylized Load Button
To start customizing, we will hide the input
of default file using CSS, and we will design a button or loading area of our own. We can do it as follows:
.inputfile { width: 0.1px; height: 0.1px; opacity: 0; overflow: hidden; position: absolute; z-index: -1; } .inputfile + label { cursor: pointer; /* "Hand" cursor */ /* Additional styles to make this look like a button */ }
The trick here is to assign a labels
to the input
with the same id
, and design that labels
as if it were the button.
Implementing Visual Feedback
Once hidden input
original, it is essential to provide feedback to the user. We want to display the name of the selected file once one has been chosen. The easiest way to do this is with a little JavaScript.
var input = document.getElementById('file'); var label = document.querySelector('label[for="file"]'); input.addEventListener('change', function(e) { var fileName = ''; if (this.files && this.files.length > 1) fileName = ( this.getAttribute('data-multiple-caption' ;) || '' ).replace('{count}', this.files.length); (fileName) label.innerHTML = fileName; else label.innerHTML = 'Choose a file';
The above code listens to the event change
of the input
, obtains the name of the selected file and updates the corresponding label, thus offering the user immediate feedback.
Moving Forward with Cross-Browser Styles
Working with custom styles becomes complicated when we consider the difference between how browsers handle the input
of type files
. To make sure our custom interface looks consistent across browsers, we often need to resort to browser-specific tricks. This is where the developer's expertise comes into play, determining the required hacks, such as browser-specific prefixes, and fallback techniques for an inclusive experience.
Improving the Drag & Drop Experience
We can take our customization a step further by implementing a file drag and drop feature. This is a more advanced feature and requires a bit more JavaScript and some additional events to take into account, such as dragover
y drop
.
var dropArea = document.getElementById('drop-area'); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => { dropArea.addEventListener(eventName, preventDefaults, false }); ); function preventDefaults(e) { e.preventDefault(); e.stopPropagation(); } dropArea.addEventListener('drop', handleDrop, false); function handleDrop(e) { var dt = e.dataTransfer; var files = dt.files; handleFiles(files); } function handleFiles(files) { ([...files]).forEach(uploadFile); } function uploadFile(file) { // Handle file upload }
This code gives the user the ability to drag files directly to a designated area on the page, improving the interactivity and usability of the form.
Conclusion
With these techniques, your file upload field can integrate perfectly with the style of your website. Furthermore, the improved user experience not only makes it visually appealing but also more accessible and easier to use.
The code presented is a starting point. For larger projects or specialized applications, the needs may be more complex, and you may need to add additional logic or integrate with frameworks like React or Vue.js to handle file uploads.
I hope this tutorial helped you take your file upload forms to the next level. Don't hesitate to contact me through my Contact Form If you have any question. I also invite you to visit my main website where you can find more tutorials, tips and tricks for your web development projects.
Happy coding!