CSS Flexbox – 粘性顶部和侧边导航栏,带滚动

huangapple go评论69阅读模式
英文:

CSS Flexbox - Sticky Top and Side navbar, with scroll

问题

我希望:

  • 一个高度为50px的顶部标题,下面是一个粘性导航。
  • 侧边导航在与顶部导航相同的位置粘在页面上。
  • 两个导航都可以滚动。
  • 不粘性的页脚保持在页面底部。

我能够分别使以下其中一个工作:

  • 滚动与固定标题和顶部导航一起工作
  • 使顶部导航粘在非固定的主要元素上工作

我需要它们同时都能够工作。HTML/CSS解决方案优先。

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

/* ! BODY / HTML */
html,
body {
  background-color: red;
}

/* ! CONTAINER */
.container {
  display: flex;
  flex-flow: column wrap;
  height: 100vh;
  min-height: 100vh;
  background-color: #ecf0f1;
}

/* ! HEADER/FOOTER */
header,
footer {
  font-size: 50px;
  color: white;
  background-color: orange;
}

/* ! TOP NAV */
.top_navbar {
  display: flex;
  flex-flow: row nowrap;
  position: sticky;
  top: 0px;
  align-items: center;
  background-color: yellow;
}

.top_navbar a {
  color: white;
}

#nt_left {
  background-color: green;
  flex-grow: 1;
}

#nt_center {
  background-color: blue;
}

#nt_right {
  background-color: indigo;
}

/* ! MAIN CONTENT */
.main_body {
  display: flex;
  flex-flow: row nowrap;
  overflow: auto;
}

#ns_left {
  flex: 0 0;
  flex-basis: 300px;
  top: 50px;
  background-color: violet;
  font-size: 18px;
}

#main {
  flex: 1 0;
  background-color: red;
  font-size: 26px;
}
<header>THIS IS A HEADER</header>
<div class="container">
  <div class="top_navbar" id="top_navbar">
    <div class="nav_top" id="nt_left">
      <a href="#" id="logo"><span class="material-symbols-outlined"> sentiment_satisfied </span></a>
    </div>
    <div class="nav_top" id="nt_right">
      <a href="#">TOP LINK 1</a>
      <a href="#">TOP LINK 2</a>
      <a href="#">TOP LINK 3</a>
    </div>
  </div>
  <div class="main_body">
    <aside class="sidebar" id="ns_left">
      <h3>sidebar</h3>
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item ...</li>
      </ul>
    </aside>
    <article class="article" id="main">
      <h3>article</h3>
      <p>
        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.
      </p>
    </article>
  </div>
</div>
<footer>THIS IS A FOOTER</footer>
英文:

I would like:

  • A 50px tall header with a sticky nav below.
  • Side nav sticky at same point as top nav.
  • Both navs retain scroll ability.
  • Non-sticky footer that stays at the very bottom.

I was able to get one of the following to work at a time:

  • Scrolling to work with fixed header & top nav
  • Sticky top nav to work with non-fixed main elements

I need to get them both to work simultaneously.
HTML/CSS only solution preferred.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}


/* ! BODY / HTML */

html,
body {
  background-color: red;
}


/* ! CONTAINER */

.container {
  display: flex;
  flex-flow: column wrap;
  height: 100vh;
  min-height: 100vh;
  background-color: #ecf0f1;
}


/* ! HEADER/FOOTER */

header,
footer {
  font-size: 50px;
  color: white;
  background-color: orange;
}


/* ! TOP NAV */

.top_navbar {
  display: flex;
  flex-flow: row nowrap;
  /*flex: 0 0 auto;*/
  position: sticky;
  top: 0px;
  align-items: center;
  background-color: yellow;
}

.top_navbar a {
  color: white;
}

#nt_left {
  background-color: green;
  flex-grow: 1;
}

#nt_center {
  background-color: blue;
}

#nt_right {
  background-color: indigo;
}


/* ! MAIN CONTENT */

.main_body {
  display: flex;
  flex-flow: row nowrap;
  overflow: auto;
}

#ns_left {
  flex: 0 0;
  flex-basis: 300px;
  top: 50px;
  background-color: violet;
  font-size: 18px;
}

