Center image using text-align center?

后端 未结 24 1622
终归单人心
终归单人心 2020-11-21 17:53

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

img {
    text-align: center;
}
24条回答
  •  逝去的感伤
    2020-11-21 18:39

    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).

提交回复
热议问题