margin: 0 auto 中 auto 的作用是什么?

发布于 2024-09-07 21:47:19 字数 116 浏览 4 评论 0原文

automargin: 0 auto; 中做什么?

我似乎无法理解 auto 的作用。我知道它有时具有使对象居中的效果。

What does auto do in margin: 0 auto;?

I can't seem to understand what auto does. I know it sometimes has the effect of centring objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

别忘他 2024-09-14 21:47:19

当您在已应用 margin: 0 auto 的对象上指定宽度时,该对象将位于其父容器的中央。

指定 auto 作为第二个参数基本上告诉浏览器自动确定左右边距,这是通过将它们设置为相等来实现的。它保证左边距和右边距设置为相同的大小。第一个参数 0 表示上边距和下边距都将设置为 0。

margin-top: 0;
margin-bottom: 0;
margin-left: auto;
margin-right: auto;

因此,举个例子,如果父级为 100px,子级为 50px,则 auto 属性将确定 margin-leftmargin-right 之间有 50px 的可用空间可供共享:

var freeSpace = 100 - 50;
var equalShare = freeSpace / 2;

这将给出:

margin-left: 25;
margin-right: 25;

看看这个 jsFiddle。您不必指定父对象的宽度,只需指定子对象的宽度。

When you have specified a width on the object that you have applied margin: 0 auto to, the object will sit centrally within its parent container.

Specifying auto as the second parameter basically tells the browser to automatically determine the left and right margins itself, which it does by setting them equally. It guarantees that the left and right margins will be set to the same size. The first parameter 0 indicates that the top and bottom margins will both be set to 0.

margin-top: 0;
margin-bottom: 0;
margin-left: auto;
margin-right: auto;

Therefore, to give you an example, if the parent is 100px and the child is 50px, then the auto property will determine that there's 50px of free space to share between margin-left and margin-right:

var freeSpace = 100 - 50;
var equalShare = freeSpace / 2;

Which would give:

margin-left: 25;
margin-right: 25;

Have a look at this jsFiddle. You do not have to specify the parent width, only the width of the child object.

心欲静而疯不止 2024-09-14 21:47:19

auto:浏览器设置边距。其结果取决于浏览器。

margin:0 auto 指定:

  • 上下边距为 0
  • 左右边距为自动

auto: The browser sets the margin. The result of this is dependent on the browser.

margin:0 auto specifies:

  • top and bottom margins are 0
  • right and left margins are auto
肤浅与狂妄 2024-09-14 21:47:19

来自 CSS 规范 计算块级、非替换元素的宽度和边距正常流程

如果“margin-left”和“margin-right”都是“auto”,则它们使用的值相等。这使元素相对于包含块的边缘水平居中。

From the CSS specification on Calculating widths and margins for Block-level, non-replaced elements in normal flow:

If both 'margin-left' and 'margin-right' are 'auto', their used values are equal. This horizontally centers the element with respect to the edges of the containing block.

白况 2024-09-14 21:47:19
margin:0 auto;

0 表示上下,auto 表示左右。这意味着左右边距将根据元素的宽度和容器的宽度采取自动边距。

一般来说,如果您想将任何元素放在中心位置,那么 margin:auto 就可以完美工作。但它只适用于块元素。

margin:0 auto;

0 is for top-bottom and auto for left-right. It means that left and right margin will take auto margin according to the width of the element and the width of the container.

Generally if you want to put any element at center position then margin:auto works perfectly. But it only works in block elements.

蓝咒 2024-09-14 21:47:19

通过对这两个值如何工作的一些解释,它会变得更加清晰。

margin 属性是以下内容的简写:

margin-top
margin-right
margin-bottom
margin-left

那么为什么只有两个值呢?

好吧,你可以用这样的四个值来表达边距:

margin: 10px, 20px, 15px, 5px;

这意味着顶部 10 像素,右边 20 像素,底部 15 像素,左边 5 像素

同样,你也可以用这样的两个值来表达:

margin: 20px 10px;

这会给你一个顶部和底部 20 像素以及左边 10 像素的边距对了。

