属性text-align: center;一个好方法来中心使用CSS图像?

img {
    text-align: center;
}

当前回答

我看到了这个帖子,它对我很有用:

img { 位置:绝对的; 上图:0; 底部:0; 左:0; 右:0; 保证金:汽车; } < span style=" font - family:宋体;"位置:相对;最小高度:200 px”> < img src = " https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a " > < / div >

(纵横对齐)

其他回答

使用这个到你的img CSS:

img {
  margin-right: auto;
  margin-left: auto;
}
.img-container {
  display: flex;
}

img {
  margin: auto;
}

这将使图像在垂直和水平方向上处于中心位置

我看到了这个帖子,它对我很有用:

img { 位置:绝对的; 上图:0; 底部:0; 左:0; 右:0; 保证金:汽车; } < span style=" font - family:宋体;"位置:相对;最小高度:200 px”> < img src = " https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a " > < / div >

(纵横对齐)

用CSS将图像居中。

img{
    display: block;
    margin-left: auto;
    margin-right: auto;
}

你可以在这里了解更多

你可以在父元素上使用text-align: center,并将img更改为display: inline-block→,因此它的行为就像一个文本元素,如果父元素有宽度,它将居中!

img {
    display: inline-block
}