这是我的代码:
<div class="block bg-success">
<h1>Projects</h1>
<div class="wraper">
<div class="box">A</div>
<div class="box">B</div>
<div class="box">C</div>
</div>
</div>
css:
.wraper{ display: grid; grid-template-columns: 400px 400px 400px; grid-gap: 10px; grid-template-columns: repeat(3, 400px); } .box{ background-color: red; height: 200px; } @media (max-width: 1220px) { .wraper{ grid-template-columns: repeat(2, 400px); } } @media (max-width: 810px) { .wraper{ grid-template-columns: repeat(1, 400px); } } @media (max-width: 400px) { .wraper{ grid-template-columns: repeat(1, 300px); } }
@media (max-width: 1220px) {
.wraper {
grid-template-columns: repeat(2, 400px);
}
.box:last-child {
grid-column: 1 / 3;
justify-self: center;
width: 400px;
}
}
jsFiddle demo.wraper { display: grid; grid-template-columns: 400px 400px 400px; grid-gap: 10px; grid-template-columns: repeat(3, 400px); } .box { background-color: red; height: 200px; } @media (max-width: 1220px) { .wraper { grid-template-columns: repeat(2, 400px); } /* NEW */ .box:last-child { grid-column: 1 / 3; justify-self: center; width: 400px; } } @media (max-width: 810px) { .wraper { grid-template-columns: repeat(1, 400px); } /* RESET */ .box:last-child { grid-column: auto; justify-self: stretch; width: auto; } } @media (max-width: 400px) { .wraper { grid-template-columns: repeat(1, 300px); } }
<div class="block bg-success"> <h1>Projects</h1> <div class="wraper"> <div class="box">A</div> <div class="box">B</div> <div class="box">C</div> </div> </div>