我无法水平居中图像。这是我的代码:
<img src="https://dummyimage.com/300x200/000/fff" alt="johnson johnson" style="width:300px; height:225px;" class="center">
我究竟做错了什么?谢谢你的时间。
你需要将left
和right
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>
你可以加上这个
<style>
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
</style>
我只是创建你的班级中心,使其成为图像的中心
如果要在线进行样式设置,可以使用中心标记
<center>
<img src="https://dummyimage.com/300x200/000/fff" alt="johnson johnson" style="width:300px; height:225px;">
<center>
只需在图像之前添加p标记并为其添加样式。
<p style="text-align: center;">
<img src="https://dummyimage.com/300x200/000/fff" alt="johnson johnson" style="width:300px;height:225px;>
</p>