在CSS中,实现图片的上下居中是一个常见的问题。当我们需要在网页中添加图片时,很多时候我们希望它能够居中显示。本文将介绍几种实现图片上下居中的方法。首先,我们可以通过设置图片的display属性为bl...
在CSS中,实现图片的上下居中是一个常见的问题。当我们需要在网页中添加图片时,很多时候我们希望它能够居中显示。本文将介绍几种实现图片上下居中的方法。
首先,我们可以通过设置图片的display属性为block,再设置其margin属性来使其在垂直方向上居中。示例代码如下:
img {
display: block;
margin: auto;
} .container {
display: flex;
justify-content: center;
align-items: center;
}
.container img {
max-width: 100%;
max-height: 100%;
} .container {
display: grid;
justify-items: center;
align-items: center;
}
.container img {
max-width: 100%;
max-height: 100%;
}