使用“中心”水平居中图像

问题描述 投票:1回答:4

我无法水平居中图像。这是我的代码:

<img src="https://dummyimage.com/300x200/000/fff" alt="johnson johnson" style="width:300px; height:225px;" class="center">

我究竟做错了什么?谢谢你的时间。

html image center
4个回答
0
投票

你需要将leftright margin应用于auto;

解:

margin:0 auto;

display:block

<div style="border:solid 1px red">
<img src="https://dummyimage.com/300x200/000/fff" alt="johnson johnson" style="width:300px; height:225px;margin:0 auto;display:block">
</div>

0
投票

你可以加上这个

<style>
.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
}
</style>

我只是创建你的班级中心,使其成为图像的中心


0
投票

如果要在线进行样式设置,可以使用中心标记

<center>
<img src="https://dummyimage.com/300x200/000/fff" alt="johnson johnson" style="width:300px; height:225px;">
<center>

0
投票

只需在图像之前添加p标记并为其添加样式。

 <p style="text-align:  center;">
     <img src="https://dummyimage.com/300x200/000/fff" alt="johnson johnson" style="width:300px;height:225px;>
 </p>
© www.soinside.com 2019 - 2024. All rights reserved.