【问题标题】:How to implement flexbox within parent div element?如何在父 div 元素中实现 flexbox?
【发布时间】:2021-07-06 05:00:15
【问题描述】:

我想让我的页面适合屏幕高度,并在其中包含可滚动的内容,但我遇到了一个问题,由于我使用的框架(Nuxt & Buefy)生成了我无法完全做到这一点我无法控制的元素。

这就是我希望页面的样子

    html,
    body {
      height: 100%;
      margin: 0
    }
    .navbar {
      height: 65px;
      background: #dd7777
    }
    .box {
      display: flex;
      flex-flow: column;
      height: 100%;
    }

    .box .row {
      border: 1px dotted grey;
    }

    .box .row.header {
      flex: 0 1 auto;
    }

    .box .row.content {
      display: flex;
      flex-flow: column;
      flex: 1 1 auto;
      overflow-y: auto;
    }

    .box .row.footer {
      flex: 0 1 40px;
    }
<div class="auto-generated-top-elemennt">
      <div class="navbar">
      Something
      </div>
      <div class="main-content">
        <div class="box">
          <div class="row header">
            <p><b>header</b>
              <br />
              <br />(sized to content)</p>
          </div>
          <div class="row content">
            <p>
              <b>content</b> (fills remaining space)
            </p>
            <p>
              <b>content</b> (fills remaining space)
            </p>
            <p>
              <b>content</b> (fills remaining space)
            </p>
            <p>
              <b>content</b> (fills remaining space)
            </p>
            <p>
              <b>content</b> (fills remaining space)
            </p>
            <p>
              <b>content</b> (fills remaining space)
            </p>
            <p>
              <b>content</b> (fills remaining space)
            </p>
            <p>
              <b>content</b> (fills remaining space)
            </p>
          </div>
          <div class="row footer">
            <p><b>footer</b> (fixed height)</p>
          </div>
        </div>
      </div>
    </div>

我希望它看起来像similar to this solution,但不知何故它不能很好地工作..

【问题讨论】:

  • 您的身高:100% 没有跟随,您需要将它们从 html 级联到框(不要丢失任何元素)
  • @TemaniAfif 我不明白你的意思,或者如何做到这一点。我尝试手动更改所涉及的每个元素的高度,但没有可见的结果。

标签: html css layout flexbox


【解决方案1】:

你需要更多的弹性盒

body {
  height: 100vh;
  margin: 0
}
.auto-generated-top-elemennt {
  display:flex;
  flex-direction:column;
  height:100%;
}
.main-content,
.box{
  flex:1;
  display: flex;
  flex-flow: column;
  min-height:0;
}
.navbar {
  height: 65px;
  background: #dd7777
} 
.box .row {
  border: 1px dotted grey;
}

.box .row.header {
  flex: 0 1 auto;
}

.box .row.content {
  display: flex;
  flex-flow: column;
  flex: 1 1 auto;
  overflow-y: auto;
}

.box .row.footer {
  flex: 0 1 40px;
}
<div class="auto-generated-top-elemennt">
  <div class="navbar">
    Something
  </div>
  <div class="main-content">
    <div class="box">
      <div class="row header">
        <p><b>header</b>
          <br />
          <br />(sized to content)</p>
      </div>
      <div class="row content">
        <p>
          <b>content</b> (fills remaining space)
        </p>
        <p>
          <b>content</b> (fills remaining space)
        </p>
        <p>
          <b>content</b> (fills remaining space)
        </p>
        <p>
          <b>content</b> (fills remaining space)
        </p>
        <p>
          <b>content</b> (fills remaining space)
        </p>
        <p>
          <b>content</b> (fills remaining space)
        </p>
        <p>
          <b>content</b> (fills remaining space)
        </p>
        <p>
          <b>content</b> (fills remaining space)
        </p>
      </div>
      <div class="row footer">
        <p><b>footer</b> (fixed height)</p>
      </div>
    </div>
  </div>
</div>

【讨论】:

  • 这是否意味着对于每个自动生成的元素 nuxt 创建,只是为了让我调整该组件,我需要添加该样式?
  • @ZombieChibiXD 是的,你需要级联你的风格.. 第一个需要有 100% 才能获得 100% 的主体,然后你需要对所有嵌套容器使用 flexbox
  • 好的,谢谢!我只会(ab)使用@media 来防止不必要的错误和错误。谢谢!
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 2022-11-18
  • 2017-10-04
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
相关资源
最近更新 更多