How to Style File Input in CSS
The native file input is notoriously hard to style. Learn the label technique to create beautiful custom upload buttons and drag-drop zones.
The Problem
Native file inputs look different in every browser and are nearly impossible to style directly. The solution is to hide the input and style a label element instead.
Native file input (try styling this!):
Solution: Hidden Input + Styled Label
Live Preview
Drag and Drop Zone
Live Preview
Key Points
- +Use
sr-onlyor similar to hide the input accessibly - +The label's
forattribute must match the input'sid - +Use
:focus-withinon the wrapper for keyboard focus styling - +JavaScript is needed to display the selected file name
Frequently Asked Questions
Browser security prevents direct styling of file inputs to protect users. The file name display and browse button are rendered by the browser, not CSS. The workaround is to hide the input and style a label that triggers it.
Yes, when done correctly. The input remains in the DOM (just visually hidden), so screen readers can access it. The label's for attribute connects it to the input, and keyboard focus works normally.
You need JavaScript to display the file name. Listen for the 'change' event on the input, then display input.files[0].name in a span or update the label text.
More CSS Tutorials
Copy CSS from Website
Extract styles with 2 clicks
Center a Div (CSS)
Flexbox, Grid, and more
Center a Div (Tailwind)
Utility classes for centering
Convert CSS to Tailwind
CSS to utility classes
Glassmorphism Effect
Frosted glass UI style
Neumorphism Button
Soft UI button design
Responsive Navbar
Mobile-friendly navigation
Sticky Header
Fixed navigation on scroll
CSS Pagination
Page navigation styles
CSS Accordion
Expandable content sections
Dropdown Menu
Hover and click dropdowns
CSS Modal
Popup dialog boxes
Toast Notifications
Animated alert messages
Hamburger Menu
Animated menu icon
Animate Gradients
Moving gradient backgrounds
Skeleton Loader
Loading placeholder UI
Element Screenshot
Capture any element as image
Pick Color from Website
Eyedropper tool comparison
Identify Fonts
Find fonts on any website
Download All Images
Bulk save images from any site
Measure Elements
Page ruler and measurements
Extract Colors from Website
Get any color palette instantly
Dark Mode Toggle
CSS variables and localStorage
Responsive Grid
CSS Grid auto-fit and minmax
Center Text in CSS
text-align, Flexbox, and Grid
Make Text Bold
font-weight values explained
Add Shadow in CSS
box-shadow, text-shadow, drop-shadow
Round Corners in CSS
border-radius and pill shapes
