Reputation: 2538
How to center a “position: absolute” element in IE 9, 10, 11, my example do not work in Internet Explorer. Also this "position: absolute; left:50%; margin-left:-20px;" ,manner will not suit me, as the layout is responsive.
.box{
position: absolute;
top: 150px;
right: 0;
left: 0;
margin: auto;
}
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
Upvotes: 20
Views: 26976
Reputation: 1
You shouldn't have to set a bottom property (even in IE), to center that element horizontally.
However, if you wanted to vertically center the element, you would need the bottom property in conjunction with an auto margin for top and bottom.
If your element will only center horizontally with "bottom" set to 0, then there is something conflicting in your code forcing you to use that property. I have seen this happen before in Bootstraps Ecosystem when not applying the proper hierarchy of container --> row and so on.
Upvotes: 0
Reputation: 3629
I just struggled with this myself, and the specific key for me was changing the max-width
property to be a width
property. Adding a max-width doesn't break it, but it relies on the width
property in IE, apparently, where in Firefox it worked with just the max-width set.
I hope this helps someone else!
Upvotes: 3
Reputation: 15807
<body>
<div class="container">
<div class="box">
</div>
</div>
</body>
CSS
.box{
position: absolute;
top:0;
right: 0;
left: 0;
bottom:0; //specify all including bottom:0
margin: auto;
height:200px;
width:200px;
}
Upvotes: 26