Change default text in input type="file"?
To replace the default text in input[type="file"]
, hide the original input with CSS, then add a custom button that triggers the file selection. Here's a simplified version:
This hides the original file input tag using CSS, a button element is then styled as you see fit. The onclick
event on the button fires the hidden file input.
A practical guide to customizing file inputs
Step 1: The label-force awakens
To start, pair a label
with the input
using the for
attribute. This ensures accessibility:
Step 2: Becoming a CSS Wizard
Apply CSS magic to transform the label
into a button:
Step 3: Power up with JavaScript
Fetch and display the originally uploaded file name with JavaScript to provide users with instant feedback:
Step 4: Frameworks are friends, not foes
Bootstrap or Ratchet can help maintain consistent styling across different browsers and devices.
Minding the bits and pieces
Keep it user-friendly
Always mimic native input behavior to maintain user familiarity with the interaction.
Adapt to different screen sizes
Make sure your customized button looks good and performs well on all devices.
Display selected files
Use event listeners to capture uploaded file names and display them to users.
Blend with overall design
Matching the custom button with your application's design language ensures a seamless user experience.
Was this article helpful?