Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put image in center of html page? [duplicate]

Possible Duplicate:
How to center div horizontally and vertically

I need to put image in center of html page, both vertical and horizontal ... been trying few stuff but seems to work fine. To get horizontal alignment I used <body style="text-align:center">

and it works fine, but what do with vertical alignment?

regards

like image 451
Darko Rodic Avatar asked Jun 26 '12 11:06

Darko Rodic


People also ask

How do you center an image on a page in HTML?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.

How do you center middle in HTML?

Centering in the middle of the page: To center your content in the middle of your page, add the following to your outer container : position : absolute; width: 100%; height: 100%;

How do I center an object in HTML?

Center Align Text To just center the text inside an element, use text-align: center; This text is centered.


2 Answers

If:

X is image width,
Y is image height,

then:

img {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -(X/2)px;
    margin-top: -(Y/2)px;
}

But keep in mind this solution is valid only if the only element on your site will be this image. I suppose that's the case here.

Using this method gives you the benefit of fluidity. It won't matter how big (or small) someone's screen is. The image will always stay in the middle.

like image 89
Mateusz Kocz Avatar answered Sep 20 '22 09:09

Mateusz Kocz


Put your image in a container div then use the following CSS (changing the dimensions to suit your image.

.imageContainer{
        position: absolute;
        width: 100px; /*the image width*/
        height: 100px; /*the image height*/
        left: 50%;
        top: 50%;
        margin-left: -50px; /*half the image width*/
        margin-top: -50px; /*half the image height*/
    }
like image 36
Danny Avatar answered Sep 20 '22 09:09

Danny