Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. CSS:层叠样式表
  3. CSS 参考
  4. 属性
  5. margin-left

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

margin-left

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

* Some parts of this feature may have varying levels of support.

margin-leftCSS 属性设置元素左侧的外边距区域。正值使其远离相邻元素,负值使其更加靠近。

尝试一下

margin-left: 1em;
margin-left: 10%;
margin-left: 10px;
margin-left: 0;
<section>  <div>    <div></div>    <div></div>    <div></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: rgba(229, 232, 252, 0.6);  flex-shrink: 0;}#example-element {  border: solid 10px #ffc129;  background-color: rgba(255, 244, 219, 0.6);}

两个相邻的盒子的垂直边距可能会合并。这被称为外边距折叠

在宽度过度约束的极少数情况下(即,当widthmargin-leftborderpadding、内容区域和margin-right 都已被定义),margin-left 会被忽略,如果被指定为auto 值,则会有相同的计算值。

语法

css
/* <length> 值 */margin-left: 10px; /* 绝对长度 */margin-left: 1em; /* 相对于文本大小 */margin-left: 5%; /* 相对于最近的块级容器的宽度 *//* 关键字值 */margin-left: auto;/* 全局值 */margin-left: inherit;margin-left: initial;margin-left: revert;margin-left: revert-layer;margin-left: unset;

margin-left 属性可以被指定为关键字auto<length><percentage>。它的值可以是正值、零或负值。

<length>

外边距的大小为固定值。

<percentage>

外边距的大小为相对于包含块的行级尺寸(由writing-mode 定义的水平文字的宽度)的百分比。

auto

左边距接收未使用的水平空间中的一部分(这主要由所使用的布局模式确定)。如果margin-leftmargin-right 的值都是auto,则最后计算的空间是均匀分布的。下表总结了不同的情况:

display 的值float 的值position 的值auto 的计算值注释
inlineinline-blockinline-table任意staticrelative0行级布局模式
blockinlineinline-blockblocktableinline-tablelist-itemtable-caption任意staticrelative0,除非将margin-leftmargin-right 都设置为auto。在这种情况下,它被设置为将元素置于其父级元素中心的值。块级布局模式
blockinlineinline-blockblocktableinline-tablelist-itemtable-captionleftrightstaticrelative0块级布局模式(浮动元素)
table-* 中的任意一个,除了table-caption任意任意0 内部的table-* 元素没有外边距,请使用border-spacing 代替。
任意,除了flexinline-flextable-*任意fixedabsolute0,除非将margin-leftmargin-right 都设置为auto。在这种情况下,如果width 固定,它会被设置为将边框区域在可用宽度内居中的值。绝对定位布局模式
flexinline-flex任意任意0,除非存在任何正水平可用空间。在这种情况下,它会均匀分配到所有水平的auto 外边距中。弹性盒布局模式

形式定义

初始值0
适用元素all elements, except elements with tabledisplay types other thantable-caption,table andinline-table. It also applies to::first-letter.
是否是继承属性
Percentagesrefer to the width of the containing block
计算值the percentage as specified or the absolute length
动画类型alength

形式语法

margin-left =
<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

示例

使用百分比值设置 margin-left

margin-left 的百分比值代表相对于容器的行级尺寸尺寸。

HTML

html
<p>  靠近花园的入口处有一株大玫瑰树,树上的花是白色的,三个园丁正忙着把花儿染红。</p><p>  爱丽丝觉得很奇怪,就想走过去仔细观察。她刚要走到他们面前,就听到其中一个人说:“小心点,老五!别把颜料溅到我身上。”</p><p>“我也没办法,”老五生气地说,“是老七碰了我的胳膊。”</p>

CSS

css
.example {  margin-left: 50%;}

结果

规范

Specification
CSS Box Model Module Level 3
# margin-physical

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp