Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

left:50% element not appearing in middle of page

Tags:

html

css

position

I have an absolute positioned popup (hover over "ice white" image to see popup) which has css left:50%. Now this should appear in the middle of page but doesn't. Any suggestions please? Thanks in advance.

like image 600
user961239 Avatar asked Sep 26 '11 17:09

user961239


3 Answers

You're also supposed to add margin-left with the negative of a half of visible width of the element. So, for example:

width: 400px;
padding: 10px;
border-width: 2px;
/* -(400 + 10 + 2)/2 = -206 */
margin-left: -206px;
left: 50%;

Note that margin: auto suggested by others won't work because you've positioned the element absolutely.

like image 141
duri Avatar answered Nov 11 '22 07:11

duri


Lol, no. The left side of the image appears at 50% of the page width. Hence; left: 50%.

In order to center your image, set margin: auto instead.

like image 40
GAgnew Avatar answered Nov 11 '22 06:11

GAgnew


.div {
 position:absolute;
 left:50%;
 transform:translate(-50%,0)
}
like image 3
Muhammed İlan Avatar answered Nov 11 '22 08:11

Muhammed İlan