-5

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!

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
snarkyazoid
  • 435
  • 2
  • 5
  • 11

5 Answers5

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
0

use the margin: auto auto;

I thinks it's the best way

GregM
  • 2,634
  • 3
  • 22
  • 37