我正在制作一个3列3行的网格,一切正常,但它没有响应,我尝试使用媒体查询,但看起来像https://prnt.sc/sj47qu amy解决方案?
<div class="projectPhotos">
<div class="_a p1">1</div>
<div class="_a p2">2</div>
<div class="_a p3">3</div>
<div class="_a p4">4</div>
<div class="_a p5">5</div>
<div class="_a p6">6</div>
<div class="_a p7">7</div>
</div>
._a{
width:220px;
height:120px;
background:gray;
border: 1px solid #fff;
font-size: 30px;
text-align: center;
color:white;
margin: auto;
}
.projectPhotos{
display: grid;
grid-template-columns: auto auto auto;
box-sizing: border-box;
grid-gap: 40px 60px;
box-sizing: border-box;
}
@media only screen and (max-width: 600px) {
.projects{
width:350px;
}
.projectPhotos{
grid-gap:10px 10px;
}
.projectPhotos ._a{
width:300px;
height:100px;
}
}
我想当您使用“ grid-template-columns:auto auto auto;”这意味着您将始终拥有3列的网格。我建议对此做出响应的只是将“ grid-template-columns”应用到您的媒体查询中,例如:
@media only screen and (max-width: 600px) {
.projects{
width:350px;
}
.projectPhotos{
grid-template-columns: auto;
grid-gap:10px 10px;
}
.projectPhotos ._a{
width:300px;
height:100px;
}
}
通过这种方式,当媒体查询为true时,您的网格将仅包含一列。
您可以将grid-template-columns
设置为auto
,因此在每一行上都是一个元素。
._a{
width:220px;
height:120px;
background:gray;
border: 1px solid #fff;
font-size: 30px;
text-align: center;
color: white;
margin: auto;
}
.projectPhotos{
display: grid;
grid-template-columns: auto auto auto;
box-sizing: border-box;
grid-gap: 40px 60px;
box-sizing: border-box;
}
@media only screen and (max-width: 600px) {
.projects{
width:350px;
}
.projectPhotos{
grid-gap:10px 10px;
grid-template-columns: auto;
}
.projectPhotos ._a{
width:300px;
height:100px;
}
}
<div class="projectPhotos">
<div class="_a p1">1</div>
<div class="_a p2">2</div>
<div class="_a p3">3</div>
<div class="_a p4">4</div>
<div class="_a p5">5</div>
<div class="_a p6">6</div>
<div class="_a p7">7</div>
</div>