<div class="wrapper">
<div class="content"></div>
</div>
.wrapper{
position: relative;
width: 100px;
height: 100px;
background-color: red;
}
.content{
position: absolute;
left: 50%;
top: 50%;
width: 10px;
height: 10px;
background-color: green;
}
你的代码没有达到居中效果,原因是没有把content的宽高计算进去;
可以补上以下代码实现居中:
.content{margin:-5px 0 0 -5px;}
建议看一下这个:http://blog.csdn.net/freshlover/article/details/11579669
left :50% 是元素的左边界距离左侧50%而不是元素中线,所以你应该margin-left:-50px;把元素向左偏移元素宽度的一半才是居中
top同理
.content{margin:-5px 0 0 -5px;}
不算。退一万步讲你忘了计算content 的 width和height了。不建议这样子居中