1.垂直居中absolute+transform
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>垂直居中absolute+transform</title>
<style>
.parent
{
width: 100px;
height: 300px;
background: rgba(47, 67, 255, 0.26);
/************************注意以下关键代码**************************/
display: flex;
align-items: center;
}
.child
{
background: rgba(225, 255, 43, 0.26);
}
</style>
</head>
<body>
<div class="parent">
<div class="child">MM</div>
</div>
</body>
</html>
2.垂直居中flex+align-items
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>垂直居中flex+align-items</title>
<style>
.parent
{
width: 100px;
height: 300px;
background: rgba(47, 67, 255, 0.26);
/************************注意以下关键代码**************************/
position: relative;
}
.child
{
width: 100px;
height: 100px;
background: rgba(225, 255, 43, 0.26);
/************************注意以下关键代码**************************/
position: absolute;
top:50%;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="parent">
<div class="child">MM</div>
</div>
</body>
</html>
3.垂直居中table-cell+vertical-align
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>垂直居中table-cell+vertical-align</title>
<style>
.parent
{
width: 100px;
height: 300px;
background: rgba(47, 67, 255, 0.26);
/************************注意以下关键代码**************************/
/*兼容ie6、7要改结构为table*/
display: table-cell;
vertical-align: middle;
}
.child
{
width: 100px;
height: 100px;
background: rgba(225, 255, 43, 0.26);
}
</style>
</head>
<body>
<div class="parent">
<div class="child">MM</div>
</div>
</body>
</html>
网友评论