【问题标题】:Flexbox fixed header, fixed left nav fluid content areaFlexbox 固定标题,固定左侧导航流体内容区域
【发布时间】:2018-01-07 01:50:55
【问题描述】:

我正在尝试使用 Flexboox 和 Bootstrap 4 实现这种布局:

  • 顶部固定标题
  • 修复了左侧边栏
  • 流畅的内容区

我不知道如何修复页眉和侧边栏。到目前为止,其他一切都按我的意愿工作:

HTML:

<header>
    header
</header>
<app>
    <nav>
        nav
    </nav>
    <article>
        Content
    </article>
</app>

CSS:

html, body {
    margin:0;
    height:100%;
    min-height:100%;
}

body {
    margin:0;
    display: flex;
    flex-direction: column;
}

header { 
    z-index: 0;
    flex: 0 64px;
    display: flex;

    background: white;
    box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.24);
}

app {
    flex: 1;
    display: flex;
}

nav {
    background: #FAF9F8;
    flex: 0 0 256px;
    order: 0;
    border-right: 1px solid #d9d9d9;
}

article {
    background: #F3F3F5;
    flex: 1 1 100px;
    order: 1;

    // Simulate large height
    height: 2000px;
}

http://jsfiddle.net/LLfcL28u/203/

【问题讨论】:

    标签: twitter-bootstrap css flexbox bootstrap-4


    【解决方案1】:

    这是一个像你一样使用 flexbox 的解决方案:http://jsfiddle.net/loic294/7hbjq82w/

    解决问题的想法是删除正文中的主滚动条:

    overflow: hidden; // Removes main scrollbar to make header and nav fix
    height: 100%; // Sets max height to ensure everything is fixed
    

    还添加一个 div 以使主要部分可滚动:

    <article>
      <div>
        Content
      </div>
    </article>
    

    并添加此 CSS 以使其正常工作:

    div {
       height: 2000px;
    }
    overflow-y: auto; // Adds scrollbar
    

    【讨论】:

    • 和我一样的小提琴链接,你能检查一下吗?
    • 哎呀,是的。这是一个很好的链接:jsfiddle.net/loic294/7hbjq82w
    • 谢谢,这很好用。不幸的是,不在我的实际应用程序中,它没有为流体内容添加滚动条。除了添加overflow-y: auto 并确保高度确实存在之外,我是否需要处理任何事情?
    • 你需要在文章标签内添加一个div,并将内容放入其中。那么它应该可以工作了。
    • 感谢您的回复,不幸的是它不起作用。这是一个屏幕截图:i.imgur.com/ZeTHIvo.png - 检查容器和 html 的大小。正文有“溢出:隐藏”;我错过了一些东西,但不确定是什么
    【解决方案2】:

    我根本不会在这里使用 flexbox,而是为固定元素使用固定位置:

    html,
    body {
      margin: 0;
      height: 100%;
    }
    header {
      position: fixed;
      z-index: 2;
      top: 0;
      left: 0;
      width: 100%;
      height: 20px;
      background: white;
      box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.24);
    }
    nav {
      position: fixed;
      z-index: 1;
      top: 20px;
      left: 0;
      width: 200px;
      height: calc(100% - 20px);
      background: #FAF9F8;
      border-right: 1px solid #d9d9d9;
    }
    
    article {
      background: #F3F3F5;
      position: relative;
      top: 20px;
      left: 200px;
      width: calc(100% - 200px);
      /* Simulate large height */
      height: 2000px;
    }
    <header>
      header
    </header>
    <nav>
      nav
    </nav>
    <article>
      Content
    </article>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2017-02-27
      相关资源
      最近更新 更多