Customize the style of input[type='file']
The style of input[type="file"] behaves differently in various browsers:
1.chrome:
2.firefox:
3. opera:
4.ie:
5.edge:
In addition, when we specify the height of input[type="file"] and set its line height to be equal to its height, an ugly style appears in chrome:
The line "No files selected" is not centered vertically.
It seems to look better in firefox. Well, I prefer using firefox. However, the performance in these browsers is inconsistent, and we must do compatibility processing.
Idea:
1. Customize a style similar to one of the browsers and put it on the lower layer;
2. "Hide" the style displayed by the browser itself, and place opacity: 0; on the upper layer, so that what we click is the event that input[type="file"] responds to;
3. After selecting a file or changing a file, change the value of displayed file.
Code:
html: <form action="" class="activityForm"> <div class="file"> <label for="file">文件:</label> <div class="userdefined-file"> <input type="text" name="userdefinedFile" id="userdefinedFile" value="未选择任何文件"> <button type="button">上传</button> </div> <input type="file" name="file" id="file"> </div> </form>
css: .file { position: relative; height: 40px; line-height: 40px; } .file label { display: inline-block; } .userdefined-file { position: absolute; top: 0; left: 60px; z-index: 2; width: 300px; height: 40px; line-height: 40px; font-size: 0; /*应对子元素为 inline-block 引起的外边距*/ } .userdefined-file input[type="text"] { display: inline-block; vertical-align: middle; padding-right: 14px; padding-left: 14px; width: 220px; box-sizing: border-box; border: 1px solid #ccc; height: 40px; line-height: 40px; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .userdefined-file button { display: inline-block; vertical-align: middle; width: 80px; text-align: center; height: 40px; line-height: 40px; font-size: 14px; background-color: #f54; border: none; color: #fff; cursor: pointer; } .file input[type="file"] { position: absolute; top: 0; left: 60px; z-index: 3; opacity: 0; width: 300px; height: 40px; line-height: 40px; cursor: pointer; }
js: document.getElementById("file").onchange = function() { document.getElementById("userdefinedFile").value = document.getElementById("file").value; }
After processing in this way, the performance will be consistent in various browsers:
1. In chrome when no file is selected:
2. After selecting the file, in firefox:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

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

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

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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