繁体   English   中英

以响应方式使图像适合划分

[英]make image fit to division in responsive way

我正在尝试将整个图像放入分区中,并希望使用媒体查询使其响应。 主要目的是在不拉伸图像的情况下将图像放入容器内,并且图像应该适合 div 标签。 因此,即使我们拉伸屏幕,图像也应该在容器内响应,即使我缩小屏幕,它也应该在容器内响应。

我一直在尝试多种方式。 我的最终代码是

 .grid-container{ width: 70%; margin: 90px auto; }.box { height: 200px; width: 100%; min-height: 200px; position: relative; }.box1{ background: transparent url('https://i.picsum.photos/id/210/200/200.jpg?hmac=ofnpd0LAPvyH0juHuFCaLU6Y6jqJe4qpuc90jXbzUjY') no-repeat; height: 100%; position:relative; background-size: cover; background-position: center; }.box2{ background: transparent url('https://i.picsum.photos/id/210/200/200.jpg?hmac=ofnpd0LAPvyH0juHuFCaLU6Y6jqJe4qpuc90jXbzUjY') no-repeat; opacity: 1; background-position:contain; background-repeat:no-repeat; }.box3{ background: transparent url('https://i.picsum.photos/id/210/200/200.jpg?hmac=ofnpd0LAPvyH0juHuFCaLU6Y6jqJe4qpuc90jXbzUjY') no-repeat; opacity: 1; background-position:contain; background-repeat:no-repeat; }.box4{ background: transparent url('https://i.picsum.photos/id/210/200/200.jpg?hmac=ofnpd0LAPvyH0juHuFCaLU6Y6jqJe4qpuc90jXbzUjY') no-repeat; opacity: 1; background-position:contain; background-repeat:no-repeat; }.box5{ background: transparent url('https://i.picsum.photos/id/210/200/200.jpg?hmac=ofnpd0LAPvyH0juHuFCaLU6Y6jqJe4qpuc90jXbzUjY') no-repeat; opacity: 1; background-position:contain; background-repeat:no-repeat; }.sub-con { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-column-gap: 10px; grid-row-gap: 10px; } @media only screen and (max-width: 1000px) {.sub-con { grid-template-columns: 1fr 1fr; } } @media only screen and (max-width: 700px) {.sub-con { grid-template-columns: 1fr; } }
 <div class="grid-container"> <div class="sub-con"> <div class="box1 box"></div> <div class="box2 box"></div> <div class="box3 box"></div> <div class="box4 box"></div> <div class="box5 box"></div> </div> </div>

请尝试以下 css

img {
max-width:100%;
height:auto;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM