編碼的世界 / 優質文選 / 生涯

HTML/CSS 三個小盒子在一個大盒子中水平居中


2022年5月04日
-   

HTML/CSS 三個小盒子在一個大盒子中水平居中
方法一:把子盒子變成行內塊
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.big {
width: 600px;
height: 600px;
border: 1px solid red;
text-align: center;
font-size: 0;
}
.one, .two, .three {
display: inline-block;
width: 100px;
height: 100px;
border: 1px solid blue;
}
</style>
</head>
<body>
<div class="big">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
</body>
</html>

顯示: 方法二:給三個小盒子再套一個盒子。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.big {
width: 600px;
height: 600px;
border: 1px solid red;

}
.brace {
width: 398px;
height: 300px;
margin: 0 auto;
}
.one, .two, .three {
float: left;
width: 100px;
height: 100px;
border: 1px solid blue;
}
</style>
</head>
<body>
<div class="big">
<div class="brace">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
</div>
</body>
</html>

顯示:

熱門文章