Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal image align CSS

I'm having some trouble aligning a main image. It should be center-aligned horizontally, yet keeps going all over the place. The page can be found here http://0034.eu/propmanager/

<img src="images/background-space.png" class="displayed" border="0" />

IMG.displayed{
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
}

That's basically the CSS I have applied to the image, all the source code is on main index.html (no separate style sheet).

like image 666
Brad Adams Avatar asked Jul 09 '13 10:07

Brad Adams


People also ask

How do I horizontally align an image?

To center an image horizontally, you can use the CSS text-align property. Step 1: Since this property only works on block-level elements and not inline elements, let's start by wrapping the image in a block element.

How do I change the image alignment in CSS?

Aligning an image means to position the image at center, left and right. We can use the float property and text-align property for the alignment of images. If the image is in the div element, then we can use the text-align property for aligning the image in the div.

How do I center an image horizontally and vertically?

Center CSS Image Vertically and Horizontally We can use flex-box to center an image horizontally and vertically. To do so we must use both the justify-content and align-items property on the parent container of the image.


2 Answers

Add this to your CSS style.

img.displayed {
    display: table-caption;
    margin: 0 auto;
}

EDIT

From the inputs of IlyaStreltsyn, I agree with the point of clearing the right with display:block for the image to be centered.

For Instance,

img.displayed {
    display: block;
    margin: 0 auto;
    clear: right;
}
like image 79
Nitesh Avatar answered Oct 03 '22 11:10

Nitesh


Add display: block;

img.displayed{
    display: block;
    margin:0 auto;
}

DEMO

like image 40
Sowmya Avatar answered Oct 03 '22 12:10

Sowmya