2

I have a simple page. Here i want a fixed header and a fixed footer and the body of the content to be scrollable.

HTML

<div class="container">
    <div class="header">This is Header</div>
    <div class="body">This is Body
        <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body
    <br/>This is Body</div>
    <div class="footer">This is Footer</div>
</div>

CSS

* {
    margin: 0;
    padding: 0;
}
.container {
    width: 100%;
    height: 100%;
    background: #DDD;
}

.header {
    width: 100%;
    height: 40px;
    background: red;
    position: absolute;
    top: 0;
}

.footer {
    width: 100%;
    height: 40px;
    background: green;
    position: absolute;
    bottom: 0;
}

.body {
    width: 100%;
    background: blue;
    overflow: auto;
}

However when i scroll, the whole page tends to scroll, making my header and footer move. I can resolve this using javascript, getting the height of the screen and subtracting the header and footer height.

But is there any way we can achieve this only using css. If yes then how ?

4
  • confused with your question.... what you want to do? Commented Sep 15, 2013 at 12:07
  • Something like this? jsfiddle.net/MXDgM Commented Sep 15, 2013 at 12:07
  • Yes to some extend like that, but without position: fixed. Commented Sep 15, 2013 at 12:10
  • And 1 more point just need to make the div scrollable not the whole page Commented Sep 15, 2013 at 12:11

2 Answers 2

3

Its very simple, just make changes as like this

.body {
    width: 100%;
    background: blue;
    overflow: auto;
    position: absolute;
    bottom: 40px;
    top: 40px;
}

Check this http://jsfiddle.net/rCV6E/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Yes but make have some impact on the smoothness.
you can watch this to improve the smoothness youtube.com/watch?v=QSoRsM5-4Z4
0

try to use css as:

.body {
    width: 100%;
    background: blue;
    overflow: scroll !important;
}

Set overflow to scroll and .header, .footer position to fixed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.