Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding scroll bars to <aside>, <main> and other flex layout elements [duplicate]

Tags:

html

css

layout

I'm trying to confine content inside a flex-based layout, which uses the following simple code:

<header>header</header>
<section>
  <aside>aside long content...</aside>
  <main>main long content...</main>
  <aside>aside long content...</aside>
</section>
<footer>footer</footer>

The CSS I have is:

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

header, footer {
  height: 50px;
  min-height: 50px;
  background: black;
  color: #fff;
}

section {
  flex-grow: 1;
  display: flex;
}

section aside  {
  width: 100px;
  background: #ccc; 
  height:100%;
  overflow:scroll;
}

section main {
  overflow:scroll;
  flex-grow: 1;
}

The problem is that when adding long length content to the aside or main elements, the horizontal scrolls in those elements show ok, however the vertical scrolls are not show but the element gets as tall as its content, pushing everything under it (the footer) off screen. The client area then gets vertical scrolls.

I need these elements to still behave as they show with no content, but have scrollers if their content are larger than their screen size.

Instead of this (overflow gets scrolls): enter image description here

I get this (overflow is visible, pushing all elements off): enter image description here

like image 997
Aram Alvarez Avatar asked Jan 13 '17 01:01

Aram Alvarez


People also ask

How do you make aside scrollable?

You can affix the aside bar to either of the sides (preferably the left side) by using float property. Example float:right; About.

Does 100vw include scrollbar?

Many assume that width: 100vw is the same as width: 100% . This is true on a page that doesn't scroll vertically, but what you might not realize is that the viewport actually includes the scrollbars, too.

Why are there two scroll bars CSS?

You're basically getting a double scrollbar because your giving the body min-height of 100vh AND setting an overflow. It appears this was done to keep the menu in the correct position on mobile devices.

How do I fix the scrollbar in CSS?

The easy fix is to use width: 100% instead. Percentages don't include the width of the scrollbar, so will automatically fit. If you can't do that, or you're setting the width on another element, add overflow-x: hidden or overflow: hidden to the surrounding element to prevent the scrollbar.


2 Answers

Remove height: 100%; from section aside as it's not needed. section is set to flex-grow: 1;, which means that whole area will take up the available viewport height, and aside is a flex child of section so the aside's height will automatically fill the height of section.

PS that layout looks familiar ;)

* {margin:0;padding:0;}
body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

header, footer {
  height: 50px;
  min-height: 50px;
  background: black;
  color: #fff;
}

section {
  flex-grow: 1;
  display: flex;
}

section aside  {
  width: 100px;
  overflow:scroll;
}

section main {
  overflow:scroll;
  flex-grow: 1;
}

aside .inner {
  background: #ccc;
}
<header>header</header>
<section>
  <aside><div class="inner"><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p></div></aside>
  <main>main long content...</main>
  <aside><div class="inner">main long content...</div></aside>
</section>
<footer>footer</footer>
like image 157
Michael Coker Avatar answered Oct 17 '22 07:10

Michael Coker


Ok, I think it works now. Seems that section height:0px; is the cure.

CSS

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

header, footer {
  height: 50px;
  min-height: 50px;
  background: black;
  color: #fff;
  transition-property: height;
  transition-duration: 0.2s;
  transition-timing-function: linear;
}

section {
  flex-grow: 1;
  display: flex;
  height:0px; /* <== for scrolls in aside */
}

aside  {
  width: 100px;
  background: #ccc; 
  overflow:auto;
  min-height: 0;
  transition-property: width;
  transition-duration: 0.2s;
  transition-timing-function: linear;
}

section main {
  overflow:auto;
  flex-grow: 1;

}
footer:hover
{
    height:200px;
}
aside:hover
{
    width:200px;
}

html

<header>header</header>
<section>
  <aside><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p></aside>
  <main>main long content...</main>
  <aside><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p><p>main long content...</p></aside>
</section>
<footer>footer</footer>
like image 34
Aram Alvarez Avatar answered Oct 17 '22 07:10

Aram Alvarez