想要直接通过css调整元素位置,例如让其居中,有时候可以直接用align属性,但是如果设定了position为absolute的时候,就需要通过left和top属性来达到效果了。可以通过js来设置元素的left和top,当然也可以直接通过css来设置。
<style type="text/css">
.test{
position:relative;
width:100%;
height:100%;
background:#016aab;
}
body{
background:#016aab;
}
.test1{
position:absolute;
width:400px;
height:200px;
left:expression((this.parentNode.clientWidth-400)/2 + 'px');
top:expression((this.parentNode.clientHeight-200)/2+'px');
background:#2794c2;
}
</style>
<div class="test">
<div class="test1"></div>
</div>
本文介绍了一种利用CSS绝对定位使元素居中的方法。通过设定left和top属性的expression表达式,可以实现元素在其父容器内的水平垂直居中。这种方法适用于需要精确控制元素位置的情况。
889

被折叠的 条评论
为什么被折叠?



