CSS(层叠样式表)是用于描述HTML或XML(包括SVG、MathML等各种XML方言)文档样式的样式表语言。图片居中是指将图片在容器内水平和垂直居中对齐。
.container {
text-align: center;
}
.container img {
display: inline-block;
}
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh; /* 使容器高度占满整个视口 */
}
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh; /* 使容器高度占满整个视口 */
}
原因:
解决方法:
max-width
和max-height
来控制图片的最大尺寸。.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh; /* 确保容器高度占满整个视口 */
}
.container img {
max-width: 100%;
max-height: 100%;
}
通过以上方法,可以确保图片在容器内始终居中显示。
领取专属 10元无门槛券
手把手带您无忧上云