<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<style>
.box{
width: 500px;
height: 500px;
border: 3px solid #f00;
/* position: relative; */
text-align: center;
line-height: 500px;
}
.small{
width: 100px;
height: 100px;
background-color: #00f;
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}
.small{
width: 100px;
height: 100px;
background-color: #00f;
position: absolute;
left: 50%;
top: 50%;
margin: -50px 0 0 -50px;
transform: translate(-50%, -50%);
}
/* 第三种 */
.small{
display: flex;
justify-content: center;
align-items: center;
}
.box{
width: 500px;
height: 500px;
border: 3px solid #f00;
/* position: relative; */
text-align: center;
line-height: 500px;
}
/* 第四种 */
.small{
width: 100px;
height: 100px;
background-color: #00f;
display: inline-block;
/*
修饰的是行内块
垂直的对齐方式
参考谁进行垂直对齐 - 当前行
*/
vertical-align: middle;
}
</style>
<div class="box">
<div class="small"></div>
<!-- <span></span> -->
</div>
</body>
<script>
</script>
</html>
/* 第五种 */
<body>
<style>
.small{
width: 100px;
height: 100px;
background-color: #f00;
grid-area: a;
}
.box{
width: 300px;
height: 300px;
border: 1px solid #000;
display: grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-template-areas: ". . ." ". a ." ". . .";
}
</style>
<div class="box">
<div class="small"></div>
</div>
</body>