大网数据广告活动
Web 标准与前端开发

margin-bottom

· 52CE 我爱测

The effect of the CSS margin-bottom property on the element box

The effect of the CSS margin-bottom property on the element box

CSS 的 margin-bottom 属性用于设置元素的底部外边距,允许设置负数值。一个正数值将让它相对于正常流与邻近块更远,而负数值将使得更近。

该属性对于*不可替代 (non-replaced 元素在规范中有概念,请自行搜索)*的行级元素没有效果,比如: tt 或者 span.

语法

/* <length> values */
margin-bottom: 10px; /* 一个绝对的长度值 */
margin-bottom: 1em; /* A length relative to the text size */
margin-bottom: 5%; /* A margin relative to the nearest block container's width */

/* Keyword values */
margin-bottom: auto;

/* Global values */
margin-bottom: inherit;
margin-bottom: initial;
margin-bottom: unset;

值

  • <length>
    • : 定义了一个确定的宽度值,参见 <length> 以了解更多可能的值。
  • <percentage>
    • : <percentage> 始终与包含该元素的容器宽度有关。
  • auto
    • : 由浏览器自己选择一个合适的值。参见 margin。

形式语法

示例

HTML

<div class="container">
  <div class="box0">Box 0</div>
  <div class="box1">Box 1</div>
  <div class="box2">Box one's negative margin pulls me up</div>
</div>

CSS

用于设置 div 的 margin-bottom 和 height 的 CSS 代码

.box0 {
  margin-bottom: 1em;
  height: 3em;
}
.box1 {
  margin-bottom: -1.5em;
  height: 4em;
}
.box2 {
  border: 1px dashed black;
  border-width: 1px 0;
  margin-bottom: 2em;
}

为了使 margin 产生的影响更明显,添加了一些 container 和 div 的样式定义

.container {
  background-color: orange;
  width: 320px;
  border: 1px solid black;
}
div {
  width: 320px;
  background-color: gold;
}

示例

规范

浏览器兼容性

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。