無論什么元素,都會(huì)生成一個(gè)矩形框來顯示內(nèi)容,為“元素框”
元素框的中心區(qū)域是內(nèi)容框
內(nèi)邊距,邊框,外邊距都是可選的,因?yàn)樗麄兊膶挾仍试S為0
(1)內(nèi)容區(qū)(content
):必須要有
(2)內(nèi)邊距(padding
):內(nèi)容與邊框的填充區(qū)域
(3)邊框(border
):可以將內(nèi)容區(qū)與外界進(jìn)行隔離
(4)外邊距(margin
):多個(gè)盒子之間的間隙
(1)padding
,margin
,border
的每一條邊都可以單獨(dú)設(shè)置屬性
(2)pading
和margin
是背景透明的所以只能設(shè)置寬度,不能設(shè)置顏色與樣式
(1)常規(guī)屬性
序號(hào) | 名稱 | 描述 |
---|---|---|
1 | padding-top | 上內(nèi)邊距 |
1 | padding-right | 右內(nèi)邊距 |
1 | padding-botten | 下內(nèi)邊距 |
1 | padding-left | 左內(nèi)邊距 |
(2)屬性簡(jiǎn)寫
序號(hào) | 值數(shù)量 | 舉例 | 描述 |
---|---|---|---|
1 | 四值 | padding:5px 10px 5px 10px | 上->右->下->左 |
2 | 三值 | padding:5px 10px 5px | 上->左右->下 |
3 | 雙值 | padding:5px 10px | 上下->左右 |
4 | 單值 | padding:5px | 上下左右都有 |
(1)常規(guī)屬性
序號(hào) | 名稱 | 描述 |
---|---|---|
1 | margin-top | 上外邊距 |
2 | margin-right | 右外邊距 |
3 | margin-bottom | 下外邊距 |
4 | margin-left | 左外邊距 |
(2)屬性簡(jiǎn)寫
序號(hào) | 值數(shù)量 | 舉例 | 描述 |
---|---|---|---|
1 | 四值 | padding:5px 10px 5px 10px | 上->右->下->左 |
2 | 三值 | padding:5px 10px 5px | 上->左右->下 |
3 | 雙值 | padding:5px 10px | 上下->左右 |
4 | 單值 | padding:5px | 上下左右都有 |
border
可以設(shè)置寬度,也可以設(shè)置樣式和顏色
序號(hào) | 名稱 | 描述 |
---|---|---|
1 | border-top-width: 1px | 上邊框?qū)挾?/td> |
2 | border-top-style: solid | 上邊框樣式 |
3 | border-top-color: black | 上邊框前景色 |
4 | border-top: 1px solid black | 上邊框?qū)傩院?jiǎn)寫 |
5 | border-right: 1px solid green | 右邊框?qū)傩院?jiǎn)寫 |
6 | border-bottom: 1px solid grey | 下邊框?qū)傩院?jiǎn)寫 |
7 | border-left: 1px solid skyblue | 左邊框?qū)傩院?jiǎn)寫 |
(1)輪廓outline
: 位于 border
與 margin
之間,因?yàn)椴徽伎臻g, 可暫時(shí)忽略
(2)外邊距始終是透明的,可能透過它看到父元素
(3)輪廓沒有針對(duì)各條邊的屬性,只能統(tǒng)一設(shè)置
display
屬性(1)display: 屬性, 默認(rèn)值inline
,適用所有元素, 不能繼承
(2)display 改變的是顯示方式, 并不能改變?cè)氐谋举|(zhì)
(3)例如,塊級(jí)元素不允許做為行內(nèi)元素后代, 并不會(huì)因?yàn)樗@示為行內(nèi)塊而改變
(1)塊級(jí)框?qū)挾?/strong>,其實(shí)就是內(nèi)容區(qū)寬度,由左內(nèi)邊界到右內(nèi)邊界的距離, 高度也一樣
(2)元素內(nèi)容寬度可以用box-sizing
進(jìn)行調(diào)整,默認(rèn)為內(nèi)容寬度(content-box)
box-sizing
屬性(1)box-sizing
: 指示瀏覽器如何計(jì)算一個(gè)元素的總寬度和總高度
(2)盒模型中,元素的width/height
默認(rèn)只會(huì)應(yīng)用到”內(nèi)容區(qū)”
(3)當(dāng)盒子中存在padding/border
時(shí),計(jì)算盒子總大小非常麻煩
序號(hào) | 屬性值 | 描述 |
---|---|---|
1 | content-box | 默認(rèn)值,width/height 只應(yīng)用到內(nèi)容區(qū) |
1 | border-box | width/height 還包括padding ,border |
(1)width
總寬度是不變的, 寬度計(jì)算邊界在邊框上,所以 width=broder+padding+content
(2)box-sizing
: 適用于所有能設(shè)置 width
和 height
的所有元素
(3)box-sizing
: 通常只適用于塊級(jí), 也適合置換元素和行內(nèi)塊元素(因?yàn)槎伎梢栽O(shè)置寬高)
序號(hào) | 屬性 | 默認(rèn)值 | 描述 |
---|---|---|---|
1 | margin-left | auto | 左外邊距, 正負(fù)均可 |
2 | border-left | 0 | 左邊框 |
3 | padding-left | 0 | 左內(nèi)邊距 |
4 | width | auto | 內(nèi)容區(qū)寬度, 必須正值 |
5 | padding-right | 0 | 右內(nèi)邊距 |
6 | border-right | 0 | 右邊框 |
7 | margin-right | auto | 右外邊距, 正負(fù)均可 |
這七個(gè)屬性中,只有內(nèi)容區(qū)和左右外邊距允許auto
,其他的,要么0,要么具體值
<style>
div:first-of-type {
background-color: lightgreen;
margin-left: auto;
margin-left: 10px;
}
</style>
序號(hào) | 屬性 | 默認(rèn)值 | 描述 |
---|---|---|---|
1 | margin-top | auto | 上外邊距, 正負(fù)均可 |
2 | border-top | 0 | 上邊框 |
3 | padding-top | 0 | 上內(nèi)邊距 |
4 | height | auto | 內(nèi)容區(qū)高度, 必須正值 |
5 | padding-bottom | 0 | 下內(nèi)邊距 |
6 | border-bottom | 0 | 下邊框 |
7 | margin-bottom | auto | 下外邊距, 正負(fù)均可 |
<style> div:first-of-type { background-color: lightgreen; margin-bottom: auto; margin-bottom: 10px; } </style>
auto
會(huì)被解析為0
(1)橫向格式化時(shí), 左右外邊距值為auto
時(shí), 由瀏覽器根據(jù)父元素空間自動(dòng)計(jì)算
(2)縱向格式化時(shí), 上下外邊距值為auto
時(shí), 瀏覽器會(huì)將它強(qiáng)制設(shè)置為0
(3)這也是塊元素?zé)o法直接設(shè)置”垂直居中”的原因(水平居中可以輕易實(shí)現(xiàn))
(1)當(dāng)多個(gè)元素框垂直方向排列時(shí), 會(huì)出現(xiàn)縱向外邊距折疊現(xiàn)象
(2)此時(shí), 二個(gè)元素框的上下外邊距為 PK, 最終大者勝出
content
),內(nèi)邊距(padding
),外框(border
),外邊距(margin
)組成的display
屬性auto
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)