margin-right
Baseline 广泛可用 *
试一试
margin-right: 1em;
margin-right: 10%;
margin-right: 10px;
margin-right: 0;
<section id="default-example">
<div id="container">
<div class="col"></div>
<div class="col transition-all" id="example-element"></div>
<div class="col"></div>
</div>
</section>
#container {
width: 300px;
height: 200px;
display: flex;
align-content: flex-start;
justify-content: flex-start;
}
.col {
width: 33.33%;
border: solid #5b6dcd 10px;
background-color: rgb(229 232 252 / 0.6);
flex-shrink: 0;
}
#example-element {
border: solid 10px #ffc129;
background-color: rgb(255 244 219 / 0.6);
}
两个相邻盒子的垂直外边距可能会融合。这称为外边距折叠。
语法
css
/* <length> values */
margin-right: 20px; /* An absolute length */
margin-right: 1em; /* relative to the text size */
margin-right: 5%; /* relative to the nearest block container's width */
margin-right: anchor-size(self-block);
margin-right: calc(anchor-size(--my-anchor height, 20px) / 4);
/* Keyword values */
margin-right: auto;
/* Global values */
margin-right: inherit;
margin-right: initial;
margin-right: revert;
margin-right: revert-layer;
margin-right: unset;
margin-right 属性被指定为关键字 auto,或一个 <length>,或一个 <percentage>。它的值可以是正数、零或负数。
值
<length>-
边距的大小为固定值。
- 对于锚点定位元素,
anchor-size()函数会解析为一个相对于相关锚点元素宽度或高度的<length>值(参见根据锚点大小设置元素边距)。
- 对于锚点定位元素,
<percentage>-
边距的大小为百分比,相对于包含块的内联尺寸(在水平书写模式下是宽度,由
writing-mode定义)。 auto-
右外边距会获得未使用的水平空间的一部分,这主要取决于所使用的布局模式。如果
margin-left和margin-right的值都为auto,则计算出的空间会均匀分布。此表总结了不同的情况display的值float的值position的值auto的计算值Comment inline、inline-block、inline-table任意 static或relative0内联布局模式 block、inline、inline-block、block、table、inline-table、list-item、table-caption任意 static或relative0,除非margin-left和margin-right都设置为auto。在这种情况下,它被设置为使元素在其父元素中居中的值。块布局模式 block、inline、inline-block、block、table、inline-table、list-item、table-captionleft或rightstatic或relative0块布局模式(浮动元素) 任何 table-*,除了table-caption任意 任意 0内部 table-*元素没有外边距,请改用border-spacing任何,除了 flex、inline-flex或table-*任意 fixed或absolute0,除非margin-left和margin-right都设置为auto。在这种情况下,如果固定,它被设置为在可用width中使边框区域居中的值。绝对定位布局模式 flex、inline-flex任意 任意 0,除非存在任何正的水平自由空间。在这种情况下,它会均匀地分配给所有水平auto外边距。弹性盒子布局模式
正式定义
正式语法
margin-right =
<length-percentage> |
auto |
<anchor-size()>
<length-percentage> =
<length> |
<percentage>
<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )
<anchor-name> =
<dashed-ident>
<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline
示例
使用像素和百分比设置右外边距
css
.content {
margin-right: 5%;
}
.side-box {
margin-right: 10px;
}
.logo {
margin-right: -5px;
}
规范
| 规范 |
|---|
| CSS Box Model Module Level 3 # margin-physical |
浏览器兼容性
加载中…