What are the ways to control the hiding of elements in css3
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!

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











How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

Implementation method: 1. Use the ":active" selector to select the state of the mouse click on the picture; 2. Use the transform attribute and scale() function to achieve the picture magnification effect, the syntax "img:active {transform: scale(x-axis magnification, y Axis magnification);}".

In CSS3, you can use the "animation-timing-function" attribute to set the animation rotation speed. This attribute is used to specify how the animation will complete a cycle and set the speed curve of the animation. The syntax is "element {animation-timing-function: speed attribute value;}".

CSS3 linear gradient can realize triangles; just create a 45-degree linear gradient and set the gradient color to two fixed colors, one is the color of the triangle and the other is transparent color. The syntax "linear-gradient(45deg, color value , color value 50%, transparent color 50%, transparent color 100%)".
