<!DOCTYPE html>
<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>
</head>
<body>
<div class="father">
<div class="son">
</div>
</div>
</body>
</html>
<style>
.father{
width: 300px;
height: 300px;
background-color: blue;
display: flex;
}
.son{
width: 200px;
height: 200px;
background-color: yellow;
margin: auto;
}
</style>
父元素设置display:flex,子元素设置margin:auto。可以实现水平垂直居中
最新推荐文章于 2023-03-08 14:27:02 发布
本文通过一个简单的示例展示了如何使用HTML和CSS实现居中布局的效果。父元素使用了flex布局,子元素通过auto margin实现了内容的居中显示。
345

被折叠的 条评论
为什么被折叠?



