【问题标题】:Flexbox: Move header and footer to sidebar with flexboxFlexbox:使用 flexbox 将页眉和页脚移动到侧边栏
【发布时间】:2016-06-26 11:19:26
【问题描述】:

我的内容布局由三个部分组成:页眉、内容和页脚。对于小型设备,我想保持这个顺序,在桌面上我想将页眉和页脚移动到侧边栏。

使用浮点数实现:http://codepen.io/anon/pen/jqrZby

标记

<div class="page">
  <div class="top"></div>
  <div class="content"></div>
  <div class="bottom"></div>
</div>

CSS

.page {
  border: 2px solid black;
  padding: 1em;
}

.page:after {
  content: "";
  display: block;
  clear: both;
}

.page > * {
  margin-bottom: 1em;
}

.top {
  border: 2px solid red;
  height: 100px;
}

.content {
  border: 2px solid blue;
  height: 400px;
}

.bottom {
  border: 2px solid green;
  height: 200px;
}

@media screen and (min-width: 800px) {
  .content {
    width: 66%;
    float: left;
  }
  .top, .bottom {
    width: 33%;
    float: right;
  }
}

我正在尝试使用内容和侧边栏之间固定间距的 flexbox 找到解决方案。有人知道这是否可能吗?

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    在不改变结构和使用 flexbox 的情况下,您需要使用 flex-direction:column

    首先我们“mobile first”构建它,因为它遵循我们已经拥有的 div 结构的实际顺序。

    然后,在适当的媒体查询中,我们需要使容器包装...通常这将需要一个固定的高度(无论是视口还是 px 值)...然后我们可以重新排序元素(使用,啊哼, order) 并施加我们所需的宽度。

    Codepen Demo

    * {
      box-sizing: border-box;
    }
    .page {
      border: 2px solid black;
      display: flex;
      flex-direction: column;
      height: 400px;
      padding: 1em;
    }
    .top {
      border: 2px solid red;
      height: 100px;
      width: 100%;
      order: 1;
      margin-bottom: 1em;
    }
    .content {
      border: 2px solid blue;
      height: 400px;
      order: 2;
    }
    .bottom {
      border: 2px solid green;
      height: 200px;
      order: 3;
      margin-top: 1em;
    }
    @media screen and (max-width: 800px) {
      .page {
        flex-wrap: wrap;
        justify-content: space-between;
      }
      .top {
        order: 2;
      }
      .top,
      .bottom {
        width: 33%;
      }
      .content {
        order: 1;
        flex: 0 0 100%;
        width: 66%;
        margin-right: 1em;
      }
    }
    <div class="page">
      <div class="top">top</div>
      <div class="content">content</div>
      <div class="bottom">bottom</div>
    </div>

    【讨论】:

    • 包装是我经常碰到的墙。我无法将页面容器设置为固定高度。而且我没有找到强制换行的方法。
    • 目前,好吧,直到 display:contents 出现(或至少得到适当支持),这是我知道的唯一选项......对于 flexbox。当然还有视口单位和最大高度可供使用。
    • html结构其实是不固定的。但我没有看到其他人如何提供帮助。但我将尝试使用视口单位。谢谢!
    猜你喜欢
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2010-12-14
    • 2023-03-16
    • 2016-06-07
    • 2018-12-29
    相关资源
    最近更新 更多