Reputation: 2538
I need to center absolute position div that has max-width
.
Here is an example. https://jsfiddle.net/allegrissimo123/3VHuK/1/
.mess{
text-align: center;
display: inline-block;
margin: 0 auto;
background: #212121;
color: #FFFFFF;
margin-bottom: 50px;
max-width: 350px;
position: absolute;
top: 40px;
left: 0px;
right: 0px;
z-index: 1000;
}
I test this in in IE9,10,11 but it does not work.
Upvotes: 12
Views: 12367
Reputation: 1867
Try this updated css. This will center align an absolute div vertically and horizontally.
CSS
.mess{
max-width: 350px;
max-height: 100px;
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom:0px;
margin:auto;
}
Upvotes: -1
Reputation: 13998
Assign width
for the class.
.mess{
text-align: center;
display: inline-block;
margin: 0 auto;
background: #212121;
color: #FFFFFF;
margin-bottom: 50px;
max-width: 350px;
position: absolute;
top: 40px;
left: 0px;
right: 0px;
z-index: 1000;
width:100%; /* add this */
}
Upvotes: 28
Reputation: 1
This will put your div in the center(Vertically and Horizontally). You don't have to provide margin in this case. The below code can be applied to any div regardless of it's width. Make sure the parent div should have position:relative or absolute or fixed;.
.mess{
/*your other properties here*/
position: absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%,-50%);
-ms-transform:translate(-50%,-50%);
transform: translate(-50%, -50%);
}
Upvotes: 1
Reputation: 523
Also, tested in IE8:
.mess{
text-align: center;
display: inline-block;
margin: 0 auto;
background: #212121;
color: #FFFFFF;
margin-bottom: 50px;
max-width: 350px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform:translate(-50%,-50%);
-ms-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
z-index: 1000;
}
Upvotes: 0