w--
w--

Reputation: 6697

I need scrollbar on inner element, not browser window (viewport)

In the code below, how do I make the article container auto grow to consume the remaining vertical space below it, but the scroll bar to remain only for that element.

In other words, I want only the inside of article to scroll and not the entire browser window.

Is there a pure css solution? Or do I need javascript to detect the size of the browser window and adjust the height property of article dynamically?

html, body {
  //height: 100%;    

}
#outer_container {
  display: flex;
  flex-direction: row;
}
#outer2 {
  flex: 1 0 auto;
}
#outer2 {
  flex: 1 0 auto;
}
#container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 50%;
  background-color: red;
}
#container header {
  background-color: gray;
}
#container article {
  flex: 1 1 auto;
  overflow-y: auto;
  height: 0px;
}
#container footer {
  background-color: gray;
}
<div id="outer_container">
  <div id="outer1">
    <h2>Outer 1</h2>
  </div>

  <section id="container">
    <header id="header">This is a header</header>
    <article id="content">
      This is the content that
      <br />With a lot of lines.
      <br />With a lot of lines.
      <br />This is the content that
      <br />With a lot of lines.
      <br />
      <br />This is the content that
      <br />With a lot of lines.
      <br />
      <br />This is the content that
      <br />With a lot of lines.
      <br />
    </article>
    <footer id="footer">This is a footer</footer>
  </section>


  <div id="outer2">
    <h2>Outer 2</h2>
  </div>
</div>

http://jsfiddle.net/ch7n6/907/

It was originally based on the answer to this question:
Flexbox and vertical scroll in a full-height app using NEWER flexbox api

Upvotes: 1

Views: 1630

Answers (2)

Michael Benjamin
Michael Benjamin

Reputation: 372029

In your code you commented out:

html, body {
  //height: 100%;    
}

But I think you were on the right track.

By uncommenting that rule, and adding height: 100% to .outer_container, your layout may be complete.

Try this:

html, body {
  height: 100%;                   /* uncommented */
  margin: 0;                      /* removes default margin */
}
#outer_container {
  height: 100%;                   /* new; see details below */
  display: flex;
  flex-direction: row;
}
#outer2 {
  flex: 1 0 auto;
  background-color: lightblue;     /* just for demo */
}
#outer1 {                          /* correction to duplicate ID */
  flex: 1 0 auto;
  background-color: lightgreen;    /* just for demo */
}
#container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 50%;
  background-color: red;
}
#container header {
  background-color: gray;
}
#container article {
  flex: 1 1 auto;
  overflow-y: auto;
  height: 0px;
}
#container footer {
  background-color: gray;
}
<div id="outer_container">
  <div id="outer1">
    <h2>Outer 1</h2>
  </div>
  <section id="container">
    <header id="header">This is a header</header>
    <article id="content">
      This is the content that
      <br />With a lot of lines.
      <br />With a lot of lines.
      <br />This is the content that
      <br />With a lot of lines.
      <br />
      <br />This is the content that
      <br />With a lot of lines.
      <br />
      <br />This is the content that
      <br />With a lot of lines.
      <br />
    </article>
    <footer id="footer">This is a footer</footer>
  </section>
  <div id="outer2">
    <h2>Outer 2</h2>
  </div>
</div>

jsFiddle

To understand how this solution works, and what may have held you up, take a look at these posts:

Upvotes: 1

Nadia Chibrikova
Nadia Chibrikova

Reputation: 5036

You can try setting position:fixed to your outer container (http://jsfiddle.net/ch7n6/909/):

#outer_container{
display:flex;
flex-direction:row;
top:0;
bottom:0;
position:fixed;
}

If it doesn't work for your design, you can change the container dimensions using window.onresize event.

Upvotes: 1

Related Questions