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

React's method of realizing secondary linkage

This article shares the specific code of React to...

CSS3 to achieve timeline effects

Recently, when I turned on my computer, I saw tha...

Detailed examples of ajax usage in js and jQuery

Table of contents Native JS How to send a get req...

Detailed explanation of :key in VUE v-for

When key is not added to the v-for tag. <!DOCT...

Example of how to create a database name with special characters in MySQL

Preface This article explains how to create a dat...

Implementation of the login page of Vue actual combat record

Table of contents 1. Preliminary preparation 1.1 ...

Implementation of React star rating component

The requirement is to pass in the rating data for...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

Get the calculated style in the CSS element (after cascading/final style)

To obtain the calculated style in a CSS element (t...

Complete steps for using Nginx+Tomcat for load balancing under Windows

Preface Today, Prince will talk to you about the ...

Linux CentOS 6.5 Uninstall, tar and install MySQL tutorial

Uninstall the system-provided MySQL 1. Check whet...

Sharing of experience on repairing MySQL innodb exceptions

A set of MySQL libraries for testing. The previou...