我想到了兩種CSS方法實現(xiàn):1..box{margin:auto;position:absolute;left:0;right:0;bottom:0;top:0;width:200px;height:200px;}2..box{position:absolute;left:50%;right:50%;width:200px;height:200px;margin-left:-100px;margin-top:-100px;}測試后神奇的發(fā)現(xiàn),第二種方法下,盒子值上下居中,不能左右居中,請問是什么問題造成的(頁面中沒有其他多余的樣式,只加了box的樣式,加了一個div標簽)。
2 回答

腦袋空空空想家
TA貢獻42條經(jīng)驗 獲得超49個贊
position設置成為absolute以后,margin就會失效了。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ajax</title>
<style>
.a1{
width: 100px;
height: 100px;
position: absolute;
background-color: red;
top:0;
bottom: 0;
left: 0;
right: 0;?
margin: auto;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
添加回答
舉報
0/150
提交
取消