1、margin实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.div1{
width: 200px;
height: 200px;
border: 1px solid #ddd;
}
.div2{
width: 100px;
height: 100px;
background-color: black;
margin: 50px auto;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
</body>
</html>
2、position实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.div1{
width: 200px;
height: 200px;
border: 1px solid #ddd;
position: relative;
}
.div2{
width: 100px;
height: 100px;
background-color: black;
position: absolute;
left: 50px;
top: 50px;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
</body>
</html>
3、CSS的display:table-cell
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.div1{
width: 200px;
height: 200px;
border: 1px solid #ddd;
display: table-cell;
vertical-align: middle;
}
.div2{
width: 100px;
height: 100px;
background-color: black;
margin: auto;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
</body>
</html>