display:none和visibility:hidden的區別

轉載自新浪博客->最初的你,文章地址:http://blog.sina.com.cn/s/blog_51048da7010185io.html


visibilitydisplay兩個屬性都有隱藏元素的功能,display:none和visibility:hidden的區別,簡單的總結一句話就是:visibility:hidden隱藏,但在瀏覽時保留位置;而display:none視爲不存在,且不加載!

1、visibility:hidden->爲隱藏的對象保留其物理空間
HTML元素(對象)僅僅是在視覺上看不見(完全透明),而它所佔據的空間位置仍然存在。

2、display: none->不爲被隱藏的對象保留其物理空間
HTML元素(對象)的寬度、高度等各種屬性值都將“丟失”


實例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
<table>
    <tr>
   <td>
    <table border="1">
    <tr>
     <td>第一行</td>
    </tr>
    <tr style="display:none">
     <td>第二行</td>
    </tr>
    <tr>
     <td>第三行</td>
    </tr>
    </table>
   </td>
   <td>
    <table border="1">
    <tr>
     <td>第一行</td>
    </tr>
    <tr style="visibility:hidden">
     <td>第二行</td>
    </tr>
    <tr>
     <td>第三行</td>
    </tr>
    </table>
   </td>
  </tr>
  <tr>
   <td>style="display:none"</td>
   <td>style="visibility:hidden"</td>
  </tr>
</table>
</body>
</html>


結果:



說明:

爲了更深入理解,推薦這篇文章,可參考

1、您可能不知道的CSS元素隱藏“失效”以其妙用

2、跟着w3c標準來探討display:none;與visibility:hidden;的區別->此文章中提到渲染樹等系列,不懂的戳下面文章,可以幫助更好的理解:

>>渲染樹render tree

>>瀏覽器的渲染原理簡介

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章