内部DIV设定position: absolute该如何居中

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试</title>
<style>
.className1{
width: 300px;
height: 300px;
margin:0 auto;
background-color: red;
position: relative;
}

.className2{
position: absolute;
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<div>
<div></div>
</div>
</body>
</html>

页面效果见图1

图1:css

试过在className2里加html

margin:0 auto

在有position: absolute;出现时margin:0 auto;是不起做用的测试


解决没法居中的办法:spa

在className2里加入如下代码:code

left:50%;
margin-left: -100px;

效果图:htm

如今已经解决问题。it

若是只是单纯的设置为
io

left:50%

效果是这样的:class

margin-left的值须要是宽度的一半;meta

相关文章
相关标签/搜索