#main {
  flex: 1 0;
  background-color: red;
  font-size: 26px;
}

<!-- language: lang-html -->

&lt;header&gt;THIS IS A HEADER&lt;/header&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;top_navbar&quot; id=&quot;top_navbar&quot;&gt;
    &lt;div class=&quot;nav_top&quot; id=&quot;nt_left&quot;&gt;
      &lt;a href=&quot;#&quot; id=&quot;logo&quot;&gt;&lt;span class=&quot;material-symbols-outlined&quot;&gt; sentiment_satisfied &lt;/span&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;nav_top&quot; id=&quot;nt_right&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;TOP LINK 1&lt;/a&gt;
      &lt;a href=&quot;#&quot;&gt;TOP LINK 2&lt;/a&gt;
      &lt;a href=&quot;#&quot;&gt;TOP LINK 3&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;main_body&quot;&gt;
    &lt;aside class=&quot;sidebar&quot; id=&quot;ns_left&quot;&gt;
      &lt;h3&gt;sidebar&lt;/h3&gt;
      &lt;ul&gt;
        &lt;li&gt;Item 1&lt;/li&gt;
        &lt;li&gt;Item 2&lt;/li&gt;
        &lt;li&gt;Item 3&lt;/li&gt;
        &lt;li&gt;Item 4&lt;/li&gt;
        &lt;li&gt;Item 5&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/aside&gt;
    &lt;article class=&quot;article&quot; id=&quot;main&quot;&gt;
      &lt;h3&gt;article&lt;/h3&gt;
      &lt;p&gt;
        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.
      &lt;/p&gt;
    &lt;/article&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;footer&gt;THIS IS A FOOTER&lt;/footer&gt;

<!-- end snippet -->

答案1

得分: 1

我的最佳尝试,将&lt;aside&gt;移到.top_navbar中并使用position:absolute来创建幻觉。

.top_navbar {
  display: flex;
  flex-flow: row nowrap;
  position: sticky;
  top: 0px;
  align-items: center;
  background-color: yellow;
  height: var(--top-navbar-height);
}

#ns_left {
  width: var(--aside-width);
  background-color: violet;
  font-size: 18px;
  position: absolute;
  top: 0;
  margin-top: var(--top-navbar-height);
  max-height: 100vh;
  overflow: auto;
  padding-bottom: calc(var(--top-navbar-height) + 70px);
}

希望这有帮助。

英文:

My best attempt, move &lt;aside&gt; into .top_navbar and use position:absolute to make the illusion.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

:root {
  --aside-width: 150px;
  --top-navbar-height: 30px;
}

/* ! BODY / HTML */

html,
body {
  background-color: red;
}


/* ! CONTAINER */

.container {
  display: flex;
  flex-flow: column nowrap;
  _min-height: 100vh; /* this line should be remove to become a true responsive site, here just demo */
  background-color: #ecf0f1;
}


/* ! HEADER/FOOTER */

header,
footer {
  font-size: 50px;
  color: white;
  background-color: orange;
  position: relative;
}


/* ! TOP NAV */

.top_navbar {
  display: flex;
  flex-flow: row nowrap;
  position: sticky;
  top: 0px;
  align-items: center;
  background-color: yellow;
  height: var(--top-navbar-height);
}

.top_navbar a {
  color: white;
}

#nt_left {
  background-color: green;
  flex-grow: 1;
}

#nt_center {
  background-color: blue;
}

#nt_right {
  background-color: indigo;
}


/* ! MAIN CONTENT */

.main_body {
  padding-left: var(--aside-width);
}

#ns_left {
  width: var(--aside-width);
  background-color: violet;
  font-size: 18px;
  position: absolute;
  top: 0;
  margin-top: var(--top-navbar-height);
  max-height: 100vh;
  overflow: auto;
  padding-bottom: calc(var(--top-navbar-height) + 70px);
}

.article1 {
  background-color: red;
  font-size: 26px;
}

.article2 {
  background-color: blue;
  font-size: 26px;
}

<!-- language: lang-html -->

