首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用媒体查询进行动态布局?

如何使用媒体查询进行动态布局?
EN

Stack Overflow用户
提问于 2022-09-13 01:52:50
回答 4查看 108关注 0票数 0

我一直在试着做一些特定尺寸的布局。

如上图所示,我试图创建两个布局,个人电脑和移动。

做一台电脑很容易。SectionASectionB是通过flex布局配置的,而SectionC则是在其下面配置的。

问题就在手机上。SectionASectionB在一个flex布局中绑定在一起,所以我想不出如何将SectionC放在AB之间。

这是因为,在html结构中,sectionC已经存在于下面。是否可以只使用CSS配置而不使用Javascript?

代码语言:javascript
复制
.header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  /*only for ilustration*/
  background-color: #d7d7d7;
}

.bottom {
  display: flex;
  flex-direction: row;
  /*only for ilustration*/
  background-color: #ffd5d5;
}

@media screen and (max-width: 760px) {
  .header {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
  }
  .bottom {
    flex-direction: column;
    border: none;
  }
}
代码语言:javascript
复制
<div>
  <div class="header">
    <div>
      SectionA
    </div>
    <div>
      SectionB
    </div>
  </div>
  <div class="bottom">
    SectionC
  </div>
</div>

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-09-13 02:57:29

  • 我刚刚对你的HTML做了一个修改。
  • 所以我把所有的div包在.header里面。
  • 然后,为了首先给出50%的宽度来指导子程序,我刚刚添加了.hlalf类,并将这个类CSS设置为flex:1 0 50%,这意味着使用50%的柔箱宽度。
  • 现在,为了在响应中心实现底层div,我们使用了挠曲盒的order特性。为此,我刚刚按照他们的顺序给所有的3div添加了类。我使用sm关键字只是为了理解为小屏幕。
代码语言:javascript
复制
    @media screen and (max-width: 560px) {
      .order-sm-1 {
        order: 1;
      }
      .order-sm-2 {
        order: 2;
      }
      .order-sm-3 {
        order: 3;
      }
    }

代码语言:javascript
复制
.header {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.header>div {
  flex: 1;
}

.header>div.half {
  /*This css tells all the direct div of header class to take 50% of the space*/
  flex: 1 0 50%;
}

.bggreen {
  background: green;
}

.bgblue {
  background: blue;
}

.bottom {
  background-color: #ffd5d5;
}

@media screen and (max-width: 760px) {
  /*This will change flex-direction to column which is by default row*/
  .header {
    flex-direction: column;
  }
  .order-sm-1 {
    order: 1;
  }
  .order-sm-2 {
    order: 2;
  }
  .order-sm-3 {
    order: 3;
  }
}
代码语言:javascript
复制
<div>
  <div class="header">
    <div class="bggreen half order-sm-1">
      SectionA
    </div>
    <div class="bgblue half order-sm-3">
      SectionB
    </div>
    <div class="bottom order-sm-2">
      SectionC
    </div>
  </div>

票数 1
EN

Stack Overflow用户

发布于 2022-09-13 02:12:01

您可以将所有部分包装在一个flex容器中,并使用flex顺序属性。

票数 0
EN

Stack Overflow用户

发布于 2022-09-13 09:25:42

我在这个派对上迟到了,但在我看来,网格是一条路,因为你可以完全控制你放东西的地方。它比flexbox灵活得多,但它最初是一个陡峭的学习曲线,因为有很多选择。Kevin对网格有一个很好的入门视频,而也是一个很好的、写得很清楚的css技巧资源。

代码语言:javascript
复制
.container {
  display: grid;
  grid-template-columns: 3fr 1fr;
  grid-template-areas: "a b" "c c";
  border: 2px solid #285DBB;
  row-gap: 0.25rem;
  column-gap: 2rem;
  padding: 0.35rem;
}

.container>div {
  background-color: #92D050;
  color: white;
  display: grid;
  place-content: center;
  padding: 0.75rem 2rem;
}

.zoneA {
  grid-area: a;
  text-decoration: underline wavy red;
}

.zoneB {
  grid-area: b;
}

.zoneC {
  grid-area: c;
}

.footer {
  margin-top: 1.5rem;
  font-size: 1.25rem;
  text-align: center;
}

.mobile {
  display: none;
}

.pc {
  display: block;
}

@media only screen and (max-width:760px) {
  .container {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 3fr 1fr;
    grid-template-areas: "a" "c" "b";
  }
  .mobile {
    display: block;
  }
  .pc {
    display: none;
  }
  .zoneB {
    text-decoration: underline wavy red;
  }
  .zoneC {
    text-decoration: underline wavy red;
  }
}
代码语言:javascript
复制
<div class="container">
  <div class="zoneA">
    SectionA
  </div>
  <div class="zoneB">
    SectionB
  </div>
  <div class="zoneC">
    SectionC
  </div>
</div>

<div class="pc footer">PC</div>
<div class="mobile footer">Mobile</div>

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

https://stackoverflow.com/questions/73696787

复制
相关文章

相似问题

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