如果你设置:

margin: 20px auto;

那么这意味着顶部和底部边距为 20px,左右边距为 auto。 auto 表示左/右边距根据容器自动设置。如果您的元素是块类型元素,这意味着它是一个框并占据视图的整个宽度,则 auto 将左右边距设置为相同,因此元素居中。

It becomes clearer with some explanation of how the two values work.

The margin property is shorthand for:

margin-top
margin-right
margin-bottom
margin-left

So how come only two values?

Well, you can express margin with four values like this:

margin: 10px, 20px, 15px, 5px;

which would mean 10px top, 20px right, 15px bottom, 5px left

Likewise you can also express with two values like this:

margin: 20px 10px;

This would give you a margin 20px top and bottom and 10px left and right.

And if you set:

margin: 20px auto;

Then that means top and bottom margin of 20px and left and right margin of auto. And auto means that the left/right margin are automatically set based on the container. If your element is a block type element, meaning it is a box and takes up the entire width of the view, then auto sets the left and right margin the same and hence the element is centered.

短叹 2024-09-14 21:47:19

中的 auto

margin: 0 auto;

告诉浏览器自动设置元素的 margin-leftmargin-right 属性,浏览器通过提供两个边距来完成此操作相同的值。

需要注意的一些重要事项是:

  1. 它只能用于具有指定宽度的块级元素:

    a.如果未指定宽度,则左右边距将设置为 0,因为块级元素占据容器的整个宽度。
    输入图片此处描述

    b.对于 inlineinline-block 元素,没有可用于设置边距的水平空间,因为前后存在其他内联元素。
    输入图片此处描述

  2. auto 仅对于水平居中有用,因此使用 margin: auto 0;居中垂直的一个元素。 来源

.card {
  width: 400px;
  height: 100px;
  background-color: yellow;
}

.box {
  width: 30px;
  height: 20px;
  background-color: red;
  margin: 0 auto;
  /* margin: auto 0; */
  /* display: inline-block; */
}
<div class="card">
  <div class="box">Box</div>
</div>

The auto in

margin: 0 auto;

tells the browser to set the margin-left and margin-right properties of the element automatically which the browser accomplishes by giving both margins the same value.

Some important things to note are:

  1. It can only be used for block-level elements having specified width:

    a. If the width is not specified, the left and right margins would be set to 0 as block-level elements take up the entire width of the container.
    enter image description here

    b. For inline or inline-block elements, there is no horizontal space available to set the margin as there are other inline elements present before and after.
    enter image description here

  2. auto is useful only for horizontal centering, so using margin: auto 0; will NOT center an element vertically. source

.card {
  width: 400px;
  height: 100px;
  background-color: yellow;
}

.box {
  width: 30px;
  height: 20px;
  background-color: red;
  margin: 0 auto;
  /* margin: auto 0; */
  /* display: inline-block; */
}
<div class="card">
  <div class="box">Box</div>
</div>

太阳公公是暖光 2024-09-14 21:47:19
margin-top:0;
margin-bottom:0;
margin-left:auto;
margin-right:auto;

0 表示上下,auto 表示左右。
浏览器设置边距。

margin-top:0;
margin-bottom:0;
margin-left:auto;
margin-right:auto;

0 is for top-bottom and auto for left-right.
The browser sets the margin.

一抹微笑 2024-09-14 21:47:19
margin: 0 auto 

它是一个 CSS 属性,将元素的顶部和底部边距设置为 0 并将其在其父元素内水平居中。

此技术通常用于将块级元素(例如 div 或图像)居中。

margin: 0 auto 

It is a CSS property that sets the top and bottom margins of an element to 0 and centers it horizontally within its parent element.

This technique is often used to center a block-level element such as a div or a image.

情丝乱 2024-09-14 21:47:19

它是“中心”标签的损坏/非常难以使用的替代品。当您需要破碎的表格以及块和文本无法居中时,它会派上用场。

It is a broken/very hard to use replacement for the "center" tag. It comes in handy when you need broken tables and non-working centering for blocks and text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文