【问题标题】:Change layout of div on small screen在小屏幕上更改 div 的布局
【发布时间】:2020-03-17 23:40:13
【问题描述】:

我有一个包含以下内容的页面 2 列布局:

我需要将其变成移动设备上的 1 列布局:

顶部:黄色(侧边栏内容1)全宽

中间:绿色(内容在此处)全宽

底部:粉红色(侧边栏内容 2)全宽

.container {
  border: 1px solid red;
  display: flex;
}

.left-sidebar {
  width: 200px;
  background: yellow;
}

.left-sidebar #left-sidebar_top {
  height: 500px;
}

.left-sidebar #left-sidebar_bottom {
  height: 400px;
  background: pink;
}

.right-content {
  background: lightgreen;
  flex: 1;
}
<div class="container">
  <div class="left-sidebar">
    <div id="left-sidebar_top">Sidebar Content1</div>
    <div id="left-sidebar_bottom">Sidebar Content2</div>
  </div>
  <div class="right-content">
    CONTENT GOES HERE
  </div>
</div>

我怎样才能完全通过 CSS 中的媒体查询来实现这一点? (不改变html)

如果不可能,你会如何修改html结构和css?

编辑:澄清问题。

【问题讨论】:

  • 是的,您可以在纯 CSS 中使用 @media 查询来实现这一点 :)
  • @NamKim:你的措辞“全宽”翻译为“100%”或“100vw”。使用提到的媒体查询,只需再次使用相同的 css 附加媒体查询,并使用您所说的修复这些新值:full with etc. 将容器显示值从 flex 设置为在 mediaquery 中显示完成大部分工作 - 创建一个jsfiddle 用你的例子让人们帮助你。不要指望别人为你写完整的代码。

标签: html css flexbox media-queries


【解决方案1】:

您是否尝试过使用@media 这样做?

你可以试试

 @media screen and (max-width: 600px){ //this should cover all the small screens
//write the logic of how you want your various div to change
}

【讨论】:

  • 是的,我知道媒体查询是什么,但我正在寻找特定的 CSS 样式来应用以实现我想要的布局。
【解决方案2】:

您可以将网格布局用于不同的查询: https://www.w3schools.com/css/css_grid.asp

但请注意,旧浏览器似乎有问题(我看着你微软)。

网格让您可以像这样设计布局:

.grid-container {
  display: grid;
  grid-template-areas:
    'header header header header header header'
    'menu main main main right right'
    'menu footer footer footer footer footer';
}

【讨论】:

    【解决方案3】:

    这是可能的,但是您需要更改 HTML 结构:只需删除左侧元素的包装器并使用下面的 CSS。最主要的是使用flex-wrap,桌面模式下容器的固定/有限高度(flex-wrap 工作)和子元素的order 参数。

    * {
      box-sizing: border-box;
    }
    
    .container {
      border: 1px solid red;
      display: flex;
      flex-direction: column;
      flex-wrap: wrap;
      width: 100%;
      height: 902px;
    }
    
    #left-sidebar_top {
      width: 200px;
      background: yellow;
      height: 500px;
      order: 1;
    }
    
    #left-sidebar_bottom {
      width: 200px;
      height: 400px;
      background: pink;
      order: 2;
    }
    
    .right-content {
      width: calc(100% - 200px);
      height: 900px;
      background: lightgreen;
      order: 3;
    }
    
    @media screen and (max-width: 500px) {
      .container {
        overflow: visible;
        flex-wrap: nowrap;
        height: auto;
      }
      #left-sidebar_top {
        width: 100%;
        order: 1;
      }
      #left-sidebar_bottom {
        width: 100%;
        order: 3;
      }
      .right-content {
        width: 100%;
        order: 2;
      }
    }
    <div class="container">
      <div id="left-sidebar_top">Sidebar Content1</div>
      <div id="left-sidebar_bottom">Sidebar Content2</div>
      <div class="right-content">
        CONTENT GOES HERE
      </div>
    </div>

    【讨论】:

    • 希望在不修改 HTML 结构的情况下实现纯 css 解决方案,但我想它不是,正如我所怀疑的那样。谢谢你的详细解答! flex order 属性非常漂亮
    猜你喜欢
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多