与盒模型相关的属性,包括 margin、padding、border、width、height 等。
下面是盒模型属性的完整列表,点击属性名可跳转到 MDN 文档查看详细信息。
属性名 | 描述 | 语法 |
---|
width | 设置元素的宽度 | width: auto | <length> | <percentage> | min-content | max-content | fit-content | fit-content(<length-percentage>) |
height | 设置元素的高度 | height: auto | <length> | <percentage> | min-content | max-content | fit-content | fit-content(<length-percentage>) |
min-width | 设置元素的最小宽度 | min-width: auto | <length> | <percentage> | min-content | max-content | fit-content | fit-content(<length-percentage>) |
min-height | 设置元素的最小高度 | min-height: auto | <length> | <percentage> | min-content | max-content | fit-content | fit-content(<length-percentage>) |
max-width | 设置元素的最大宽度 | max-width: none | <length> | <percentage> | min-content | max-content | fit-content | fit-content(<length-percentage>) |
max-height | 设置元素的最大高度 | max-height: none | <length> | <percentage> | min-content | max-content | fit-content | fit-content(<length-percentage>) |
box-sizing | 定义元素的尺寸计算方式 | box-sizing: content-box | border-box |
属性名 | 描述 | 语法 |
---|
margin | 设置元素的外边距 | margin: <length> | <percentage> | auto |
margin-top | 设置元素的上外边距 | margin-top: <length> | <percentage> | auto |
margin-right | 设置元素的右外边距 | margin-right: <length> | <percentage> | auto |
margin-bottom | 设置元素的下外边距 | margin-bottom: <length> | <percentage> | auto |
margin-left | 设置元素的左外边距 | margin-left: <length> | <percentage> | auto |
margin-inline | 设置元素的行内方向外边距 | margin-inline: <length> | <percentage> | auto |
margin-inline-start | 设置元素的行内起始外边距 | margin-inline-start: <length> | <percentage> | auto |
margin-inline-end | 设置元素的行内结束外边距 | margin-inline-end: <length> | <percentage> | auto |
margin-block | 设置元素的块级方向外边距 | margin-block: <length> | <percentage> | auto |
margin-block-start | 设置元素的块级起始外边距 | margin-block-start: <length> | <percentage> | auto |
margin-block-end | 设置元素的块级结束外边距 | margin-block-end: <length> | <percentage> | auto |
属性名 | 描述 | 语法 |
---|
border | 设置元素的边框 | border: <line-width> || <line-style> || <color> |
border-width | 设置元素的边框宽度 | border-width: <line-width>{1,4} |
border-style | 设置元素的边框样式 | border-style: <line-style>{1,4} |
border-color | 设置元素的边框颜色 | border-color: <color>{1,4} |
border-top | 设置元素的上边框 | border-top: <line-width> || <line-style> || <color> |
border-right | 设置元素的右边框 | border-right: <line-width> || <line-style> || <color> |
border-bottom | 设置元素的下边框 | border-bottom: <line-width> || <line-style> || <color> |
border-left | 设置元素的左边框 | border-left: <line-width> || <line-style> || <color> |
border-radius | 设置元素的边框圆角 | border-radius: <length-percentage>{1,4} [ / <length-percentage>{1,4} ]? |
border-top-left-radius | 设置元素的左上角边框圆角 | border-top-left-radius: <length-percentage> [ <length-percentage> ]? |
border-top-right-radius | 设置元素的右上角边框圆角 | border-top-right-radius: <length-percentage> [ <length-percentage> ]? |
border-bottom-right-radius | 设置元素的右下角边框圆角 | border-bottom-right-radius: <length-percentage> [ <length-percentage> ]? |
border-bottom-left-radius | 设置元素的左下角边框圆角 | border-bottom-left-radius: <length-percentage> [ <length-percentage> ]? |
/* 基本盒模型示例 */
.box {
/* 尺寸 */
width: 300px;
height: 200px;
box-sizing: border-box;
/* 外边距 */
margin: 20px;
/* 边框 */
border: 2px solid #333;
border-radius: 8px;
/* 内边距 */
padding: 15px;
/* 轮廓 */
outline: 1px dashed red;
outline-offset: 5px;
}
/* 逻辑属性示例 */
.logical-box {
margin-inline: 20px;
padding-block: 15px;
border-inline-start: 2px solid blue;
}
大多数现代浏览器都支持这些盒模型属性。逻辑属性(如 margin-inline、padding-block 等)在较新的浏览器中得到支持。具体兼容性信息请参考各属性的 MDN 文档。