标签:
最近工作中用到了显示和隐藏——visibility和display,它们两个都有显示隐藏的意思,但是又有所差别,接下来我们先看一下效果吧。
当没有效果的时候,我们展示一下源码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style> body, table { font-size: 12px; } table { table-layout: fixed; empty-cells: show; border-collapse: collapse; margin: 0 auto; } td { height: 30px; width: 556px; } h1, h2, h3 { font-size: 12px; margin: 0; padding: 0; } .table { border: 1px solid #cad9ea; color: #666; } .table th { background-repeat: repeat-x; height: 30px; } .table td, .table th { border: 1px solid #cad9ea; padding: 0 1em 0; } .table tr.alter { background-color: #f5fafe; } </style> </head> <body> <table class="table"> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </body> </html>
首先,先看一下visibility:hidden和visibility:visible
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style> body, table { font-size: 12px; } table { table-layout: fixed; empty-cells: show; border-collapse: collapse; margin: 0 auto; } td { height: 30px; width: 556px; } h1, h2, h3 { font-size: 12px; margin: 0; padding: 0; } .table { border: 1px solid #cad9ea; color: #666; } .table th { background-repeat: repeat-x; height: 30px; } .table td, .table th { border: 1px solid #cad9ea; padding: 0 1em 0; } .table tr.alter { background-color: #f5fafe; } </style> </head> <body> <table class="table"> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr style="visibility:hidden"> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </body> </html>
上面这段代码在页面中显示的效果:上设置visibility属性为hidden,第二行会消失——也就是我们说的隐藏。但在页面上保留该元素的空间。
总结visibility特性:
visibility:visible ——元素可见,默认值 |
visibility:hidden ——元素不可见,但仍然为其保留相应的空间。使用该属性后,HTML元素(对象)仅仅是在视觉上看不见(完全透明),而它所占据的空间位置仍然存在,也即是说它仍具有高度、宽度等属性值。 |
visibility:collapse ——只对table对象起作用,能移除行或列但不会影响表格的布局。如果这个值用在table以外的对象上则表现为hidden |
visibility:inherit ——继承上级元素的visibility值 |
js的style.visibility ——
将元素visibility属性设为 hidden,隐藏该元素内容,但占用域的空间。 |
接着,看一下display:none和display:block
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style> body, table { font-size: 12px; } table { table-layout: fixed; empty-cells: show; border-collapse: collapse; margin: 0 auto; } td { height: 30px; width: 556px; } h1, h2, h3 { font-size: 12px; margin: 0; padding: 0; } .table { border: 1px solid #cad9ea; color: #666; } .table th { background-repeat: repeat-x; height: 30px; } .table td, .table th { border: 1px solid #cad9ea; padding: 0 1em 0; } .table tr.alter { background-color: #f5fafe; } </style> </head> <body> <table class="table"> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr style="display:none"> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </body> </html>
当第二行设置为display:none的时候,第二行会消失并且原来所在的位置变成第三行。当设置为display:block,又会显示出来。
总结display:
block:
表现为一个块级元素(一般情况下独占一行)
当display被设置为block(块)时,容器中所有的元素将会被当作一个单独的块,就像<DIV>元素一样,它会在那个点被放入到页面中。(实际上你可以设置<span>的display:block,使其可以像<DIV>一样工作。
inline:
表现为一个行级元素(一般情况下不独占一行)
将display设置为inline,将使其行为和元素inline一样---即使它是普通的块元素如<DIV>,它也将会被组合成像<span>那样的输出流。
none:
元素不可见,并且不为其保留相应的位置
最后是display被设置:none,这时元素实际上就从页面中被移走,它下面所在的元素就会被自动跟上填充。
Display的使用
1、display默认属性值为块级的元素:
adress,quote,body,xmp,center,col,colgroup,dd,dtr,div,
dl,dt,fieldset,form,hn,hr,iframe,legend,listing,marquee,
menu,ol,p,plaintext,pre,table,td,th,tr,ul
2、display默认属性值为none的元素:
br,frame,nextid,tbody,tfoot,thead
3、li元素的display属性默认值为:list-item
4、其他元素display属性默认值都为inline
Display的特性
改变元素的display属性将对周围元素造成的影响有:
1、在属性值设为block的元素后面添加新行
2、从属性值设为inline的元素所在行中删除一行
3、隐藏属性值设为none的元素并且释放该元素在文档中所占的物理空间,对于其他元素来说,相当于该元素不存在,因此,该元素的位置被其他元素顶替
display:none;
使用该属性后,HTML元素(对象)的宽度、高度等各种属性值都将“丢失”;
display:none:
1、JS读取元素属性值
如果在样式文件或页面文件代码中直接用display:none对元素进行了隐藏,载入页面后,在没有通过js设置样式使元素显示的前提下,使用js代码会无法正确获得该元素的一些属性,比如offSetTop,offSetLeft等,返回的值会为0,通过js设置style.display来使元素显示后才能正确获得这些值。
2、SEO优化时需要注意
使用display:none隐藏的元素不会被百度等搜索网站检索,会影响到网站的SEO,某些情况下可以使用left:-100000px来达到同样效果。
3、样式文件
如果是通过样式文件或<style>css</style>方式来设置元素的display:none样式,用js设置style.display=""并不能使元素显示,可以使用block或inline等值来代替。通过style="display:none"直接在元素上进行的设置不会有这个问题
style.display
将元素display属性设为 block,会在该元素后换行。
将元素display属性设为 inline,会消除元素换行。
将元素display属性设为 none,隐藏该元素内容,且不占用域的空间。
标签:
原文地址:http://www.cnblogs.com/dakang/p/5731031.html