


How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples)
In CSS, there are many ways to hide elements (meaning invisible to the naked eye within the screen), some occupy space, some do not occupy space; some can respond to clicks, and some cannot respond to clicks. So how do these CSS methods implement hidden elements, and what are the differences between them. This chapter will introduce to you how to hide page elements in css? Four implementation methods of css hidden elements and the differences between them (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Four implementations of css hidden elements
1. visibility: hidden, set the element to be hidden
Code example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>元素隐藏--visibility: hidden</title> <style> .demo{ width: 800px; height: 200px; margin: 50px auto; } span{ visibility: hidden; } </style> </head> <body> <div class="demo"> <p>元素隐藏1--visibility: hidden,元素隐藏2--visibility: hidden,元素隐藏3--visibility: hidden</p> <p>元素隐藏1--visibility: hidden,<span>元素隐藏2--visibility: hidden</span>,元素隐藏3--visibility: hidden</p> </div> </body> </html>
Rendering:
The visibility attribute specifies whether the element is visible. Setting its value to hidden only visually hides the element. The hidden element itself still occupies its own position and contributes to our web page layout.
2. Opacity: 0, set the element to hide
Code example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>元素隐藏--opacity: 0</title> <style> .demo{ width: 800px; height: 200px; margin: 50px auto; } span{ opacity: 0; } </style> </head> <body> <div class="demo"> <p>元素隐藏1--opacity: 0,元素隐藏2--opacity: 0,元素隐藏3--opacity: 0</p> <p>元素隐藏1--opacity: 0,<span>元素隐藏2--opacity: 0</span>,元素隐藏3--opacity: 0</p> </div> </body> </html>
Rendering:
opacity The property means to set the transparency of an element. It is not designed to change the bounding box of an element. This means that setting opacity to 0 only visually hides the element. The element itself still occupies its own position and contributes to the layout of the web page. This is similar to visibility: hidden above.
3. display:none, set the element to hide
Code example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>元素隐藏--display:none</title> <style> .demo{ width: 800px; height: 200px; margin: 50px auto; } span{ opacity: 0; } </style> </head> <body> <div class="demo"> <p>元素隐藏--display:none,元素隐藏--display:none,元素隐藏--display:none</p> <p>元素隐藏--display:none,<span>元素隐藏--display:none</span>,元素隐藏--display:none</p> </div> </body> </html>
Rendering:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>元素隐藏--position: absolute</title> <style> .demo{ width: 800px; height: 200px; margin: 50px auto; } span{ position: absolute; top: -9999px; left: -9999px; } </style> </head> <body> <div class="demo"> <p>元素隐藏1--position: absolute,元素隐藏2--position: absolute,元素隐藏3--position: absolute</p> <p>元素隐藏1--position: absolute,<span>元素隐藏2--position: absolute</span>,元素隐藏3--position: absolute</p> </div> </body> </html>Rendering:
##position : absolute, the main principle of setting the hidden element is to make it invisible on the screen by setting the top and left of the element to a large enough negative number.
2. Difference
1.visibility:hidden The element will be hidden, but it will not disappear and still occupy space. The original HTML style will not be changed after hiding; It will be inherited by descendants, who can also unhide it by setting visibility: visible;
Events that have been bound to this element will not be triggered. Dynamically modifying this property will cause redrawing.
2. Opacity:0
Only the transparency is 100%, the element is hidden, and it still occupies space. The original html style will not be changed after hiding;
will be inherited by child elements, and the child Elements cannot be unhidden through opacity=1;
3. display:none
does not occupy space and hides the element, so dynamically changing this attribute will cause rearrangement (change the page layout), which can be understood as placing the element on the page. The same thing happens when the element is deleted;
will not be inherited by descendants, but its descendants will not be displayed. After all, they are all hidden together.
4. position: absolute
position: absolute, by setting the top and left of the element to a large enough negative number, it will be invisible on the screen and have a dynamic interactive effect. .
The above is the detailed content of How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples). For more information, please follow other related articles on the PHP Chinese website!

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools
