How to centre an image in CSS

- by

CSS-LogoCentering text in CSS is relatively easy. All you do is add the “text-aling: center” property.

If you’ve ever tried this with anything that isn’t text you’ll have noticed that this approach doesn’t work.

Instead, display your class as a block, then set the margins to auto. You also need to specify a width so the rest flows around this fixed parameter:

.yourclass {
	margin-left:auto;
	margin-right:auto;
	width: 400px;
	display:block;
}

This approach works with everything that is not text such as images.

Thanks to Bert Bos for this tip!

2 thoughts on “How to centre an image in CSS”

Add your voice!

This site uses Akismet to reduce spam. Learn how your comment data is processed.