The display: none; property truly hides the element. When this property is used, the hidden element does not take up any space, user interaction events such as click events do not work, and screen readers do not read the content of the element. Any child elements of this element will also be hidden. When using this property to switch an element from a visible state to a hidden state, the element does not occupy the original space and triggers the browser's repaint and reflow. Adding a transition animation to this property is ineffective; the transition between any different state values always takes effect immediately. The effect produced by this method is as if the element does not exist at all, but it can still be accessed in the DOM and manipulated through the DOM.
<styletype="text/css">.display-hide{display: none;}</style><section><divid="t1"></div></section><scripttype="text/javascript"> document.getElementById("t1").addEventListener("click",e=>{alert("It will be hidden after the first click, and cannot be clicked again"); e.srcElement.classList.add("display-hide");})</script>
opacity is used to set the transparency of an element. Setting opacity to 0 only visually hides the element, while the element itself still occupies its own position and affects the layout of the webpage. It will also respond to user interactions such as click events, and adding a transition property to it can display animation effects. For the opacity property, its transparent visual effect can be used to create clickjacking attacks.
<styletype="text/css">.opacity-hide{opacity: 0;}</style><section><divid="t2">opacity</div></section><scripttype="text/javascript"> document.getElementById("t2").addEventListener("click",e=>{alert("It will become transparent after the first click, still occupying the original position, click events still work, and the transition animation effect takes effect"); e.srcElement.classList.add("opacity-hide");})</script>
When the value of the visibility property is set to hidden, the element will be hidden, but it will still occupy its own position and affect the layout of the webpage. Unlike opacity, it does not respond to any user interactions. The element will also be hidden in screen readers. If the visibility of a child element is set to visible while the visibility of the parent element is set to hidden, the child element will still be displayed while the parent element is hidden.
<section><divid="t6">height</div></section><scripttype="text/javascript"> document.getElementById("t6").addEventListener("click",e=>{alert("The first click will hide the element, it will not occupy the original position, the click event will no longer take effect, and the transition animation will be effective"); e.srcElement.classList.add("height-hide");})</script>
<section><divid="t5">clip-path</div></section><section><divid="t6">height</div></section></body><scripttype="text/javascript"> document.getElementById("t1").addEventListener("click",e=>{alert("The first click will hide it, and it can no longer be clicked afterwards."); e.srcElement.classList.add("display-hide");}) document.getElementById("t2").addEventListener("click",e=>{alert("The first click will make it transparent, still occupying the original position, and the click event is still valid. The transition animation takes effect."); e.srcElement.classList.add("opacity-hide");}) document.getElementById("t3").addEventListener("click",e=>{alert("The first click will hide it, still occupying the original position, and the click event is no longer valid."); e.srcElement.classList.add("visibility-hide");}) document.getElementById("t4").addEventListener("click",e=>{alert("The first click will hide it, and the element will not occupy the original position. The transition animation takes effect."); e.srcElement.classList.add("position-hide");}) document.getElementById("t5").addEventListener("click",e=>{alert("The first click will hide it, and the element will occupy the original position, and the click event is no longer valid."); e.srcElement.classList.add("clip-path-hide");}) document.getElementById("t6").addEventListener("click",e=>{alert("The first click will hide it, and the element will not occupy the original position. The click event is no longer valid, and the transition animation takes effect."); e.srcElement.classList.add("height-hide");})</script></html>