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

9

109

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?

Pless answered 3/4, 2012 at 17:22 Comment(3)
just use margin: auto;. with absolute position, all other style elements(regarding position/etc) are ignored.Hautemarne
Wrap it in a <center> tag and set the width of .divtagABS to 100%.Chitter
I can confirm: even though some say that this statement is outdated, I noticed a not-centering failure for position: absolute; on IE Edge.Kaylenekayley
M
210

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.

Miscalculate answered 3/4, 2012 at 17:25 Comment(8)
url: vision.to/articles/margins-and-absolute-positioning.php is not working, please replaceUtah
courtesy of wayback machine: web.archive.org/web/20130117135705/http://www.vision.to/…Maltreat
"An element with position absolute cannot be centered..." Actually, this is not true: The key is to set all top/bottom/left/right properties to 0 and declare width and height, then margin: auto will automatically center the absolutely positioned element. See: Absolute Centering technique; it's also on Smashing Magazine.Spouse
And in order to make a width pseudo-dynamic, use width: 100%; and (i.e.) max-width: 500px;.Kaylenekayley
Yep you need to add width: 100% to make it work with edge and IE9-11.Johnajohnath
I think you only need to set left, right and a (max)width for it to center, No need for bottom or top for horizontal alignment. Although for best practice you should set at least one of those so browsers cant misinterpretCreation
I managed to center my element thanks to this answer BUT setting top AND bottom poses a problem for me. It makes the element not the right height (content overflows). Better to set only one of them. Actually just left and right are needed for the horizontal centering with margin: X auto.Irs
And you can use inset: 0 instead of setting all 4 values (top - right - bottom - left) to 0. Not available for IE, unfortunately.Graeme
B
72

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.

Begone answered 3/4, 2012 at 17:27 Comment(7)
Yep, this is what you should use if the element has a fixed width.Derinna
You can use margin-left:-25%; to center it instead of hard-coding a number.Haworth
@AaronHarun - sadly, no that won't work the way you expect. I think the 25% will be based on the parent's width. I set up a code pen to test it out: codepen.io/chippper/pen/xwKIl - you can toggle the last line of CSS to see what I mean.Begone
This triggered me into a similar trick but using margin-left: -50%;. Oddly enough, it fixed a very weird alignment issue for me!Effect
@Begone @AaronHarun You can use transform: translateX(-25%) which uses it's own width. https://mcmap.net/q/112691/-horizontally-center-absolute-positioned-element-below-the-center-of-another-element (As you noted, @chipcullen, width: -25% will use the parent element's width, typically not what you want.)Aplanatic
@Aplanatic - I think that is what the previous comment was alluding to. At the time I wrote this answer, transform wasn't well supported - thankfully that has changed! :)Begone
This works fine with max-width too. So it's flexible enough.Kenward
B
40

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;
}
Belligerency answered 13/10, 2013 at 4:45 Comment(5)
Brilliant ! But why isn't it working unless left and right are set ?Rosen
Very nice solution. Works also with width:100% under Bootstrap ;)Jigger
If you want to make it centre vertically and horizontally then just expand this to top: 0; bottom: 0; combine this with a CSS 3 transition for width and height and you have a pretty nice effectCrux
I think this is the horizontal case of the Absolute Centering technique (also: Smashing Magazine), which uses margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; (with declared width and height) for both horizontal and vertical centering. The linked articles explain how this method works and compares it to other centering methods.Spouse
@Rosen because left, right, top, and bottom are setted to auto by default and this, together with an absolute layout, actually prevent margin: auto to be noticeableCampanulate
I
25

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

Infringement answered 11/1, 2016 at 16:10 Comment(1)
Best work with Absolute position div's but have some issue in IE10 when positioning div has width more than parent width... :(Tammany
R
2

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

Raffinose answered 24/5, 2019 at 21:19 Comment(0)
H
2

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).

Hellas answered 18/3, 2022 at 19:37 Comment(0)
C
1

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.

Catacaustic answered 11/8, 2013 at 19:29 Comment(0)
S
-1

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.

Seventeenth answered 3/4, 2012 at 17:26 Comment(0)
C
-5

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;
 }
Consubstantial answered 3/4, 2012 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.