Email or username:

Password:

Forgot your password?
jfml ✨ Jonas Laugs

Wow. I tried to #center #images on my #Wordpress #blog with #CSS for ages. Could never do it „align-center“ from back in the day didn't work, looked online, never found anything.

Now, while doing something else it seems like:

display: block;
margin-left: auto;
margin-right: auto;

does the trick!

No idea why, obviously, as always +__+

6 comments
seb :javascript:

@jfml
display: flex;
justify-content: center

gang here

Matt Wilcox

@jfml It's because of the difference between elements that use the `block` and `inline` boxes. developer.mozilla.org/en-US/do

`text-align: center;` means it centres the content of elements with an inline type - the text *inside* the p, for example. Not the p itself.

When you want to centre "an HTML element" you likely want to centre *the element* and not *its content*. Which means it needs to be block level, and then you can use margins.

jfml ✨ Jonas Laugs

@mattwilcox Mmmh, ok. So text would be inline but images aren't?

Matt Wilcox

@jfml Yeah, this link should explain what the difference is and which applies to what.

developer.mozilla.org/en-US/do

Émeric Owczarz

@jfml we even have the "margin-inline" class now, whitch results in less code writing :
margin-inline: auto;
👍

Go Up