Skip to content Skip to sidebar Skip to footer

How To Position Image In The Center/middle Both Vertically And Horizontally

How can i make the image start from the middle of th

Solution 1:

There are several ways to do this, and if it needs to work in all browsers (IE7+ and the rest) you need to do different things to make it work in some of the cases.

  1. Use absolute position. This only works if you know the size of the image. Here you set it to position: absolute; left: 50%; top: 50%; margin: -<half height of image> 0 0 -<half width of image>.

    See example here: http://jsfiddle.net/JPch8/

  2. Use margin: 0 auto;text-align: center; and line-height/font-size. This is a bit more tricky, since line-height doesn't work as it should in IE for inline-block elements like images. That's where the font-size comes in. Basically, you set the line-height of the image container to the same as the container's height. This will vertically align inline elements, but in IE you need to set the font-size instead to make it work.

    See example here: http://jsfiddle.net/JPch8/2/

  3. In modern browsers that support display: flex you can do it by simply setting the container div to display: flex; align-items: center; justify-content: center;

    See example here: https://jsfiddle.net/ptz9k3th/

Solution 2:

put the image in a <div> with text-align:center; without specifying the size of the box

<divclass="picture_div"style="margin:0px auto; text-align:center;"><imgsrc="media/BezierCurve.gif" /></div>

alternatively you can specify width and the height of the <div> box in order to center the image (actually the div box).

<divid="blue"style="border:1px solid blue; width:100px; height:100px; margin:10px auto;"><imgsrc="media/BezierCurve.gif" /></div>

Solution 3:

"float:left; position:relative" probably doesn't work as expected. Floated elements are considered absolute.

To get the image centered vertically you need a height on the div, and you need height on it's parents. (Centering vertically is kind of a pain). My example below will work if those are your only elements but be aware that height: 100% on the containers will likely affect the rest of your layout.

<html><head><title></title><styletype="text/css">html, body { 
     height: 100%;
}

#photo_leftPanel {
     height: 500px; /*guessing*/width: 604px;
     float: left;
}

#photo_leftPanelimg {
     margin: auto;
     vertical-align: middle;
}
</style></head><body><divid="photo_leftPanel"><imgsrc="bla.jpg" /></div></body></html>

Solution 4:

A suitable solution for modern browsers is flexbox. A flex container can be configured to align its items both horizontally and vertically.

<divstyle="display: flex; align-items: center; justify-content: center; width: 600px; height: 600px;"><imgsrc="bla.jpg"></div>

Solution 5:

HTML:

<divid="photo_leftPanel"><imgsrc="bla.jpg"></div>

CSS:

div.photo_leftPanel {
   position: relative;
}

div.photo_leftPanel > img {
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
}

Post a Comment for "How To Position Image In The Center/middle Both Vertically And Horizontally"