&lt;header&gt;THIS IS A HEADER&lt;/header&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;top_navbar&quot; id=&quot;top_navbar&quot;&gt;
    &lt;div class=&quot;nav_top&quot; id=&quot;nt_left&quot;&gt;
      &lt;a href=&quot;#&quot; id=&quot;logo&quot;&gt;&lt;span class=&quot;material-symbols-outlined&quot;&gt; sentiment_satisfied &lt;/span&gt;&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class=&quot;nav_top&quot; id=&quot;nt_right&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;TOP LINK 1&lt;/a&gt;
      &lt;a href=&quot;#&quot;&gt;TOP LINK 2&lt;/a&gt;
      &lt;a href=&quot;#&quot;&gt;TOP LINK 3&lt;/a&gt;
    &lt;/div&gt;
    &lt;aside class=&quot;sidebar&quot; id=&quot;ns_left&quot;&gt;
      &lt;h3&gt;sidebar&lt;/h3&gt;
      &lt;ul&gt;
        &lt;li&gt;Item 1&lt;/li&gt;
        &lt;li&gt;Item 2&lt;/li&gt;
        &lt;li&gt;Item 3&lt;/li&gt;
        &lt;li&gt;Item 4&lt;/li&gt;
        &lt;li&gt;Item 5&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item ...&lt;/li&gt;
        &lt;li&gt;Item last&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/aside&gt;
  &lt;/div&gt;
  &lt;div class=&quot;main_body&quot;&gt;
    &lt;article class=&quot;article1&quot; id=&quot;main1&quot;&gt;
      &lt;h3&gt;article&lt;/h3&gt;
      &lt;p&gt;
        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.

        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.

        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.
      &lt;/p&gt;
    &lt;/article&gt;
    
    &lt;article class=&quot;article2&quot; id=&quot;main2&quot;&gt;
      &lt;h3&gt;article&lt;/h3&gt;
      &lt;p&gt;
        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.

        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.

        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.
      &lt;/p&gt;
    &lt;/article&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;footer&gt;THIS IS A FOOTER&lt;/footer&gt;

<!-- end snippet -->

答案2

得分: 0

这是您的代码片段的翻译:

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-css -->
* {
  box-sizing: border-box;
}

:root {
  --aside-width: 300px;
  --aside-height: 250px;
}

html,
body,
.container {
  height: 100%;
}

body {
  margin: 0;
  background-color: red;
}

header,
footer {
  font-size: 50px;
  color: white;
  background-color: orange;
}

section {
  position: relative;
}

nav {
  display: grid;
  grid-template-columns: var(--aside-width) 1fr;
  grid-template-areas: 'nav-left nav-right' 'aside .';
  position: sticky;
  top: 0;
  background-color: transparent;
}

nav a {
  color: white;
}

aside {
  grid-area: 'aside';
  width: var(--aside-width);
  height: var(--aside-height);
  background-color: violet;
  font-size: 18px;
}

main {
  display: flex;
  flex-flow: row nowrap;
}

.article-main {
  padding: 1em;
  padding-inline-start: calc(var(--aside-width) + 1em);
  background-color: red;
  font-size: 26px;
}

.nav-left {
  grid-area: 'nav-left';
  background-color: green;
}

.nav-right {
  grid-area: 'nav-right';
  background-color: indigo;
}

<!-- 语言: lang-html -->
<header>这是页眉</header>
<section>
  <nav>
    <div class="nav-left">
      <a href="#" id="logo"><span class="material-symbols-outlined">sentiment_satisfied </span></a>
    </div>
    <div class="nav-right">
      <a href="#">顶部链接1</a>
      <a href="#">顶部链接2</a>
      <a href="#">顶部链接3</a>
    </div>
    <aside>
      <h3>侧边栏</h3>
      <ul>
        <li>项目1</li>
        <li>项目2</li>
        <li>项目3</li>
        <li>项目4</li>
        <li>项目5</li>
        <li>项目...</li>
      </ul>
    </aside>
  </nav>
  <main>
    <article class="article-main">
      <h3>文章</h3>
      <p>
        Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
        cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
        copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest. Williamsburg neutra tilde umami vice mustache master cleanse green juice kale
        chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette
        DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally
        kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.
      </p>
    </article>
  </main>
</section>
<footer>这是页脚</footer>

<!-- 结束代码片段 -->

希望这能帮助您!如果您有任何其他问题,请随时提出。

