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!

两种方法:1、利用display属性,只需给元素添加“display:none;”样式即可。2、利用position和top属性设置元素绝对定位来隐藏元素,只需给元素添加“position:absolute;top:-9999px;”样式。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

自适应布局又称“响应式布局”,是指可以自动识别屏幕宽度、并做出相应调整的网页布局;这样的网页能够兼容多个不同的终端,而不是为每个终端做一个特定的版本。自适应布局是为解决移动端浏览网页而诞生的,能够为使用不同终端的用户提供很好的用户体验。

实现方法:1、使用“:active”选择器选中鼠标点击图片的状态;2、使用transform属性和scale()函数实现图片放大效果,语法“img:active {transform: scale(x轴放大倍数,y轴放大倍数);}”。

css3中的动画效果有变形;可以利用“animation:动画属性 @keyframes ..{..{transform:变形属性}}”实现变形动画效果,animation属性用于设置动画样式,transform属性用于设置变形样式。

在css3中,可以利用“animation-timing-function”属性设置动画旋转速度,该属性用于指定动画将如何完成一个周期,设置动画的速度曲线,语法为“元素{animation-timing-function:速度属性值;}”。

本篇文章带大家一起深入了解一下CSS3中的新特性::target-text 选择器,聊聊该选择器的作用和使用方法,希望对大家有所帮助!

css3线性渐变可以实现三角形;只需创建一个45度的线性渐变,设置渐变色为两种固定颜色,一个是三角形的颜色,另一个为透明色即可,语法“linear-gradient(45deg,颜色值,颜色值 50%,透明色 50%,透明色 100%)”。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

Dreamweaver Mac version
Visual web development tools

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

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