How to change the default upload file button style with plain HTML and CSS

Source: Internet
Author: User

If you've ever tried it, you'll know that it can be cumbersome to implement a unified upload file button with a pure CSS style and HTML. Take a look at the different browsers below. Obviously, they look very different.

Our goal is to create a concise, pure CSS implementation, in all browsers look and layout is the same as the upload file button. We can do this:

Step 1. Create a simple HTML tag

1234 < div class = "FileUpload btn btn-primary" > &NBSP;&NBSP;&NBSP;&NBSP; < span >upload</ span > &NBSP;&NBSP;&NBSP;&NBSP; < input type = "file" class = "upload" /> </ div >

2nd step: CSS: It's a little tricky.

12345678910111213141516 .fileUpload {    position: relative;    overflow: hidden;    margin: 10px;}.fileUpload input.upload {  position: absolute;    top: 0;    right: 0;    margin: 0;    padding: 0;    font-size: 20px;    cursor: pointer;    opacity: 0;    filter: alpha(opacity=0);}

For simplicity, I use a button (Div.file-upload) with the bootstrapcss style applied.

Demonstrate:

Upload button to display the selected file

Unfortunately, pure CSS does not do this. However, if you really want to display the selected file, the following JavaScript snippet can help you.

Javascript:

123 document.getElementById("uploadBtn").onchange = function() {    document.getElementById("uploadFile").value = this.value;};

Dom:

12345 <input id="uploadFile" placeholder="Choose File" disabled="disabled" /><div class="fileUpload btn btn-primary">    <span>Upload</span>    <input id="uploadBtn" type="file" class="upload" /></div>

Demonstrate:

Original address: geniuscarrier.com

How to change the default upload file button style with plain HTML and CSS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.