首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用媒体查询调整大小时,网格不会堆叠列

使用媒体查询调整大小时,网格不会堆叠列
EN

Stack Overflow用户
提问于 2020-07-18 10:30:16
回答 1查看 356关注 0票数 1

当使用媒体查询将大小调整到500px以下时,我的网格不会堆叠列。我的目标是让列在调整大小后堆叠并居中,这样它就可以方便移动。我知道当前的媒体查询是有效的,因为当它低于500px时,背景变成了蓝色,但是网格并没有像我期望的那样改变。

代码语言:javascript
复制
body {
  margin: 0;
}

img {
  max-width: 100%;
}

ul {
  list-style: none;
}

a:link {
  text-decoration: none;
}

.section-3 {
  background-color: #f1f1f1;
  padding-top: .7em;
  padding-bottom: 2em;
  min-height: 50vh;
  margin: 0 auto;
  border-top: 2px solid teal;
  max-width: 100%;
}

.section-3-body {
  text-align: center;
  max-width: 100%;
}

.section-3 .title .meet {
  color: teal;
  border-top: 2px solid teal;
  border-bottom: 2px solid teal;
  border-radius: 4px;
  padding: .7em;
  display: inline-block;
}

.section-3 .grid-container {
  display: grid;
  grid-gap: 2em;
  grid-template-columns: repeat(3, 1fr);
  margin-top: 15px;
  max-width: 100%;
  overflow: hidden;
}

.grid-container>div {
  border: 2px solid #123;
  border-radius: 5px;
  background-color: #ffd8a8;
  padding: 1em;
}

.grid-container h1 {
  size: 12px
}

h3+h3 {
  margin: 0;
}

h1+h3 {
  margin: 0;
}

.section-3 .footer {
  margin-top: 30px;
}

.section-3 .footer .learn-more {
  color: teal;
  padding: .7em;
  border-radius: 14px;
  border: 2px solid teal;
  font-size: 20px;
  font-weight: bold;
}

.section-3 .footer .learn-more:hover {
  background-color: teal;
  color: white;
}

@media screen and (max-width:500px) {
  .grid-container {
    display: grid;
    grid-template-columns: repeat(1, 1fr);
    grid-gap: 1rem;
    background-color: teal;
  }
}
代码语言:javascript
复制
<section class="section-3">
  <div class="section-3-body">
    <div class="title">
      <h1 class="meet">Testing the Testing</h1>
    </div>
    <div class="grid-container">
      <div class="grid-left column">
        <img src="https://unsplash.it/400">
        <h1 class="name">Testing Names Name</h1>
        <h3 class="testing-2">Testing 2</h3>
        <h3 class="testing-3">Testing 3</h3>
        <h3 class="testing-4">Testing 4</h3>
      </div>
      <div class="grid-center column">
        <img src="https://unsplash.it/400">
        <h1 class="name">Testing Names Name</h1>
        <h3 class="testing-2">Testing 2</h3>
        <h3 class="testing-3">Testing 3</h3>
        <h3 class="testing-4">Testing 4</h3>
      </div>
      <div class="grid-right column">
        <img src="https://unsplash.it/400">
        <h1 class="name">Testing Names Name</h1>
        <h3 class="testing-2">Testing 2</h3>
        <h3 class="testing-3">Testing 3</h3>
        <h3 class="testing-4">Testing 4</h3>
      </div>
    </div>
    <div class="footer">
      <a href="#learn" class="learn-more">Test the button</a>
    </div>
  </div>
</section>

EN

回答 1

Stack Overflow用户

发布于 2020-07-18 23:23:43

这是一个特定的问题。

在您的大屏幕代码中,容器设置为:

代码语言:javascript
复制
.section-3 .grid-container {
  display: grid;
  grid-gap: 2em;
  grid-template-columns: repeat(3, 1fr);
  margin-top: 15px;
  max-width: 100%;
  overflow: hidden;
}

但在媒体对较小屏幕的查询中,有这样的情况:

代码语言:javascript
复制
.grid-container {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  grid-gap: 1rem;
  background-color: teal;
}

如您所见,.section-3 .grid-container的高于.grid-container,因此媒体查询中的grid-template-columns无法接管。

但是,由于在另一个规则中没有background-color属性,因此青色背景将随媒体查询一起生效。

作为一种解决方案,在媒体查询中使用相同的选择器(.section-3 .grid-container),这样特异性就匹配了,并且由于它在代码中稍后出现,所以它可以在触发时接管。

jsFiddle demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62964024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档