gpt4 book ai didi

css - 如何使用 CSS 网格布局利用响应式图像

转载 作者:行者123 更新时间:2023-11-28 02:56:52 26 4
gpt4 key购买 nike

我一直在使用新的 CSS 网格布局,但我似乎找不到太多关于如何使图像响应的信息?我试过使用媒体查询并将图像宽度设置为 100%,但图像仍然溢出到网格之外。我错过了什么?

最佳答案

您可以使用以下概念:

function getRandomSize(min, max) {
return Math.round(Math.random() * (max - min) + min);
}

var allImages = "";

for (var i = 0; i < 25; i++) {
var width = getRandomSize(200, 400);
var height = getRandomSize(200, 400);
allImages += '<img src="https://placekitten.com/'+width+'/'+height+'" alt="pretty kitty">';
}

$('#photos').append(allImages);
#photos {
/* Prevent vertical gaps */
line-height: 0;

-webkit-column-count: 5;
-webkit-column-gap: 0px;
-moz-column-count: 5;
-moz-column-gap: 0px;
column-count: 5;
column-gap: 0px;
}

#photos img {
/* Just in case there are inline attributes */
width: 100% !important;
height: auto !important;
}

@media (max-width: 1200px) {
#photos {
-moz-column-count: 4;
-webkit-column-count: 4;
column-count: 4;
}
}
@media (max-width: 1000px) {
#photos {
-moz-column-count: 3;
-webkit-column-count: 3;
column-count: 3;
}
}
@media (max-width: 800px) {
#photos {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
}
}
@media (max-width: 400px) {
#photos {
-moz-column-count: 1;
-webkit-column-count: 1;
column-count: 1;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="photos"></section>

关于css - 如何使用 CSS 网格布局利用响应式图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46401875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com