总结——visibility和display
最近工作中用到了显示和隐藏——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: auto;
} td {
height: 30px;
width: 556px;
} h1, h2, h3 {
font-size: 12px;
margin: ;
padding: ;
} .table {
border: 1px solid #cad9ea;
color: #;
} .table th {
background-repeat: repeat-x;
height: 30px;
} .table td, .table th {
border: 1px solid #cad9ea;
padding: 1em ;
} .table tr.alter {
background-color: #f5fafe;
} </style>
</head>
<body>
<table class="table">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></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: auto;
} td {
height: 30px;
width: 556px;
} h1, h2, h3 {
font-size: 12px;
margin: ;
padding: ;
} .table {
border: 1px solid #cad9ea;
color: #;
} .table th {
background-repeat: repeat-x;
height: 30px;
} .table td, .table th {
border: 1px solid #cad9ea;
padding: 1em ;
} .table tr.alter {
background-color: #f5fafe;
} </style>
</head>
<body>
<table class="table">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr style="visibility:hidden">
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></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: auto;
} td {
height: 30px;
width: 556px;
} h1, h2, h3 {
font-size: 12px;
margin: ;
padding: ;
} .table {
border: 1px solid #cad9ea;
color: #;
} .table th {
background-repeat: repeat-x;
height: 30px;
} .table td, .table th {
border: 1px solid #cad9ea;
padding: 1em ;
} .table tr.alter {
background-color: #f5fafe;
} </style>
</head>
<body>
<table class="table">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr style="display:none">
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></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,隐藏该元素内容,且不占用域的空间。
总结——visibility和display的更多相关文章
- visibility和display的异同
都有隐藏节点的作用. visibility:hidden; display:none; (1)visibility 规定了元素是否可见,即使不可见也会占用上面的空间,在这里就是在指它与display的 ...
- visibility,display区别
visibility:hidden,display:none 前者隐藏位置还在,后者隐藏位置消失
- CSS:opacity:0,visibility:hidden,display:none的区别
CSS中opacity=0,visibility=hidden,display=none的时候,三者有什么区别呢?? 参考了stackoverflow的博客,才发现区别如下所示: Here is a ...
- 表单元素(控件)不可见,你用visibility还是display?(转)
属性大比拼:visibility和display的介绍 今天在做一个表单时涉及到这方面,当选中相应的选项后设置相应的几个元素(控件可见或不可见),后来还是用了visibility来实现.我们先来看下v ...
- CSS中用 opacity、visibility、display 属性将 元素隐藏 的 对比分析
说明 opacity 用来设置透明度 display 定义建立布局时元素生成的显示框类型 visibility 用来设置元素是否可见. opacity.visibility.display 这三个属性 ...
- visibility和display的区别
大多数人很容易将CSS属性display和visibility混淆,它们看似没有什么不同,其实它们的差别却是很大的. visibility属性用来确定元素是显示还是隐藏的,这用visibility=& ...
- CSS visibility与display 属性
所有主流浏览器都支持 visibility 属性. 注释:任何的版本的 Internet Explorer (包括 IE8)都不支持 "inherit" 和 "colla ...
- transition与visibility与display
http://www.zhangxinxu.com/wordpress/2013/05/transition-visibility-show-hide/ 术语解释是: visibility: 离散步骤 ...
- HTML 显示/隐藏DIV的技巧(visibility与display的差别)
参考链接:http://blog.csdn.net/szwangdf/article/details/1548807 div的visibility可以控制div的显示和隐藏,但是隐藏后页面显示空白: ...
随机推荐
- 应用程序如何找到DLL文件?
使用LoadLibrary显式链接,那么在函数的参数中可以指定DLL文件的完整路径: 如果不指定路径,或者进行隐式链接,Windows将遵循下面的搜索顺序来定位DLL: (1)包含EXE文件的目录(2 ...
- 学习马士兵的struts2/hibernate/spring中遇到的问题及其解决方法
STRUTS2 1. 写好最简单的hello world项目后,无法通过浏览器访问到hello.jsp页面,提示没有资源. 学习structs2的时间,已经更新到了2.3.16了,structs中的很 ...
- Kubuntu(14.04)共享wifi(热点)
笔记本(kubuntu14.04)通过有线上网,共享本机无线给手机.平板及其他通过wifi上网的设备. 曾经在网上找过各种方法.下载了非常多软件都不能在本机上实现wifi共享,以下这样的方法眼下相对简 ...
- accept函数
accept()函数 系统调用 accept() 会有点古怪的地方的! 你能够想象发生 这种事情:有人从非常远的地方通过一个你在侦听 (listen()) 的port连接 (connect()) 到你 ...
- 安卓高手之路之 WindowManager
安卓中的画面不是纯粹由window组成.而是改成了window+view的组织模式.window是一个顶层窗口的概念.view就相当于在window内的控件.而subwindow则是依附于window ...
- [Ramda] Filter, Reject and Partition
We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...
- jquery.validate+jquery.form提交的三种方式
原文:http://www.cnblogs.com/datoubaba/archive/2012/06/06/2538873.html jquery.validate+jquery.form提交的三种 ...
- JS比较两个数值大小的正确方法
转自:http://www.zzsky.cn/build/content/1832.htm 一般情况下: <script type="text/javsscript"> ...
- c# 方法参数(传值,传引用,ref,out,params,可选参数,命名参数)
一.方法参数的类型----值类型和引用类型 当方法传递的参数是值类型时,变量的栈数据会完整地复制到目标参数中即实参和形参中的数据相同但存放在内存的不同位置.所以,在目标方法中对形参所做的更改不会 ...
- PHP版根据经纬度和半径计算出经纬度的范围
百度地图提供了范围搜索的功能,但是它使用的是百度自己的数据,但是有时候我们需要使用自己的数据,显示在地图上.比如给定两个参数:指定位置(某一处的经纬度lnglat)和搜索半径(r),搜索指定范围内的数 ...