使用以下三种方法可以将图片在 div 中垂直居中:

  1. 使用 flex 布局:
div {
  display: flex;
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
}

img {
  max-width: 100%;
  max-height: 100%;
}
  1. 使用 position 和 transform:
div {
  position: relative;
}

img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
  1. 使用 display: table 和 vertical-align:
div {
  display: table;
  text-align: center;
}

img {
  display: inline-block;
  vertical-align: middle;
}

以上方法可以让图片在 div 中垂直居中显示。

CSS 图片垂直居中:三种方法实现图片在 DIV 中居中显示

原文地址: https://www.cveoy.top/t/topic/ZUY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录