Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

Is the property text-align: center; a good way to center an image using CSS?

img {

    text-align: center;

}

1 Answer

0 votes
by (40.7k points)

The code you have mentioned will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. 

For more information, you can refer to this document: the W3C spec 

You can use the code given below instead of that:

img.center {

    display: block;

    margin: 0 auto;

}

<div style="border: 1px solid black;">

</div>

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...