方法1
- 代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
.main{
width: 300px;
height: 300px;
position: relative;
border: 1px solid #465468;
}
.inner{
position: absolute;
background:red;
width:100px;
height:100px;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div class = "main">
<div class = "inner"></div>
</div>
</body>
</html>
方法2
- 代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
.main{
width: 300px;
height: 300px;
position: relative;
border: 1px solid #465468;
}
.inner{
position: absolute;
background:red;
width:100px;
height:100px;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
</style>
</head>
<body>
<div class = "main">
<div class = "inner"></div>
</div>
</body>
</html>
方法3
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
.main{
width: 300px;
height: 300px;
border: 1px solid #465468;
align-items:center;
justify-content:center;
display:flex;
}
.inner{
background:red;
width:100px;
height:100px;
}
</style>
</head>
<body>
<div class = "main">
<div class = "inner"></div>
</div>
</body>
</html>
-
效果