英文:

Well, here's my crappy attempt:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
box-sizing: border-box;
}
:root {
--aside-width: 300px;
--aside-height: 250px;
}
html,
body,
.container {
height: 100%;
}
body {
margin: 0;
background-color: red;
}
header,
footer {
font-size: 50px;
color: white;
background-color: orange;
}
section {
position: relative;
}
nav {
display: grid;
grid-template-columns: var(--aside-width) 1fr;
grid-template-areas: &#39;nav-left nav-right&#39; &#39;aside .&#39;;
position: sticky;
top: 0;
background-color: transparent;
}
nav a {
color: white;
}
aside {
grid-area: &#39;aside&#39;;
width: var(--aside-width);
height: var(--aside-height);
background-color: violet;
font-size: 18px;
}
main {
display: flex;
flex-flow: row nowrap;
}
.article-main {
padding: 1em;
padding-inline-start: calc(var(--aside-width) + 1em);
background-color: red;
font-size: 26px;
}
.nav-left {
grid-area: &#39;nav-left&#39;;
background-color: green;
}
.nav-right {
grid-area: &#39;nav-right&#39;;
background-color: indigo;
}

<!-- language: lang-html -->

&lt;header&gt;THIS IS A HEADER&lt;/header&gt;
&lt;section&gt;
&lt;nav&gt;
&lt;div class=&quot;nav-left&quot;&gt;
&lt;a href=&quot;#&quot; id=&quot;logo&quot;&gt;&lt;span class=&quot;material-symbols-outlined&quot;&gt; sentiment_satisfied &lt;/span&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;nav-right&quot;&gt;
&lt;a href=&quot;#&quot;&gt;TOP LINK 1&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;TOP LINK 2&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;TOP LINK 3&lt;/a&gt;
&lt;/div&gt;
&lt;aside&gt;
&lt;h3&gt;sidebar&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Item 1&lt;/li&gt;
&lt;li&gt;Item 2&lt;/li&gt;
&lt;li&gt;Item 3&lt;/li&gt;
&lt;li&gt;Item 4&lt;/li&gt;
&lt;li&gt;Item 5&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;/ul&gt;
&lt;/aside&gt;
&lt;/nav&gt;
&lt;main&gt;
&lt;article class=&quot;article-main&quot;&gt;
&lt;h3&gt;article&lt;/h3&gt;
&lt;p&gt;
Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over
cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO
copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest. Williamsburg neutra tilde umami vice mustache master cleanse green juice kale
chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie, church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette
DIY mixtape. Gastropub leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO copper mug coloring book tattooed waistcoat. Dreamcatcher plaid literally
kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.
&lt;/p&gt;
&lt;/article&gt;
&lt;/main&gt;
&lt;/section&gt;
&lt;footer&gt;THIS IS A FOOTER&lt;/footer&gt;

<!-- end snippet -->

答案3

得分: 0

I don't know how I did it, but I was able to get everything accomplished. I think a key factor is remembering that nested flexboxes do not account for the height of sibling(?) flexboxes. So when trying to give the sidebar (aside) a height of 100vh, it was doing just that, but not accounting for the height of the header & top navbar. I solved that with a calc: height: calc(100vh - (var(--top-navbar-height) + var(--header-height)));

There were some other issues I was having, but managed to work through most of them. The only thing left is to adjust the height of the sidebar (aside) when scrolling up. Because the header disappears, it leaves a whitespace until the footer is displayed. I'm assuming I'll have to use JavaScript to solve that? If anyone has an answer for that, it'd be awesome!

Here is the complete code in case anyone wants a sample:

英文:

I don't know how I did it, but I was able to get everything accomplished. I think a key factor is remembering that nested flexboxes do not account for the height of sibling(?) flexboxes. So when trying to give the sidebar (aside) a height of 100vh, it was doing just that, but but not accounting for the height of the header & top navbar. I solved that with a calc: height: calc(100vh - (var(--top-navbar-height) + var(--header-height)));

There were some other issues I was having, but managed to work through most of them. The only thing left is to adjust the height of the sidebar (aside) when scrolling up. Because the header disappears, it leaves a whitespace until the footer is displayed. I'm assuming I'll have to use JavaScript to solve that? If anyone has an answer for that, it'd be awesome!

