Home Web Front-end HTML Tutorial Summary of input box style modification of type='file'

Summary of input box style modification of type='file'

Feb 25, 2017 pm 02:18 PM

The input type="file" in the form is often used in front-end development, but unfortunately the performance of input type="file" is not uniform in various browsers, and the style is difficult to work; so how do we Let’s deal with this problem. This is the main content of our discussion today. What is the input of type="file"?

I don’t think there is any need to say what this is. Everyone knows it anyway. And in today’s era of various mobile phones, you can also upload it by taking photos directly. Anyway, it is much more fun than before. .

And in the past, you could only upload one file. Now, you only need to add the multiple attribute to upload multiple files, and there are many file formats supported. Without further ado, the details are Please see http://www.w3.org/html/ig/zh/wiki/HTML5/number-state#.E6.96.87.E4.BB.B6.E4.B8.8A.E4.BC.A0.E7 .8A.B6.E6.80.81

Upload button style adjustment

Anyone who has played with CSS knows that in HTML elements, the style of form control elements Modification is the most painful. The styles of many controls change according to the system theme. If you want to modify it, you can only simulate it, especially in IE browser.

For the upload button type="file", we once modified its style. Some people used the simulation method, but it is said that using the simulation method may cause so-called security issues. Sexual issues, well, for a page guy like me, I don’t understand it, and even if I understand it, I don’t know how to deal with it. Then don't simulate it...

But if you don't simulate it, how can you modify the style?

Picture positioning overlay scheme

Before, the method I know, in fact, everyone also knows, is to pass type="file "The upload button is made transparent and then superimposed on a picture, so that people can feel that it is achieved by clicking to upload the picture, and there is no need to look at the native upload button.

<input type="file" id="upfile" ><span ></span>.up_icon,.up_input {position: absolute;top: 10px;left: 10px;width: 64px;height: 64px;z-index: 2}.up_icon {overflow: hidden;font-size: 0;line-height: 99em;background: url(http://www.php.cn/) no-repeat 0 0;z-index: 1;}
Copy after login

Take a look at the demo: http://jsbin.com/qacijusihivi/1/

It should be very clear in this demo that you can see the implementation method. The upload button is positioned through positioning. Positioned above the image, the image can be used as the background image of an empty label. Then set the opacity transparency of the upload button to 0 and then you will no longer be able to see the button, but actually It exists, and then...then...there is no more, and the effect is there...

Solution for webkit

This solution for the webkit kernel is actually a bit nonsense and has little practical use. , because it is only valid for the webkit kernel. If the writing method with the -webkit- prefix is ​​not supported, it will have no effect, so you can just take a look at it for fun.

<input type="file" id="upfile">input[type="file"]::-webkit-file-upload-button {position: absolute;top: 10px;left: 10px;width: 64px;height: 64px;overflow: hidden;line-height: 99em;background:url(http://www.php.cn/) no-repeat 0 0;border: 0 none;z-index: 2;}
Copy after login

Look at the demo first: http://jsbin.com/wicihihabifi/1/

The HTML structure in this demo is very simple, just an [input tag That's it. It's much simpler than the method we saw before, but it is definitely much worse in terms of compatibility. I don't know how it is on the mobile phone. Now most mobile phones are webkit-based browsers. Forget it. , just treat it as entertainment and have fun watching it ~

The simple HTML structure modification style is completely dependent on the ::-webkit-file-upload-button pseudo element, for this pseudo element Just modify the element's style accordingly, because this is just an ordinary button element. It will be clearer if we view this button element by displaying the shadow DOM.

Summary of input box style modification of type=file

This is the DOM tree seen in the chrome developer tools. Generally speaking, if we do not enable viewing of shadow DOM, we cannot see a type There is so much content in the input of ="file". The way to turn it on is very simple. Click the gear Summary of input box style modification of type=file in the upper right corner of the developer tools, and then check this box in the pop-up layer.

Summary of input box style modification of type=file

Now readers can check the other input tags by themselves. If there is a shadow DOM, it can definitely be expanded. Then among the new HTML5 tags, there are some...

Finally

For the input tag of type="file", currently, the only two ways I know to modify the style are these, and then for webkit's solution is also very limited, but it should be no problem for mobile phones. Some people may say, what should I do with other browsers? Yes, I don’t know what to do.

In the Firefox browser, although there is a selector input type="file" > button[type="button"]exists with forms.css , but I don’t know why, after I added this selector to my style, I still didn’t see any effect, so I stopped playing.

PS: Regarding the form.css file, if you are a fan of the Firefox browser, you must know the existence of the path resource://gre-resources/forms.css.

Oh, that’s it. There is also an Opera browser. When I played it on Mac, the style of the prefix -webkit- was directly inherited...

For more articles related to the summary of input box style modification of type="file", please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles