Control method: 1. Use the "display:none" statement to remove the element from the accessibility tree, thereby hiding the element; 2. Use the "visibility: hidden" statement to set the element to be invisible; 3. Use The "opacity: 0" statement sets the element to be transparent; 4. Let the element move away from the screen display position, etc.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
How css3 controls the hiding of elements
The first way: remove the accessibility tree
display : none
The display attribute can set the internal and external display type of the element. Setting display to none will remove the element from the accessibility tree.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>display : none</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { display : none; } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
Second type: Hidden element
visibility: hidden
Change visibility Setting it to hidden will make the element invisible, but at this time the element is still in the accessibility tree (the element is moved out of the accessibility tree when display: none), and registering click events is invalid.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>visibility: hidden</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { visibility: hidden; } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
Third type: transparent
opacity: 0
opacity(no Transparency), the value range is 0 (completely transparent) ~ 1 (completely opaque). Setting opacity to 0 will make the element completely transparent. At this time, the element is not visible (because it is transparent) and is still in the accessibility tree. Registering click events is valid.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>opacity: 0</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { opacity: 0; } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
transparent
Set the background-color, color and border-color of the element to transparent (transparent). At this time, the element Invisible (because it is transparent), still in the accessibility tree, registering click events is valid.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>transparent</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { color: transparent; background-color: transparent; border-color: transparent; } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
rgba(0,0,0,0)
Technically speaking, transparent is rgba(0,0, 0,0), set the element's background-color, color and border-color to rgba(0,0,0,0) (transparent). At this time, the element is invisible (because it is transparent) and is still located In the accessibility tree, registering click events is valid.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>rgba(0,0,0,0)</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { color: rgba(0,0,0,0); background-color: rgba(0,0,0,0); border-color: rgba(0,0,0,0); } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
rgba only needs the fourth parameter to be 0 to achieve the effect of hiding elements.
hsla(0,0%,0%,0)
hsla uses the same element hiding mechanism as rgba, both are controlled by the fourth parameter Alpha , set the element's background-color, color, and border-color to hsla(0,0%,0%,0). At this time, the element is not visible (because it is transparent) and is still in the accessibility tree. Registering click events is valid.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>hsla(0,0%,0%,0)</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { color: hsla(0,0%,0%,0); background-color: hsla(0,0%,0%,0); border-color: hsla(0,0%,0%,0); } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
hsla is the same as rgba. Only the fourth parameter is 0 to achieve the effect of hiding elements.
filter: opacity(0%)
filter (filter) opacity (0% ~ 100%) converts the transparency of the image, the value range is Between 0% (completely transparent) ~ 100% (completely opaque). Set the element's filter to opacity (0%). At this time, the element is invisible (because it is transparent) and is still in the accessibility tree. Registering click events is effective.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>filter: opacity(0%)</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { filter: opacity(0%); } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
Fourth: scaling
transform: scale(0, 0)
Setting transform to scale(0, 0) will cause the element to be scaled to 0 pixels on both the x-axis and y-axis. This element will be displayed and will occupy the position, but because it has been scaled to 0%, the element and content will occupy The pixel ratio is 0*0, so this element and its content cannot be seen and cannot be clicked.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>transform: scale(0, 0)</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { transform: scale(0,0); } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
width: 0;height: 0;overflow: hidden
Set both width and height to 0 so that the element occupies The pixel ratio is 0*0, but two situations will occur at this time:
-
When the display attribute of the element is inline, the element content will stretch the width and height of the element;
When the display attribute of the element is block or inline-block, the element width and height are 0, but the element content is still displayed normally. At this time, add overflow:hidden; to crop the outside of the element. element content.
The difference between this method and transform: scale(0,0) is that transform: scale(0,0) scales both the elements and the content, while this method scales both the elements and the content. Scale the element to 0px, and then crop the element content outside the element.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>width: 0;height: 0;overflow: hidden</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { width:0; height:0; overflow: hidden; border-width: 0;/* user agent stylesheet中border-width: 2px; */ padding: 0;/* user agent stylesheet中padding: 1px 6px; */ } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
Fifth type: off-screen display position
Off-screen display position can also make the element invisible, but to achieve this There are too many CSS styles for effects. Here is just one example.
Code:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>脱离屏幕显示位置</title> <style type="text/css"> p { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { position: fixed; top: -100px; left: -100px; } </style> </head> <body> <p> <button id="normal">按钮</button> </p> <p> <button id="bt">按钮</button> </p> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
(Learning video sharing: css video tutorial)
The above is the detailed content of What are the ways to control the hiding of elements in css3. For more information, please follow other related articles on the PHP Chinese website!

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.


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

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