Here is the complete code in case anyone wants a sample:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

:root {
--header-height: 50px;
--footer-height: 50px;
--top-navbar-height: 30px;
--aside-width: 250px;
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
/* ! BODY / HTML */
html,
body {
background-color: red;
}
/* ! PAGE CONTAINER */
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
background-color: #ecf0f1;
}
/* ! HEADER/FOOTER */
header,
footer {
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
header {
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
height: var(--header-height);
font-size: 50px;
color: white;
background-color: orange;
}
footer {
height: var(--footer-height);
font-size: 25px;
color: white;
background-color: orange;
left: 0;
bottom: 0;
margin-top: auto;
}
/* ! NAVBAR &amp; MAIN FLEXBOX */
.main {
display: flex;
flex-direction: row;
flex: 1 1 auto;
}
.navbar {
display: flex;
flex-direction: row;
flex: 0 0 auto;
position: sticky;
top: 0;
align-items: center;
padding: 0 10px 0 10px;
height: var(--top-navbar-height);
font-size: 22px;
background-color: yellow;
}
/* ! NAVBAR STYLE */
#nt_left {
flex-grow: 1;
}
#nt_right a {
margin: 10px;
}
/* ! SIDENAV STYLE */
aside {
display: flex;
flex-direction: column;
flex: 0 0 auto;
top: var(--top-navbar-height);
width: var(--aside-width);
height: calc(100vh - (var(--top-navbar-height) + var(--header-height)));
position: sticky;
overflow-y: auto;
padding: 0 10px 0 10px;
background-color: green;
font-size: 18px;
}
/* ! ARTICLE STYLE */
article {
display: flex;
flex-direction: column;
flex: 1 1 auto;
padding: 0 10px 0 10px;
background-color: blue;
font-size: 18px;
}

<!-- language: lang-html -->

	&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;header&gt;THIS IS A HEADER&lt;/header&gt;
