<style type="text/css" rel="stylesheet">
.content {
color: white;
}
.cell {
background-color: blue;
display: table-cell;
}
</style>
<div class="content">
<div class="cell">
test
</div>
</div>
.cell {
background-color: blue;
display: table-cell;
width: 100px;
height: 100px;
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>cell</title>
</head>
<body>
<style type="text/css" rel="stylesheet">
.content {
color: white;
}
.table {
display: table;
width: 100%;
height: 200px;
}
.cell {
background-color: blue;
display: table-cell;
width: 100%;
height: 100%;
}
</style>
<div class="content">
<div class="table">
<div class="cell">
test
</div>
</div>
</div>
</body>
</html>
.cell {
background-color: blue;
display: table-cell;
width: 100%;
height: 100%;
text-align: center;
}
.cell {
background-color: blue;
display: table-cell;
width: 100%;
height: 100%;
text-align: center;
vertical-align: middle;
}
注意:设置 line-height: 100%;无效,无法让文字垂直居中,设置 line-height: 200px;也可以垂直居中
原文:https://www.cnblogs.com/cowboybusy/p/11459408.html