-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
  • 113,548
  • 43
  • 231
  • 352
snarkyazoid
  • 425
  • 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,138
  • 2
  • 11
  • 29
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
  • 1,973
  • 3
  • 20
  • 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,688
  • 1
  • 21
  • 24
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
  • 72,900
  • 16
  • 109
  • 126
0

use the margin: auto auto;

I thinks it's the best way

GregM
  • 2,626
  • 3
  • 19
  • 37