父元素高度为auto:子元素使用top:-50%没有效果的问题
2016-03-23 13:47
549 查看
当为父元素设定了height:auto的时候,子元素设置position:relative top:50%没有效果的问题
代码如下:<!DOCTPYE html>
<html>
<head>
<meta content="text/html" charset="utf-8">
<style type="text/css">
html, body
{
width:100%;
height:100%;
}
#one
{
position: absolute;
top:50%;
left:50%;
<span style="color:#ff0000;"><strong> height:auto;</strong></span>
width:auto;
}
.pa
{
position: relative;
height:400px;
width:200px;
<strong><span style="color:#ff0000;">top:-50%;</span></strong>
left:-50%;
border:1px solid black;
background-color:greenyellow ;
}
</style>
</head>
<body>
<div class="pr" id="one">
<div class="pa pa1">#one的子元素pa1,相对#one绝对定位,#one是它的父元素,与.pa2为同级兄弟元素</div>
</div>
</body>
</html>其实这段代码是想让 .pa 水平垂直居中的,可是并没有效果,top:-50%没有效果,如下图
正常情况下父元素设置height:auto应用获得子元素最大高度,在浏览器中也显示了父元素的模型高度是子元素高度400px,但是使用top:-50%并没有效果,这是因为
w3c的文档说
<percentage>
The offset is a percentage of the containing block's width (for 'left' or 'right') or height (for 'top' and 'bottom'). For 'top' and 'bottom', if the height of the containing block is not specified explicitly (i.e., it depends on content height), the percentage value is interpreted like 'auto'.
如果父元素的高度没有明确指定,top 的值跟设定为auto的效果是一样的 。
当我们把height高度设定为固定值的时候:比如height:400px,代码如下,这时候子元素使用top:-50%就有效果了
<!DOCTPYE html>
<html>
<head>
<meta content="text/html" charset="utf-8">
<style type="text/css">
html, body
{
width:100%;
height:100%;
}
#one
{
position: absolute;
top:50%;
left:50%;
<strong><span style="color:#cc0000;"> </span><span style="color:#ff0000;"> height:400px;</span></strong>
width:auto;
}
.pa
{
position: relative;
height:400px;
width:200px;
<strong> <span style="color:#ff0000;"> top:-50%;</span></strong>
left:-50%;
border:1px solid black;
background-color:greenyellow ;
}
</style>
</head>
<body>
<div class="pr" id="one">
<div class="pa pa1">#one的子元素pa1,相对#one绝对定位,#one是它的父元素,与.pa2为同级兄弟元素</div>
</div>
</body>
</html>结果如下图
所以,当父元素height设定为:auto的时候,子元素position:relative top:-50%,top:百分比值并不起作用,用px可以,比如:top:-(子元素高度的一半)
代码如下:<!DOCTPYE html>
<html>
<head>
<meta content="text/html" charset="utf-8">
<style type="text/css">
html, body
{
width:100%;
height:100%;
}
#one
{
position: absolute;
top:50%;
left:50%;
<span style="color:#ff0000;"><strong> height:auto;</strong></span>
width:auto;
}
.pa
{
position: relative;
height:400px;
width:200px;
<strong><span style="color:#ff0000;">top:-50%;</span></strong>
left:-50%;
border:1px solid black;
background-color:greenyellow ;
}
</style>
</head>
<body>
<div class="pr" id="one">
<div class="pa pa1">#one的子元素pa1,相对#one绝对定位,#one是它的父元素,与.pa2为同级兄弟元素</div>
</div>
</body>
</html>其实这段代码是想让 .pa 水平垂直居中的,可是并没有效果,top:-50%没有效果,如下图
正常情况下父元素设置height:auto应用获得子元素最大高度,在浏览器中也显示了父元素的模型高度是子元素高度400px,但是使用top:-50%并没有效果,这是因为
w3c的文档说
<percentage>
The offset is a percentage of the containing block's width (for 'left' or 'right') or height (for 'top' and 'bottom'). For 'top' and 'bottom', if the height of the containing block is not specified explicitly (i.e., it depends on content height), the percentage value is interpreted like 'auto'.
如果父元素的高度没有明确指定,top 的值跟设定为auto的效果是一样的 。
当我们把height高度设定为固定值的时候:比如height:400px,代码如下,这时候子元素使用top:-50%就有效果了
<!DOCTPYE html>
<html>
<head>
<meta content="text/html" charset="utf-8">
<style type="text/css">
html, body
{
width:100%;
height:100%;
}
#one
{
position: absolute;
top:50%;
left:50%;
<strong><span style="color:#cc0000;"> </span><span style="color:#ff0000;"> height:400px;</span></strong>
width:auto;
}
.pa
{
position: relative;
height:400px;
width:200px;
<strong> <span style="color:#ff0000;"> top:-50%;</span></strong>
left:-50%;
border:1px solid black;
background-color:greenyellow ;
}
</style>
</head>
<body>
<div class="pr" id="one">
<div class="pa pa1">#one的子元素pa1,相对#one绝对定位,#one是它的父元素,与.pa2为同级兄弟元素</div>
</div>
</body>
</html>结果如下图
所以,当父元素height设定为:auto的时候,子元素position:relative top:-50%,top:百分比值并不起作用,用px可以,比如:top:-(子元素高度的一半)
相关文章推荐
- Web布局连载——两栏固定布局(五)
- easyui------显示隐藏列功能
- [div+css]晒晒最新制作专题推广页模板
- 盒子模型
- CSS选择器
- CSS样式优先权
- [css] line-height 百分比单位和数值单位的区别
- Apple官网研究之使用Justify布局导航
- 设计更快的网页(三):字体和 CSS 调整
- 纯CSS制作的新闻网站中的文章列表
- 10条影响CSS渲染速度的写法与使用建议第1/3页
- BS项目中的CSS架构_仅加载自己需要的CSS
- 很不错的 CSS Hack 又学了一招
- 发一个css比较清爽的写法
- CSS expression控制图片自动缩放效果代码[兼容 IE,Firefox]
- css布局网页水平居中常用方法
- CSS经典技巧十则第1/2页
- css 兼容性问题this.style.cursor=''hand''
- IE6不能正常解析CSS文件问题的解决方法及原因分析