Home > Web Front-end > Front-end Q&A > What are the ways to control the hiding of elements in css3

What are the ways to control the hiding of elements in css3

青灯夜游
Release: 2022-01-13 13:49:06
Original
1615 people have browsed it

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.

What are the ways to control the hiding of elements in css3

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

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(&#39;normal&#39;);
            let bt = document.getElementById(&#39;bt&#39;);
            normal.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click normal&#39;);   
            })
            bt.addEventListener(&#39;click&#39;,function(){
                alert(&#39;click bt&#39;);   
            })
        </script>
    </body>
</html>
Copy after login

(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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template