one
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.father{
width: 200px;
height: 200px;
border: 3px solid green ;
}
.son{
width: 50px;
height: 50px;
background-color:
margin: 0 auto;
position: relative;
top: 50%;
/* 自身高度的一半 */
margin-top: -25px;
}
</style>
</head>
<body>
<div class="father">
<div class="son">
sadasdas
</div>
</div>
</body>
</html>
tow
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.father{
width: 200px;
height: 200px;
border: 3px solid green ;
display: flex;
justify-content: center;
align-items: center;
}
.son{
width: 50px;
height: 50px;
background-color:
}
</style>
</head>
<body>
<div class="father">
<div class="son">
sadasdas
</div>
</div>
</body>
</html>
three
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.father{
width: 400px;
height: 400px;
border: 3px solid green ;
position: relative;
}
.son{
width: 200px;
height: 200px;
background-color:
position: absolute;
left: 50%;
top: 50%;
/* margin-top: -100px;
margin-left: -100px; */
/* 失败 */
/* margin-bottom:100px ;
margin-right: 100px; */
/* 可以 */
transform: translate(-50%,-50%); /*自己的50% */
}
</style>
</head>
<body>
<div class="father">
<div class="son">
sadasdas
</div>
</div>
</body>
</html>
four
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
.father{
display: table-cell;
width: 400px;
height: 400px;
vertical-align: middle;
text-align: center;
background-color: purple;
}
.child{
display: inline-block;
vertical-align: middle;
width: 200px;
height: 200px;
background-color: gray;
}
</style>
</head>
<body>
<div class="father">
<div class="child"></div>
</div>
</body>
</html>
five
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
width: 300px;
height: 300px;
background-color: deepskyblue;
position: relative;
}
.son{
width: 100px;
height: 100px;
background-color: pink;
/*实现水平垂直居中*/
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="father">
<div class="son">
</div>
</div>
</body>
</html>
six (不设置宽高 不能用的)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father{
width: 300px;
height: 300px;
background-color: deepskyblue;
position: relative;
}
.son{
background-color: pink;
/*实现水平垂直居中*/
position: absolute;
left: 100px;
right: 100px;
bottom: 100px;
top: 100px;
}
</style>
</head>
<body>
<p>不设置宽高</p>
<div class="father">
<div class="son">
</div>
</div>
</body>
</html>