Background: I have been looking at some interview questions recently, and there are some solutions about horizontal and vertical centralization. I usually use Flex, and seldom use table-cell
If you really want to achieve vertical centering, table-cell alone is not enough
- Table-cell The width and height need to be set
- The child elements of the table-cell must be
display: inline-block;
Cannot be block-level elements
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>Document</title>
<style>
#id1 {
width: 100px;
height: 100px;
background: blue;
display: table-cell;
vertical-align: middle;
text-align: center;
}
#id2 {
background: red;
display: inline-block;
width: 50%;
height: 50%;
}
</style>
</head>
<body>
<div id="id2">
<div id="id3">Test the</div>
</div>
</body>
</html>
Copy the code