如何通过媒体查询禁用网格?

问题描述 投票:0回答:1

我在使用媒体查询在较小的设备(最大宽度:600px)上禁用/删除网格时遇到一些问题。

我想要的是图片消失,网格被禁用或变成一列,但我现在的方式不起作用。

如果不可能,请提供其他提示!

.section-am {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: 1fr 1fr 7%;
  gap: 15px;
  max-width: 80%;
  margin: 0 auto;
  padding: 20px;
  overflow: hidden;
}

.about-me1 {
  grid-column: 2 / -1;
  grid-row: 1 / 2;
  margin: 50px;
}

.about-me2 {
  grid-column: 1 / 2;
  grid-row: 2 / -1;
  margin: 50px;
}

.about-me-img1,
.about-me-img2 {
  width: 100%;
  height: auto;
  max-width: 300px;
  object-fit: cover;
  border-radius: 10px;
  margin: 0 auto;
}

.about-me3 {
  grid-column: 1/-1;
  grid-row-start: 3;
  margin: 50px;
  text-align: center;
}

.about-me4 {
  grid-column: 1/-1;
  grid-row-start: 4;
  margin: 50px;
  text-align: center;
}

.section-am a {
  color: #2d545e;
  text-decoration: none;
}

.section-am a:hover {
  text-decoration: underline;
  color: #12343b;
}

@media (max-width: 600px) {
  h1 {
    padding-top: 100px;
  }
  .section-am {
    grid-template-columns: 1fr;
  }
  .about-me-img1,
  .about-me-img2 {
    display: none;
  }
}
<section class="section-am">
  <p class="about-me1">
    Something about me
  </p>
  <img src="https://picsum.photos/200/300" alt="Picture i took while studying" class="about-me-img1">

  <p class="about-me2">
    More about me
  </p>
  <img src="https://picsum.photos/200/300" alt="Picture i took while studying" class="about-me-img2">
  <p class="about-me3">Even more</p>
  <p class="about-me4">Lastly</p>
</section>

预期的结果应该是这样的:

enter image description here

(抱歉图片质量不好,希望您理解我的愿景)

html css media-queries css-grid
1个回答
0
投票

我想你已经快到了。 只需要在目标媒体查询中添加几个类即可。

.section-am {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: 1fr 1fr 7%;
  gap: 15px;
  max-width: 80%;
  margin: 0 auto;
  padding: 20px;
  overflow: hidden;
}

.about-me1 {
  grid-column: 2 / -1;
  grid-row: 1 / 2;
  margin: 50px;
}

.about-me2 {
  grid-column: 1 / 2;
  grid-row: 2 / -1;
  margin: 50px;
}

.about-me-img1,
.about-me-img2 {
  width: 100%;
  height: auto;
  max-width: 300px;
  object-fit: cover;
  border-radius: 10px;
  margin: 0 auto;
}

.about-me3 {
  grid-column: 1/-1;
  grid-row-start: 3;
  margin: 50px;
  text-align: center;
}

.about-me4 {
  grid-column: 1/-1;
  grid-row-start: 4;
  margin: 50px;
  text-align: center;
}

.section-am a {
  color: #2d545e;
  text-decoration: none;
}

.section-am a:hover {
  text-decoration: underline;
  color: #12343b;
}

@media (max-width: 600px) {
  h1 {
    padding-top: 100px;
  }
  .section-am {
    grid-template-columns: 1fr;
  }
  .about-me-img1,
  .about-me-img2 {
    display: none;
  }
  .about-me1,
  .about-me2,
  .about-me3,
  .about-me4{
    width: 100%;
    grid-column: 1;
    margin: 15px auto;
  }
}
<section class="section-am">
  <p class="about-me1">
    Something about me
  </p>
  <img src="https://picsum.photos/200/300" alt="Picture i took while studying" class="about-me-img1">

  <p class="about-me2">
    More about me
  </p>
  <img src="https://picsum.photos/200/300" alt="Picture i took while studying" class="about-me-img2">
  <p class="about-me3">Even more</p>
  <p class="about-me4">Lastly</p>
</section>

© www.soinside.com 2019 - 2024. All rights reserved.