代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .father { width: 600px; height: 600px; /* 1. 设置行高等于高度 */ background-color: pink; line-height: 600px; } .father img { /* 2. 再给图片设置vam 垂直居中 即可让图片在当前容器内垂直居中 */ vertical-align: middle; } </style> </head> <body> <div class="father"> <img src="./images/1.jpg" alt="" /> </div> </body> </html>
效果图:
CSS3-案例-图片设置垂直居中
![](https://img-home.csdnimg.cn/images/20240711042549.png)