Is the property text-align: center;
a good way to center an image using CSS?
img {
text-align: center;
}
Actually, the only problem with your code is that the text-align
attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span
tag around the image and set its style to text-align: center
, as so:
span.centerImage {
text-align: center;
}
The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span
(or div
).