手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

css如何设置图片垂直居中

时间:2024-10-25 07:48:52

1、新建一个html文件,在这个html文件上创建一个<img>标签,然后对这个标签设置图片路径,最后为这个标签添加一个类(案例中类为:img-center)

css如何设置图片垂直居中

2、对img-center类设置样式。1.设置positon定位为绝对定位,上下左右的距离都为:0;2.设置这个类的外编辑为:auto。代码:.img-center{position: absolute;top: 0;right: 0;bottom: 0;left: 0;margin: auto;width: 300px;}

css如何设置图片垂直居中

3、保存html文件后,使用浏览器打开,即可看到图片自动居中。

css如何设置图片垂直居中

4、所有代码。可以直接复制所有代码,粘贴后运行即可看到效果。所有代码:<!DOCTYPE html><html><head媪青怍牙><meta charset="UTF-8"><title>图片自动水平垂直居中</title><style type="text/css">.img-center{position: absolute;top: 0;right: 0;bottom: 0;left: 0;margin: auto;}</style></head><body><img src="images/22.png" alt="" class="img-center"/></body></html>

© 手抄报圈