第一种方案inline-block+align+table-cell+vertical-align
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{
margin: 0;
}
.parent{
background: gray;
width: 600px;
height: 500px;
text-align: center;
display: table-cell;
vertical-align: middle;
}
.son{
background: pink;
display: inline-block;
}
</style>
</head>
<body>
<div class="parent">
<div class="son">我是一段文字</div>
</div>
</body>
</html>
第二种方式position+transform
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{
margin: 0;
}
.parent{
background: gray;
width: 600px;
height: 500px;
position: relative;
}
.son{
background: pink;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="parent">
<div class="son">我是fsdfds一段文字</div>
</div>
</body>
</html>
第三种方式flex
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body{
margin: 0;
}
.parent{
background: gray;
width: 600px;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
}
.son{
background: pink;
}
</style>
</head>
<body>
<div class="parent">
<div class="son">我是fsdfds一段文字</div>
</div>
</body>
</html>