Remove browse button from input type=file

Summary

Developers often face challenges in styling the default "Choose File" button of an HTML input type="file" element, as browsers provide no direct option to remove it for UI consistency. A common technique to achieve a custom upload button involves layering. This method entails creating a standard input type="file" element and a separate, custom-styled overlay element. By positioning the overlay directly over the file input, setting the input's opacity to 0, and ensuring it has a higher z-index, the custom overlay becomes the visible and clickable component, while the hidden file input still triggers the native file selection dialog. This allows for full control over the visual appearance of the file upload interaction.

In Web 2.0 era, many websites will ask the user to upload their avatar when creating their profile. In HTML, the input control we should use is input="file". This is a file control provided by the browser vendor and it should be ran in a sandbox for security consideration. Browsers don't provide ways to choose file from local and upload to the remote server without the input="file" control. 

There is one annoying part about the input="file", which is a default button named "Choose File" or "Browser" will always appear when this control is created. Sometimes, to keep consistent and provide better user experience, you may don't want to see the browse button. However, the browser itself doesn't provide an option to remove this button. In this post, we will provide a simple way to remove this button and put your own style for this control.

The rationale behind this is to set the opacity of this control to transparent and add an overlay above the file control. Let's take a look at an example. First, creating the HTML elements needed.

<form action="/">
    <div class="avatar-zone">
    </div>   
 
    <input type="file" class="upload_btn"/>
    <div class="overlay-layer">Upload photo</div>
</form>

Below is CSS for the elements without the file control's opacity set to 0.

.avatar-zone{
	width:200px;
	height:200px;
	background-color:#CCCCCC;
}
.overlay-layer{
	width:200px;
	height:40px;
	position:absolute;
	margin-top:-40px;
	opacity:0.5;
	background-color:#000000;
	z-index:0;
	font-size:25px;
	color:#FFFFFF;
	text-align:center;
	line-height:40px;
}
.upload_btn{
	position:absolute;
	width:200px;
	height:40px;
	margin-top:-40px;
	z-index:10;
}

And here is what it looks like:

Now to remove the browse button, we add the CSS opacity:0. The HTML code now lloks like:

.avatar-zone{
	width:200px;
	height:200px;
	background-color:#CCCCCC;
}
.overlay-layer{
	width:200px;
	height:40px;
	position:absolute;
	margin-top:-40px;
	opacity:0.5;
	background-color:#000000;
	z-index:0;
	font-size:25px;
	color:#FFFFFF;
	text-align:center;
	line-height:40px;
}
.upload_btn{
	position:absolute;
	width:200px;
	height:40px;
	margin-top:-40px;
	z-index:10;
	opacity:0;
}

And now here is what it looks like:

Now when you click on "Upload photo", the dialog for choosing the file will appear.

The key here is that we have create two components: one is the file control and the other is the overlay layer. The overlay layer will just lay above the file control by setting its z-index to a value greater than file control's z-index. This is to ensure that the file control is one clicked when the mouse clicks over the overlay area.

HTML FILE BROWSE REMOVE BROWSE BUTTON

  RELATED

  COMMENTS

3
Anonymous
Dec 10, 2016 at 8:37 am

The file name doesn't show

Anonymous
Oct 16, 2018 at 10:15 am

I think in this case most people don't care about the filename anymore. Since it's using opacity, the filename will also disappear

john
Apr 13, 2020 at 10:03 pm

Nice Post!

I have just read and it helps me a lot in my current project. Thanks for posting useful information.

Thanks

web development