Home Web Front-end CSS Tutorial What does margin:auto mean in css? Detailed explanation of the usage of margin:auto attribute

What does margin:auto mean in css? Detailed explanation of the usage of margin:auto attribute

Nov 01, 2018 pm 05:07 PM

We all know that using margin:auto can center the element horizontally. But have you ever thought about why using margin:auto can center an element horizontally? To answer this question, we first need to take a look at how margin:auto works. If auto is suitable for vertical centering, what is the situation? Let's look at the specific content.

First of all, what does auto do?

Define the auto element, which varies depending on the element type and context. In margins, auto can mean two things: taking up available space or 0 px. These two will define different layouts for the elements.

"Automatically" occupy available space

This is the most common way we use auto. By assigning the left and right margins of auto elements, they occupy the available horizontal space in the element's container equally - so the element will be centered.

<div id="outer">
<div id="inner"></div>
</div>
Copy after login
Copy after login
Copy after login
#inner {
  margin: auto;
  width: 250px;
  height: 125px;
  background-image: linear-gradient(45deg, #84ECEF 10%, #F8F62F 60%, #FDC018);
}
#outer {
  height: 500px;
  width: 500px;
  background: #1F1D20;
  background-image: linear-gradient(#757575 1px, transparent 1px), linear-gradient(90deg, #757575 1px, transparent 1px);
  background-size: 25px 25px;
}
Copy after login

The effect is as follows:

What does margin:auto mean in css? Detailed explanation of the usage of margin:auto attribute

However, this only works with horizontal margins, it does not work with floated and inline elements, and it does not work by itself Used for absolutely and fixedly positioned elements.

Recommended Manual: CSS Online Manual

Since the left and right margins of auto occupy the "available" space evenly, when you auto only give them What do you think will happen when one occurs?

Left or right margin auto will take up all "available" space, making the element appear to be offset to the right or left.

<div id="outer">
<div id="inner"></div>
</div>
Copy after login
Copy after login
Copy after login
#inner {
  margin-right: auto;
  width: 250px;
  height: 125px;
  background-image: linear-gradient(45deg, #84ECEF 10%, #F8F62F 60%, #FDC018);
}

#outer {
  height: 500px;
  width: 500px;
  background: #1F1D20;
  background-image: linear-gradient(#757575 1px, transparent 1px), linear-gradient(90deg, #757575 1px, transparent 1px);
  background-size: 25px 25px;
}
Copy after login

The effect is as follows:

What does margin:auto mean in css? Detailed explanation of the usage of margin:auto attribute

"auto" is 0px

As mentioned before, auto is floating , does not work in inline and absolute elements. All of these elements already have their layout determined, so there's no auto for margins and expecting it to be centered like this.

This would defeat the original purpose of using something like float. So set the auto value of these elements to 0px.

auto will also not work on a typical block element if it has no width. All the examples I've shown you so far have width.

The width auto of the value will have 0px margins. The width of a block element usually covers its entire container, which is auto or 100%, so margin:auto will be set to 0px.

What will it look like if auto is set vertically?

auto always evaluates to 0px in top and bottom margins (except for absolute elements). The W3C spec says it is this:

"If "margin-top" or "margin-bottom" is "auto", its usage value is 0"

So far, why This is not said. This is probably because of typical vertical page flow, where the page size increases in height. Therefore, centering an element vertically in its container relative to the page itself will not cause it to appear centered, unlike doing it horizontally (in most cases).

Perhaps for the same reason, they decided to add an exception for absolute elements, which can be vertically centered the entire height of the page.

This may also be due to an edge collapse effect (the collapse of the "edges" of adjacent elements), which is another exception to vertical margins.

However, the latter seems to be an unlikely scenario - since elements that don't collapse their margins - like Floats and overflow other elements visible, still have 0px vertical margin auto assigned to them.

Recommended related articles:
1.Causes and solutions why margin does not work in CSS
Recommended related videos:
1.CSS Video Tutorial-Jade Girl Heart Sutra Edition

center on the absolutely positioned element

Because the absolutely positioned element happens to have an exception , we will use the auto value to center vertically and horizontally. But before that, we need to find out when margin:auto actually works like we want it to in absolutely positioned elements.

This is where another W3C spec comes in:

"If all three of "left", "width" and "right" are "auto": first add " Any "auto" value of margin-left" and "margin-right" is set to 0... "

" if none of the three is "auto": If "margin-left" and "margin -right" are both "auto", then the equation is solved with the additional constraint that the two margins get equal values"

This pretty much says that for the margins of horizontal auto, the catch intervals are equal, Then the values ​​left, width and right should not be auto, their default values. So all we have to do is give them some value in the absolutely positioned elements. left and right should have the same value for perfect centering.

The spec also mentions something like vertical margins.

"If all three of "top", "height" and "bottom" are auto, set "top" to a static position..."

“如果三者中没有一个是”自动“:如果”margin-top“和”margin-bottom“都是”auto“,则在额外约束下解决方程式,即两个边距得到相等的值......”

因此,对于一个绝对元件被垂直居中,其top,height和bottom值不应该auto。

现在结合所有这些,这是我们将得到的:

<div id="outer">
<div id="inner"></div>
</div>
Copy after login
Copy after login
Copy after login
HTML
 
CSS
Result
EDIT ON#inner {
  margin: auto;
  position: absolute;
  left: 0px;
  right: 0px;
  bottom: 0px;
  top: 0px;
  width: 250px;
  height: 125px;
  background-image: linear-gradient(45deg, #84ECEF 10%, #F8F62F 60%, #FDC018);
}
#outer {
  position: relative;
  height: 500px;
  width: 500px;
  background: #1F1D20;
  background-image: linear-gradient(#757575 1px, transparent 1px), linear-gradient(90deg, #757575 1px, transparent 1px);
  background-size: 25px 25px;
}
Copy after login

效果如下:

What does margin:auto mean in css? Detailed explanation of the usage of margin:auto attribute

最后

如果您想要将页面上的元素向右或向左偏移而没有包含它的其他元素(就像浮点数一样),请记住有auto用于边距的选项。

将元素转换为绝对定位只是为了使它可以垂直居中可能不是一个好主意。还有其他选项,如flexbox和CSS变换,更适合那些。

The above is the detailed content of What does margin:auto mean in css? Detailed explanation of the usage of margin:auto attribute. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1240
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou&#039;s conic-gradient() polyfill was the last item:

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

See all articles