Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering absolute DIV horizontally in IE8

Tags:

html

css

Is it possible to an center a div horizontally, if that div is also absolute? The following works great in Chrome/Safari/Firefox, but in IE8 it sits to the far left:

.holder-answers {
    position: absolute;
    display: none;
    left: 0;
    right: 0;
    margin-left: auto;
    margin-right: auto;
    top: 200px;
    width: 501px;
}
like image 666
lennysan Avatar asked Jan 26 '11 15:01

lennysan


People also ask

How do you horizontally center an absolute div?

using a "text-align: center;" and a "left: 0; right: 0;" will allow you to absolute position a div while keeping it horizontally centered.

How do you center an absolute position horizontally?

If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).

How do you align a div horizontally and vertically centered?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How can you center an element horizontally and vertically using the position property?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.


1 Answers

Can you not try:

.holder-answers {
    position: absolute;
    top: 200px;
    left: 50%;
    width: 501px;
    margin-left: -250px; /* half the box width */
}

This method should work in all browsers.

like image 87
Chris Avatar answered Sep 28 '22 07:09

Chris