&lt;div class=&quot;navbar&quot;&gt;
&lt;div class=&quot;nav_top&quot; id=&quot;nt_left&quot;&gt;
&lt;a href=&quot;#&quot; id=&quot;logo&quot;&gt;&lt;span class=&quot;material-symbols-outlined&quot;&gt; sentiment_satisfied &lt;/span&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;nav_top&quot; id=&quot;nt_right&quot;&gt;
&lt;a href=&quot;#&quot;&gt;TOP LINK 1&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;TOP LINK 2&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;TOP LINK 3&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;main&quot;&gt;
&lt;aside&gt;
&lt;h3&gt;sidebar&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Item 1&lt;/li&gt;
&lt;li&gt;Item 2&lt;/li&gt;
&lt;li&gt;Item 3&lt;/li&gt;
&lt;li&gt;Item 4&lt;/li&gt;
&lt;li&gt;Item 5&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item ...&lt;/li&gt;
&lt;li&gt;Item last&lt;/li&gt;
&lt;/ul&gt;
&lt;/aside&gt;
&lt;article&gt;
&lt;h3&gt;Hipster Ipsum&lt;/h3&gt;
&lt;p&gt;
Williamsburg neutra tilde umami vice mustache master cleanse green juice kale chips godard subway tile lomo. Messenger bag single-origin coffee ascot poke lyft readymade fixie,
church-key gastropub meditation. Af gochujang la croix, readymade blog pour-over cronut paleo offal heirloom. Kale chips +1 polaroid truffaut raclette DIY mixtape. Gastropub
leggings meggings tbh hot chicken slow-carb YOLO mustache lyft VHS deep v fanny pack hell of. Echo park sustainable food truck jean shorts, prism XOXO copper mug coloring book
tattooed waistcoat. Dreamcatcher plaid literally kinfolk bodega boys vaporware before they sold out pop-up fashion axe fit hammock pinterest.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Pok pok deep v schlitz, kickstarter bushwick godard sustainable umami fit small batch. +1 master cleanse offal bushwick photo booth green juice. Copper mug thundercats
stumptown, prism 90&#39;s tonx letterpress cred irony. Bruh messenger bag same organic meggings bushwick bespoke ascot sus franzen schlitz mukbang vinyl aesthetic helvetica.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Bespoke ascot waistcoat blue bottle edison bulb praxis farm-to-table seitan, put a bird on it disrupt 90&#39;s four loko. You probably haven&#39;t heard of them af celiac bushwick.
Mustache butcher coloring book, woke fixie heirloom plaid tonx 90&#39;s swag kickstarter. Craft beer shabby chic celiac try-hard tattooed edison bulb vaporware snackwave iPhone
small batch pok pok next level thundercats. Selvage deep v godard, meh vice tumblr man bun church-key bodega boys synth cloud bread ascot. Godard taiyaki tilde brunch, cornhole
single-origin coffee hexagon DIY asymmetrical lumbersexual hot chicken pabst irony tousled.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Next level hot chicken meggings cold-pressed. Health goth bicycle rights unicorn coloring book before they sold out aesthetic Brooklyn bushwick yr kickstarter sriracha.
Literally ascot tattooed tote bag poutine PBR&amp;B. Hashtag taxidermy ennui 8-bit direct trade truffaut, disrupt prism ascot.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Pickled unicorn messenger bag helvetica pitchfork gluten-free. Next level roof party craft beer whatever tattooed, humblebrag pour-over mukbang ascot raw denim bushwick
waistcoat cloud bread austin intelligentsia. Taxidermy small batch iceland gatekeep ethical paleo gluten-free tote bag authentic neutra vaporware tumblr man braid. Green juice
slow-carb kombucha brunch chillwave plaid. Adaptogen pinterest forage, humblebrag polaroid meh next level street art big mood pug authentic bushwick. Narwhal humblebrag
meditation try-hard 90&#39;s. Organic fanny pack fingerstache mukbang, keytar vegan deep v normcore.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Hexagon skateboard listicle trust fund, shabby chic kickstarter bodega boys forage four loko slow-carb photo booth. Enamel pin prism mustache glossier 8-bit. Gochujang trust
fund seitan, drinking vinegar distillery lumbersexual slow-carb sustainable. Umami fingerstache quinoa, organic Brooklyn humblebrag poke butcher tofu.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Praxis truffaut activated charcoal kickstarter neutra. Selvage hexagon organic bruh heirloom kombucha farm-to-table actually quinoa artisan. Neutra vibecession gastropub,
locavore gluten-free polaroid succulents subway tile taxidermy sartorial poutine viral. Skateboard iceland vegan, tote bag sartorial chartreuse thundercats. Chambray same
biodiesel poke artisan keytar schlitz. Jean shorts kickstarter mixtape jianbing cold-pressed, meditation organic ramps godard post-ironic fixie skateboard. Pickled next level
jean shorts celiac shabby chic freegan.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Live-edge XOXO seitan, schlitz bushwick cornhole glossier iPhone bruh waistcoat. Fit hot chicken quinoa bodega boys semiotics, glossier farm-to-table ennui. Cardigan
letterpress coloring book schlitz viral kinfolk asymmetrical occupy succulents heirloom poutine squid flannel actually. 3 wolf moon snackwave knausgaard banjo 90&#39;s deep v you
probably haven&#39;t heard of them.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Flannel twee mukbang, church-key four dollar toast pour-over cardigan schlitz art party 8-bit wolf JOMO. Listicle shoreditch typewriter, ennui jianbing kombucha copper mug
lo-fi. Kale chips +1 pitchfork beard YOLO yes plz locavore shabby chic yuccie vexillologist leggings hot chicken lomo PBR&amp;B wolf. Farm-to-table shabby chic four loko venmo
tumeric everyday carry bushwick leggings ugh chillwave bodega boys tilde readymade vape.
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
Prism cray street art, bushwick gochujang pickled mumblecore williamsburg plaid biodiesel polaroid next level gastropub lo-fi. Glossier pickled snackwave disrupt. Kogi celiac
lomo banh mi chartreuse banjo. Tofu lumbersexual craft beer sustainable, butcher woke knausgaard kogi.
&lt;/p&gt;
&lt;/article&gt;
&lt;/div&gt;
&lt;footer&gt;THIS IS A FOOTER&lt;/footer&gt;
&lt;/div&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 12:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定