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

img {
    text-align: center;
}

当前回答

使用网格来堆叠图像。这很简单,这是代码

.grid {
   display:grid;
}

.grid img {
    display:block;
    margin:0 auto;
}

其他回答

你可以:

<中心> < img src = "…"/ > < /中心>

Use:

<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>

我认为这是在Laravel框架中将图像居中的方法。

除非你需要支持旧版的ie浏览器。

现代的方法是在CSS中使用margin: 0 auto。

例如:http://jsfiddle.net/bKRMY/

HTML:

<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>

CSS:

p.pic {
    width: 48px;
    margin: 0 auto;
}

这里唯一的问题是,段落的宽度必须与图像的宽度相同。如果你没有在段落上设置宽度,它将不起作用,因为它将假设100%并且你的图像将向左对齐,当然除非你使用text-align:center。

如果你喜欢,可以试试这把小提琴。

如果您希望将图像垂直和水平地居中,而不考虑屏幕大小,您可以尝试此代码

img{
   display: flex;
   justify-content:center;
   align-items: center;
   height: 100vh;
}

有时我们直接在WordPress管理员的页面中添加内容和图像。当我们将图像插入到内容中并想要对齐该中心时。代码显示为:

 **<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**

在这种情况下,你可以像这样添加CSS内容:

article p img{
    margin: 0 auto;
    display: block;
    text-align: center;
    float: none;
}