CSS absolute position won't work with margin-left:auto margin-right: auto

HtmlCssPosition

Html Problem Overview


Say you have the following CSS applied to a div tag

.divtagABS {
    position: absolute;
    margin-left: auto;  
    margin-right: auto;
}

the margin-left and margin-right does not take effect

but if you have relative, it works fine i.e.

.divtagREL {
    position: relative;
    margin-left: auto;  
    margin-right: auto;
}

Why is that? I just want to center an element.

Can someone explain why setting margins to auto in absolute position does not work?

Html Solutions


Solution 1 - Html

EDIT : this answer used to claim that it isn't possible to center an absolutely positioned element with margin: auto;, but this simply isn't true. Because this is the most up-voted and accepted answer, I guessed I'd just change it to be correct.

When you apply the following CSS to an element

position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;

And then give the element a fixed width and height, such as 200px or 40%, the element will center itself.

Here's a Fiddle that demonstrates the effect.

Solution 2 - Html

I've used this trick to center an absolutely positioned element. Though, you have to know the element's width.

.divtagABS {
    width: 100px;
    position: absolute;
    left: 50%;
    margin-left: -50px;
  }

Basically, you use left: 50%, then back it out half of it's width with a negative margin.

Solution 3 - Html

if the absolute element has a width,you can use the code below

.divtagABS{
    width:300px;
    positon:absolute;
    left:0;
    right:0;
    margin:0 auto;
}

Solution 4 - Html

Working JSFiddle below. When using position absolute, margin: 0 auto will not work, but you can do like this (will also scale):

left: 50%;
transform: translateX(-50%);

Update: Working JSFiddle

Solution 5 - Html

I already had this same issue and I've got the solution writing a container (.divtagABS-container, in your case) absolutely positioned and then relatively positioning the content inside it (.divtagABS, in your case).

Done! The margin-left and margin-right AUTO for your .divtagABS will now work.

Solution 6 - Html

All answers were just a suggested solutions or workarounds. But still don't get answer to the question: why margin:auto works with position:relative but does not with position:absolute.

Following explanation was helpful for me:

"Margins make little sense on absolutely positioned elements since such elements are removed from the normal flow, thus they cannot push away any other elements on the page. Using margins like this can only affect the placement of the element to which the margin is applied, not any other element." http://www.justskins.com/forums/css-margins-and-absolute-82168.html

Solution 7 - Html

This issue can be confusing until you realize some nuances of different positionings.

Margins work similarly for relative and absolute elements, but margins are relative to a 'bounding box.' You have to consider what is the bounding box of the element, to apply margins against.

  • For a relative positioned element, the bounding box is its parent element.
  • For an absolute positioned element, the bounding box is itself.
  • For a relative positioned element, left/right/top/bottom rules position the element itself.
  • For an absolute positioned element, left/right/top/bottom rules position the element's bounding box.

This is why to center a relative positioned element, you only have to set margins and it works.

To center an absolute positioned element, you have to set the margins, and also set the bounding box (left/right/top/bottom).

Solution 8 - Html

If the element is position absolutely, then it isn't relative, or in reference to any object - including the page itself. So margin: auto; can't decide where the middle is.

Its waiting to be told explicitly, using left and top where to position itself.

You can still center it programatically, using javascript or somesuch.

Solution 9 - Html

When you are defining styles for division which is positioned absolutely, they specifying margins are useless. Because they are no longer inside the regular DOM tree.

You can use float to do the trick.

.divtagABS {
    float: left;
    margin-left: auto;  
    margin-right:auto;
 }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1118019View Question on Stackoverflow
Solution 1 - HtmlKevin BowersoxView Answer on Stackoverflow
Solution 2 - HtmlchipcullenView Answer on Stackoverflow
Solution 3 - HtmlGilbertSunView Answer on Stackoverflow
Solution 4 - HtmlBaked InhalfView Answer on Stackoverflow
Solution 5 - HtmlRafael CamargoView Answer on Stackoverflow
Solution 6 - HtmlAlex VovchukView Answer on Stackoverflow
Solution 7 - HtmlmichaeljsaloView Answer on Stackoverflow
Solution 8 - HtmlCodecraftView Answer on Stackoverflow
Solution 9 - HtmlStarxView Answer on Stackoverflow