
批改狀態(tài):合格
老師批語:整體看上去寫的很不錯(cuò),繼續(xù)加油!
通過對生活中的盒子和網(wǎng)頁中單個(gè)模塊內(nèi)部結(jié)構(gòu)的分析,可以發(fā)現(xiàn)它們的結(jié)構(gòu)是非常相似的。
由內(nèi)到外分別是:外邊距、邊框、內(nèi)邊距、內(nèi)容區(qū)。
盒模型是用來布局網(wǎng)頁的重要容器。
網(wǎng)頁頁面中的元素都可以看成是一個(gè)盒子,占據(jù)一定的頁面空間
屬性 | 含義 |
---|---|
margin |
外邊距 |
border |
邊框 |
padding |
內(nèi)邊距(內(nèi)填充) |
width |
寬度 |
height |
高度 |
box-sizing |
自定義盒模型寬和高的規(guī)范 |
backgroumd-clip |
背景的延伸 |
<body>
<div class="box">box
<div class="box1">box1</div>
</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
</body>
<style>
div.box {
padding-top: 0;
width: 400px;
height: 400px;
background-color: #00f000;
}
.box1 {
width: 200px;
height: 200px;
margin-top: px;
background-color: lightblue;
}
.box2 {
padding: 100px;
margin: 100px;
width: 400px;
border: 20px solid green;
height: 400px;
background-color: lightcoral;
box-sizing: border-box;
}
.box3 {
padding: 100px;
margin: 100px;
width: 400px;
border: 5px solid green;
height: 400px;
background-color: lightcoral;
background-clip: content-box;
box-sizing: border-box;
}
</style>
<body>
<div class="box">box
<div class="box1">box1</div>
</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
</body>
<style>
div.box {
padding-top: 0;
width: 400px;
height: 400px;
background-color: #00f000;
}
.box1 {
width: 200px;
height: 200px;
margin-top: 100px;
background-color: lightblue;
}
.box2 {
padding: 100px;
margin: 100px;
width: 400px;
border: 20px solid green;
height: 400px;
background-color: lightcoral;
box-sizing: border-box;
}
.box3 {
padding: 100px;
margin: 100px;
width: 400px;
border: 5px solid green;
height: 400px;
background-color: lightcoral;
background-clip: content-box;
box-sizing: border-box;
}
</style>
<body>
<div class="box">
<div class="box1"></div>
</div>
<div class="box2"></div>
<div class="box3"></div>
</body>
<style>
div.box {
padding-top: 0;
padding:100px;
width: 400px;
height: 400px;
background-color: #00f000;
}
.box1 {
width: 200px;
height: 200px;
margin-top: px;
background-color: lightblue;
}
.box2 {
padding: 100px;
margin: 100px;
width: 400px;
border: 20px solid green;
height: 400px;
background-color: lightcoral;
box-sizing: border-box;
}
.box3 {
padding: 100px;
margin: 100px;
width: 400px;
border: 5px solid green;
height: 400px;
background-color: lightcoral;
background-clip: content-box;
box-sizing: border-box;
}
</style>
圖片文字有些錯(cuò)誤
box-sizing;content-box;
width(盒子視口)=border+padding+width(自定義)
height(盒子視口)=border+padding+height(自定義)
width(盒子)=border+padding+width(自定義)+margin
height(盒子)=border+padding+height(自定義)+margin
box-sizing: border-box;
width(盒子視口)=width(自定義)
height(盒子視口)=height(自定義)
width(盒子)=width(自定義)+margin
height(盒子)=height(自定義)+margin
margin-left:auto;
與margin-right:auto;
可以輕易的設(shè)置水平居中。
因?yàn)樵诖怪狈较蛏?,塊級元素不會自動擴(kuò)充,它的外部尺寸沒有自動充滿父元素,也沒有剩余空間可說。所以margin-top:auto;
與margin-bottom:auto
無法使其垂直居中。
可以簡寫margin:auto;
解決方法:
添加定位屬性
<style>
.box {
width: 400px;
height: 450px;
background-color: #ccc;
/*簡寫margin屬性*/
margin: auto;
/*添加定位屬性*/
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
</style>
了解了盒模型的構(gòu)成和屬性
外邊距的特殊用法(使塊級元素居中)
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號