Jonathan
Jonathan

Reputation: 2845

HTML scrolling layout

I am attempting to design an HTML screen layout with a fixed header and footer along with a scrollable middle. I am trying to use overflow:hidden and overflow:auto in CSS to get a scrollbar for the central, scrollable portion of the layout without success. The code follows:

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  margin-block-start: 0;
  margin-block-end: 0;
  background-clip: padding-box;
  font-family: Arial, Helvetica, sans-serif;
}

* {
  text-rendering: optimizeLegibility;
  box-sizing: border-box;
}

article {
  overflow-y: auto;
}

#site {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow-y: hidden;
}

#hdr {
  font-weight: bold;
  text-align: center;
  padding-bottom: 1rem;
  padding-top: .5rem;
  color: violet;
}

#Document {
  height: auto;
  color: rgb(0, 0, 26);
  margin: 0;
  top: auto;
}
<div id="site">
  <!-- The entire document will initially be invisible
  and will be enabled for display through JavaScript -->
  <header id="hdr">
    Header should be fixed and contains the document title, 
    placed here with Javascript
  </header>
  <article id="Document" style="margin-top:1rem">
    <br><br>
    <br><br><br><br><br><br><br><br><br> 
    Article should scroll when needed. 
    Scroll down manually to see more content.
    <br><br>
    <br><br><br><br><br><br><br><br><br> 
    More text is displayed here
  </article>
  <footer style="margin-top:1rem">
    Footer should be fixed and will contain 
    copyright/publishing information
  </footer>
</div>

Can anyone see what I am doing wrong and make suggestions on how to make it work? I have tried this code on Chrome, Firefox, and Opera running on Ubuntu 20.04 without any success.

Upvotes: 0

Views: 186

Answers (3)

Amin Darian
Amin Darian

Reputation: 187

Try giving the article a specific height, and not a percentage. For instance:

article {
  overflow-y: auto;
  width: 100vh;
}

Upvotes: 0

k123
k123

Reputation: 428

It can be done with a flexbox layout.

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

* {
  text-rendering: optimizeLegibility;
  box-sizing: border-box;
}

#site {
  margin: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

#header {
  font-weight: bold;
  text-align: center;
  padding-bottom: 1rem;
  padding-top: .5rem;
  color: violet;
}

#Document {
  flex: 1;
  overflow: auto;
  background: lightblue;
}

#footer {
  font-weight: bold;
  text-align: center;
  padding-bottom: 1rem;
  padding-top: .5rem;
  color: violet;
}
<div id="site">
  <!-- The entire document will initially be invisible
  and will be enabled for display through JavaScript -->
  <header id="header">
    Header should be fixed and contains the document title, 
    placed here with Javascript
  </header>
  <article id="Document">
    <br><br>
    <br><br><br><br><br><br><br><br><br> 
    Article should scroll when needed. 
    Scroll down manually to see more content.
    <br><br>
    <br><br><br><br><br><br><br><br><br> 
    More text is displayed here
  </article>
  <footer id="footer">
    Footer should be fixed and will contain 
    copyright/publishing information
  </footer>
</div>

Upvotes: 1

schmauch
schmauch

Reputation: 733

You could also use

#hdr {
    position: fixed;
}
footer {
    position: fixed;
    bottom: 0;
}

Or did I misread the question?

Upvotes: 0

Related Questions