
批改狀態(tài):合格
老師批語:還不錯,一天的作業(yè)寫在一起!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>margin:auto 塊元素垂直居中</title>
<style>
.container {
width: 300px;
height: 300px;
background-color: lightgreen;
position: relative;
}
.container .item {
width: 100px;
height: 100px;
background-color: violet;
/* 水平居中 */
/* margin-left: auto; */
/* margin-right: auto; */
/* 垂直居中,margin-top,margin bottom 0 */
/* margin-top: auto; */
/* margin-bottom: auto; */
/* 通過絕對定位元素實現(xiàn)垂直居中 */
position: absolute;
/* 讓當(dāng)前元素絕對定位的上下文充滿整個父級容器 */
top: 0;
left: 0;
right: 0;
bottom: 0;
/* 水平垂直居中 */
margin: auto;
}
</style>
</head>
<body>
<div class="container">
<div class="item"></div>
</div>
</body>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號