\n","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Nikita Barsukov","url":"https://explain.codes//author/nikita-barsukov"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2024-08-04T18:00:02.034Z","dateModified":"2024-08-23T19:26:23.031Z"}
Explain Codes LogoExplain Codes Logo

Styling an input type="file" button

html
file-input
css-pseudo-elements
accessibility
Nikita BarsukovbyNikita Barsukov·Aug 4, 2024
TLDR

Here's a quick-and-dirty solution. To style a input[type="file"], create a disguise for your file input using a label. Hide the real input with CSS and stylize the label to look like a button.

<style> /* Secret agent level "file input" disguise */ .custom-file-upload { display: inline-block; padding: 6px 12px; cursor: pointer; background-color: #f8f8f8; border: 1px solid #ccc; border-radius: 4px; /* Makes file input look like an average button, nothing to see here... */ } </style> <!-- The real file input, hiding in the shadows --> <input type="file" id="file-upload" style="display: none;" /> <!-- The pretty face of our hidden file input --> <label for="file-upload" class="custom-file-upload">Choose a file</label> <script> /* Let's make it functional, shall we? */ document.querySelector('.custom-file-upload').onclick = function () { /* Surprise! Plot twist here, we're clicking the hidden file input */ document.getElementById('file-upload').click(); }; </script>

Maintaining browser compatibility and user interaction

The browser compatibility saga continues! Different browsers render file inputs in their own way. The above approach helps maintain a consistent look and user experience across multiple browsers.

We can spice things up a bit more, using CSS pseudo-elements like ::file-selector-button to directly style the button of the file input in modern browsers.

Speaking of interactions (no, not that awkward conversation with your boss), add some JavaScript to change the label text based on the selected file name.

/* Who's that file? It's ... */ const fileInput = document.getElementById('file-upload'); const fileLabel = document.querySelector('.custom-file-upload'); fileInput.onchange = () => { /* Suddenly reveal the file name like a magician pulling a rabbit out of a hat */ fileLabel.textContent = fileInput.files.length ? fileInput.files[0].name : 'Choose a file'; };

Add a dash of CSS for hover effects and transitions to make the button interaction smooth like a good pickup line.

/* File input button: "Are you a magician? Because whenever I look at you, everyone else disappears." */ .custom-file-upload:hover { background-color: #e8e8e8; transition: background-color 0.2s; }

Handle form submissions with JavaScript and event.preventDefault(). This can be handy when you need some control before surrendering the form data to the server.

document.querySelector('form').onsubmit = (event) => { /* Yelling "Freeze!" at your form submission */ event.preventDefault(); /* Do all the checks here. If it passes, call it Marvel because it's going into the endgame now */ };

Advanced styling and accessibility tips

Control input sizing

Need to control the size of the input field? Use the size attribute. It sets the width of the input to a specific number of characters.

<input type="file" id="file-upload" style="display: none;" size="0" />

Ensure accessibility

In the words of Uncle Sam, we want "accessibility for all!". Use ARIA attributes or offscreen techniques to keep the input accessible for screen readers.

/* The hiding-in-plain-sight technique for our secret agent "file input" */ input[type="file"] { opacity: 0.01; position: absolute; left: -999em; }

Tackling browser quirks

Not all browsers are alike. Some are more particular than others (yes, we're looking at you, IE!). Browser-specific pseudo-elements like ::-webkit-file-upload-button can give you extra control. Nonetheless, do check your solution across different browsers.

Validation and hidden inputs

If a hidden input falls in the form, does it make a sound? Well, some validation tools might ignore inputs hidden with display: none. Validate such inputs using JavaScript or use other CSS methods to hide them that don't affect their detectability.