1.section方案
class
.centerImg{
border:solid 1px #ccc;
background-color:red;
background-image:url(xxx.jpg);
background-origin:content;
background-position:50% 50%;
background-size:contain; /*保持图像本身的宽高比例,将图片缩放到宽度或高度正好适应定义背景的区域*/
background-repeat:no-repeat;
}
use
<section class="centerImg" style="width:400px;height:200px;"></section>
本文介绍了一种使用CSS实现背景图片居中显示并保持原有宽高比的方法。通过设置特定的CSS属性,如background-size、background-position等,确保了图片能够适应不同的容器尺寸同时保持视觉效果的一致性。
9108

被折叠的 条评论
为什么被折叠?



