Discussion on style customization and browser compatibility issues when using input element [type="file"]

Discussion on style customization and browser compatibility issues when using input element [type="file"]
I encountered such a problem when doing the written test questions of Baixing.com these two days. I used the new features of HTML5 to implement an existing module of Baixing.com. After browsing Baixing.com for a while, I finally chose the form module used for publishing information. The reason is very simple. There are many new features for forms in HTML5. These new features are also very practical. After all, there are too many places with forms, such as registration, login, posting... (Hey, I'm a little off topic.)


At this time, I saw this element in the form on the original web page


My first reaction was, ha, it's just an input element, I can customize the style with CSS, then I naturally prepared to "right click" - "inspect element" to see the specific style of Baixing.com and found out later...


I must have opened it incorrectly... In this case, of course I have to do it myself. One thing I can confirm is that this form control for uploading files must use input[type="file"]. OK, just add this line of code:

Copy code
The code is as follows:

<input type="file" />

Refresh in the Chrome browser:


There is no doubt that this is the default style, and I found that this default style is difficult to modify. The most annoying thing is that different browsers have different default styles. It is very clear from a picture on the Internet:


(So ​​I say, you browsers are not obedient at all, and you don’t communicate well with each other. You are so high up, but the front-end students are suffering.)
However, the solution is still easy to think of. Use an element to wrap the input, add other required elements to the element, and set the style to achieve the desired effect. Set the position value of the input element to absolute to fill the outer elements, and then make the input transparent.
The HTML code is as follows:

Copy code
The code is as follows:

<div id="input-file">
<span id="text">Click to upload</span>
<input id="file" type="file" />
</div>

The corresponding CSS code is as follows:

Copy code
The code is as follows:

#input-file {
position: relative; /* Ensure the positioning of child elements*/
width: 120px;
height: 30px;
background: #eee;
border: 1px solid #ccc;
text-align: center;
cursor: pointer;
}
#text {
display: inline-block;
margin-top: 5px;
color: #666;
font-family: "黑体";
font-size: 18px;
}
#file {
display: block;
position: absolute;
top: 0;
left: 0;
width: 120px; /* The width and height should be consistent with the surrounding elements*/
height: 30px;
opacity: 0;
-moz-opacity: 0; /* Compatible with older browsers */
filter: alpha(opacity=0); /* IE compatible */
}

The display effect is as shown below:

kimoji......
However, there is still a bug here. When a button is made like this, it should be clickable when the mouse hovers over it. But even if the cursor: pointer; attribute is added to all elements, some areas will still be displayed as pointers. Is there any expert who can solve this problem?

<<:  Solution to the error reported by Mysql systemctl start mysqld

>>:  Detailed explanation of value transfer between parent and child components in Vue3

Recommend

Introduction to document.activeELement focus element in JavaScript

Table of contents 1. The default focus is on the ...

Resolving MySQL implicit conversion issues

1. Problem Description root@mysqldb 22:12: [xucl]...

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has ...

MySQL prepare principle detailed explanation

Benefits of Prepare The reason why Prepare SQL is...

How to click on the a tag to pop up the input file upload dialog box

html Copy code The code is as follows: <SPAN cl...

Example code for implementing auto-increment sequence in mysql

1. Create a sequence table CREATE TABLE `sequence...

Modify the boot time of grub in ubuntu

The online search to modify the grub startup time...

Mount the disk in a directory under Ubuntu 18.04

Introduction This article records how to mount a ...

Detailed explanation of MySQL database (based on Ubuntu 14.0.4 LTS 64 bit)

1. Composition and related concepts of MySQL data...

Several CSS3 tag shorthands (recommended)

border-radius: CSS3 rounded corners Syntax: borde...

Solve MySQL login error: 'Access denied for user 'root'@'localhost'

First of all, I don't know why I can't lo...