W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <head> <style> .imgbox { float: left; text-align: center; width: 185px; border: 1px solid gray; margin: 4px; padding: 6px; } button { width: 100%; } </style> </head> <body> <h1>display:none 与 visiblity: hidden 的区别</h1> <p><b>visibility:hidden</b> 隐藏元素,但仍占据布局中的空间。</p> <p><b>display:none</b> 从文档中删除元素。它不会占据任何空间。</p> <div class="imgbox" id="imgbox1">Box 1<br> <img src="/i/css/imgbox-1.gif" alt="Box 1" style="width:100%"> <button onclick="removeElement()">删除</button> </div> <div class="imgbox" id="imgbox2">Box 2<br> <img src="/i/css/imgbox-2.gif" alt="Box 2" style="width:100%"> <button onclick="changeVisibility()">隐藏</button> </div> <div class="imgbox">Box 3<br> <img src="/i/css/imgbox-3.gif" alt="Box 3" style="width:100%"> <button onclick="resetElement()">重置所有</button> </div> <script> function removeElement() { document.getElementById("imgbox1").style.display = "none"; } function changeVisibility() { document.getElementById("imgbox2").style.visibility = "hidden"; } function resetElement() { document.getElementById("imgbox1").style.display = "block"; document.getElementById("imgbox2").style.visibility = "visible"; } </script> </body> </html>