I would like to make an image center in the exact middle of the page, so it's centered vertical and horizontal! Can I do that with a 765x741? Thanks!
Asked
Active
Viewed 1.0k times
-5
-
1Did you search the web at all? http://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/ – Aaron McIver Oct 28 '11 at 19:48
-
1Yes! What have you tried already? Have you tried or looked at [any of these questions (and their answers)](http://stackoverflow.com/search?q=image+css+html+center+horizontal+vertical)? – David Thomas Oct 28 '11 at 19:48
-
Is there anything else on the page? – Jason McCreary Oct 28 '11 at 19:48
-
no there is only that image. :P – snarkyazoid Oct 28 '11 at 19:49
5 Answers
1
Create css class for the image.
.centered {
position: fixed;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -100px;
}

jdross
- 1,188
- 2
- 11
- 30
1
Yes of cause you can create a static class in CSS like suggested already:
.centerPic
{
position:fixed;
left: 50%;
top: 50%;
margin-left:-382px; /* Static value */
margin-top:-370px; /* Static value */
}
But this approach limits your use of other pictures of different sizes. I suggest you set the margin-left and margin-top properties depending on your picture sizes in dynamic javascript instead:
function SetCenterStyle (objPic)
{
objPic.className = "centerPic";
objPic.style.marginLeft = ( -1 * ( parseInt(objPic.width) / 2 ) ) + "px";
objPic.style.marginTop = ( -1 * ( parseInt(objPic.height) / 2 ) ) + "px";
}
(You can then of cause omit the margin-left and margin-top settings in the centerPic class)

Alex
- 2,011
- 3
- 21
- 27
0
I solved it this way:
img.class-name{
position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
}

Babul Mirdha
- 3,816
- 1
- 22
- 25
0
Yes you can! The following css will do it
.centerImg
{
position:fixed;
left:50%;
top:50%;
margin-left:-382px /*half the width*/
margin-top:-370px /*half the height*/
}

Joseph Marikle
- 76,418
- 17
- 112
- 129