Customize the style of the <input type="file"> element used when uploading files in HTML

Customize the style of the <input type="file"> element used when uploading files in HTML

In a web page, the <input type="file"> element is basically used when uploading files. Its default style is:

Under Chrome:

In IE:

Regardless of which of the above, the style is relatively simple and does not coordinate with the style of many web pages.

There are many occasions to change the display style according to user needs and design style.

If you want to make a bootstrap-style upload button like the following, how can you implement it?

Basic elements needed to build an upload button


Copy code
The code is as follows:
<span> <span>Upload</span> <input type="file"> </span>

Effect (chrome):

What you see now is displayed in two lines.

The reason why the periphery is not replaced with a div is that in IE7- browser, as long as it is not set to inline, its width will be expanded to the maximum possible width. If it is set to inline, the width of the element cannot be adjusted, so using span and setting it to inline-block can solve this problem.

Add style to turn two lines into one line


Copy code
The code is as follows:
<span"> <span>Upload</span> <input type="file"> </span>

css:


Copy code
The code is as follows:
.fileinput-button { position: relative; display: inline-block; } .fileinput-button input{ position: absolute; right: 0px; top: 0px; }

Effect:

There is no light blue border by default. It will only be displayed after clicking the mouse. It is displayed here for clear viewing.

By setting the outer span to display:relative and the input to display:absolute, they are all out of the document flow.

By limiting the input to the outer span and performing absolute positioning, the original two-line display is changed to a single line display.

In fact, it has overflowed here. The real width is the width of the "Upload" text. Modify the fileinput-button style to add overflow: hidden


Copy code
The code is as follows:
.fileinput-button { position: relative; display: inline-block; overflow: hidden; }

Effect:

Very interesting. You can see the blue border on the top and right, which actually hides the overflow on the left and bottom.

At this time, clicking the word "Upload" with the mouse is actually clicking on the input, which can display the "Open" dialog box, because the input is closer to the user than "Upload" in the display hierarchy.

Note the right in the input positioning, why not use left positioning.

When we change to left.

Effect (chrome):

Effect (IE):

In Chrome, the selection button in the input element is exposed, but it doesn’t matter, you can make it transparent by setting transparency later.

However, the input box will be exposed in IE. The key is that when the mouse moves to the input box, the pointer will change to the input state, which is very difficult to handle.

By moving the input box to the left using the right positioning method, you can avoid the mouse pointer turning into input state in IE.

Transparent input element

CSS:


Copy code
The code is as follows:
.fileinput-button { position: relative; display: inline-block; overflow: hidden; } .fileinput-button input{ position: absolute; left: 0px; top: 0px; opacity: 0; -ms-filter: 'alpha(opacity=0)'; }

Effect:

The input is completely gone, but clicking "upload" still works.

Can support IE8+.

Import bootstrap and add button styles

Add references to external css and js in head.


Copy code
The code is as follows:
<link rel="stylesheet" href="bootstrap/bootstrap.css"> <link rel="stylesheet" href="bootstrap/bootstrap-theme.css"> <script src="bootstrap/jquery-1.10.2.js"></script> <script src="bootstrap/bootstrap.js"></script>

Add button style.


Copy code
The code is as follows:
<span> <span>Upload</span> <input type="file"> </span>

Effect:

Solving the size problem

If you add width:100px to the fileinput-button style and set the outer span to 100px wide, you will find that there is no response when you click the bottom part. The reason is that the input is the default size and cannot cover the bottom part.

You can solve the overlap problem by setting a large font size for the input to expand it. Here we set it to 200px.


Copy code
The code is as follows:
.fileinput-button input{ position:absolute; right: 0px; top:0px; opacity: 0; -ms-filter: 'alpha(opacity=0)'; font-size: 200px; }

This will solve the coverage problem.

Finish.

Reference: jQuery-File-Upload

If you want to be compatible with IE7, you can refer to the writing method in jQuery-File-Upload.

Code:

XML/HTML CodeCopy content to clipboard
  1. <!DOCTYPE html >   
  2. < html >   
  3. < head >   
  4.      < title > </ title >   
  5.      < meta   http-equiv = "Content-Type"   content = "text/html;charset=utf-8" >   
  6.      < link   rel = "stylesheet"   href = "bootstrap/bootstrap.css" >   
  7.      < link   rel = "stylesheet"   href = "bootstrap/bootstrap-theme.css" >   
  8.      < script   src = "bootstrap/jquery-1.10.2.js" > </ script >   
  9.      < script   src = "bootstrap/bootstrap.js" > </ script >   
  10.      < style >   
  11. .fileinput-button {
  12. position: relative;
  13. display: inline-block;
  14. overflow: hidden;
  15. }
  16. .fileinput-button input{
  17. position:absolute;
  18. right: 0px;
  19. top: 0px;
  20. opacity: 0;
  21. -ms-filter: 'alpha( opacity = 0 )';
  22. font-size: 200px;
  23. }
  24.      </ style >   
  25. </ head >   
  26. < body   style = "padding: 10px" >   
  27.      < div   align = "center" >   
  28.          < span   class = "btn btn-success fileinput-button" >   
  29.              < span > Upload </ span >   
  30.              < input   type = "file" >   
  31.          </ span >   
  32.      </ div >   
  33. </ body >   
  34. </ html >   

<<:  Using vue3 to imitate the side message prompt effect of Apple system

>>:  Detailed explanation of the implementation example of group ranking in Mysql tutorial

Recommend

MySQL Practical Experience of Using Insert Statement

Table of contents 1. Several syntaxes of Insert 1...

Docker installs the official Redis image and enables password authentication

Reference: Docker official redis documentation 1....

The rel attribute of the HTML link tag

The <link> tag defines the relationship bet...

The meaning and calculation method of QPS and TPS of MySQL database

When doing DB benchmark testing, qps and tps are ...

Centos7 implements sample code for restoring data based on MySQL logs

Introduction Binlog logs, that is, binary log fil...

Three BOM objects in JavaScript

Table of contents 1. Location Object 1. URL 2. Pr...

How to solve the timeout during pip operation in Linux

How to solve the timeout problem when pip is used...

JavaScript implementation of classic snake game

This article shares the specific code of JavaScri...

CSS screen size adaptive implementation example

To achieve CSS screen size adaptation, we must fi...

Detailed explanation of VUE responsiveness principle

Table of contents 1. Responsive principle foundat...

JavaScript to implement the web version of the snake game

This article shares the specific code for JavaScr...