How can I get a scroll bar only on <main>?

David Thielen 2,276 Reputation points
2023-05-04T08:59:34.54+00:00

This is for Blazor but I've reduced it to very simple HTML (this is basically my MainLayout.razor file).

I want to locate <header> and <nav> at the top of the page and <footer> at the bottom. And all three are always visible, they do not scroll away. And have <footer> always at the bottom.

And - the main problem I'm hitting - I want the vertical scroll bar to only appear when needed, and to be for just <main>. Only <main> is scrolled and the scroll bar is in just the <main> part.

And the height of the <header>, <nav>, and <footer> is variable. The <header> can be null (height == 0) in many cases.

How can I accomplish this?

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="main.css" />
    <title>Basics</title>
</head>
<body>

<header class="my-header">
    <p>Nicolay Copy</p>
</header>

<nav class="my-nav">
    <ul>
        <li><a href="#home">Home</a></li>
        <li><a href="#news">News</a></li>
        <li><a href="#contact">Contact</a></li>
        <li style="float:right"><a class="active" href="#about">About</a></li>
    </ul>
</nav>

    <main class="my-main">
        <p>Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>

        <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle field of that war. We come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do.</p>

        <p>But, in a larger sense, we can not dedicate we can not consecrate we can not hallow, this ground The brave men, living and dead, who struggled here, have hallowed it, far above our poor power to add or detract. The world will little note, nor long remember what we say here; while it can never forget what they did here.</p>

        <p>It is rather for us, the living, we here be dedicated to the great task remaining before us that, from these honored dead we take increased devotion to that cause for which they here, gave the last full measure of devotion that we here highly resolve these dead shall not have died in vain; that the nation, shall have a new birth of freedom, and that government of the people, by the people, for the people, shall not perish from the earth.</p>
    </main>

<footer class="my-footer">
    <p>Lincoln speech text is in the public domain; the organization, remaining text, and photo on this page are copyright 2020 Abraham Lincoln Online. All rights reserved.</p>
</footer>

</body>
</html>

css file:

body {
    margin: 0 0 0 0;
    padding: 0 0 0 0;
}

.my-header {
    background-color: lightcoral;
    margin: 0;
    padding: 0;
    position: sticky;
    top: 0;
}

.my-nav {
    background-color: lightgreen;
    margin: 0;
    padding: 0;
    position: sticky;
}

nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

nav li {
    float: left;
    padding: 0px 16px 0px 16px;
}

.my-main {
    flex: 1;
    overflow: auto;
    height: 100%;
    background-color: lightyellow;
    margin: 0;
    padding: 0;
}

.my-footer {
    z-index: 100;
    background-color: lightblue;
    margin: 0;
    padding: 0;
    position: fixed;
    bottom: 0;
    width: 100%;
}

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,390 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2023-05-11T06:45:00.52+00:00

    Hi @David Thielen

    Here's a solution that is sooo close. One problem - that vertical scroll bar is there even if not needed. And it will then scroll to shift the top line up to hide its margin. Any ideas?

    For the container div (which contains <p> tag), try to set the overflow property to auto:

    <html style="height: 100%; overflow-y: hidden;">
    <body style="margin: 0; height: 100%; overflow-y: hidden;">
        <div style="display: flex; flex-direction: column; height: 100%; overflow-y: hidden;">
            <div style="width: 100%; background-color: blueviolet; color: white">
                Hello Header!
            </div>
            <div style="width: 100%; background-color: dodgerblue; color: white">
                Hello Navigation!
            </div>
            <div style="flex-grow: 1; width: 100%; overflow: auto;">
                <div style="height: 100%; overflow: auto;">
                    <p>Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
    
                    <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle field of that war. We come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do.</p>
    
                    <p>But, in a larger sense, we can not dedicate we can not consecrate we can not hallow, this ground The brave men, living and dead, who struggled here, have hallowed it, far above our poor power to add or detract. The world will little note, nor long remember what we say here; while it can never forget what they did here.</p>
    
                    <p>It is rather for us, the living, we here be dedicated to the great task remaining before us that, from these honored dead we take increased devotion to that cause for which they here, gave the last full measure of devotion that we here highly resolve these dead shall not have died in vain; that the nation, shall have a new birth of freedom, and that government of the people, by the people, for the people, shall not perish from the earth.</p>
                </div>
            </div>
            <div style="width: 100%; background-color: dodgerblue; color: white">
                Hello Footer!
            </div>
        </div>
    </body>
    </html>
    

    The output as below:

    image2


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion


3 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,026 Reputation points
    2023-05-04T15:02:27.5166667+00:00

    With css, height=100% means 100% height of parent. As none of the divs parents have an absolute size, the height is not applied. You need to make body and html 100%

    html, body {

    height: 100%; margin: 0 0 0 0; padding: 0 0 0 0; }


  2. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2023-05-09T02:58:42.56+00:00

    Hi @David Thielen

    How can I get a scroll bar only on <main>?

    To set the scroll bar only in the main section instead of the entire page, you can set the overflow property to hidden for the html and body, try to change the CSS style as below:

    html, body {
        height: 100%;
        margin: 0 0 0 0;
        padding: 0 0 0 0;
        overflow: hidden;
    }
    
    body {
        margin: 0 0 0 0;
        padding: 0 0 0 0;
    }
    
    .my-header {
        background-color: lightcoral;
        margin: 0;
        padding: 0;
        position: sticky;
        top: 0;
    }
    
    .my-nav {
        background-color: lightgreen;
        margin: 0;
        padding: 0;
        position: sticky;
    }
    
    nav ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
    }
    
    nav li {
        float: left;
        padding: 0px 16px 0px 16px;
    }
    
    .my-main {
        flex: 1;
        overflow: auto;
        height: 100%;
        background-color: lightyellow;
        margin: 0;
        padding: 0;
    }
    
    .my-footer {
        z-index: 100;
        background-color: lightblue;
        margin: 0;
        padding: 0;
        position: fixed;
        bottom: 0;
        width: 100%;
    }
    
    

    Then, the output as below:

    image2


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion


  3. David Thielen 2,276 Reputation points
    2023-05-10T08:54:06.53+00:00

    Here's a solution that is sooo close. One problem - that vertical scroll bar is there even if not needed. And it will then scroll to shift the top line up to hide its margin. Any ideas?

    <html style="height: 100%; overflow-y: hidden;">
    <body style="margin: 0; height: 100%; overflow-y: hidden;">
    <div style="display: flex; flex-direction: column; height: 100%; overflow-y: hidden;">
        <div style="width: 100%; background-color: blueviolet; color: white">
            Hello Header!
        </div>
        <div style="width: 100%; background-color: dodgerblue; color: white">
            Hello Navigation!
        </div>
        <div style="flex-grow: 1; width: 100%; overflow: auto;">
            <div style="height: 100%;">
                <p>Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
    
                <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle field of that war. We come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do.</p>
    
                <p>But, in a larger sense, we can not dedicate we can not consecrate we can not hallow, this ground The brave men, living and dead, who struggled here, have hallowed it, far above our poor power to add or detract. The world will little note, nor long remember what we say here; while it can never forget what they did here.</p>
    
                <p>It is rather for us, the living, we here be dedicated to the great task remaining before us that, from these honored dead we take increased devotion to that cause for which they here, gave the last full measure of devotion that we here highly resolve these dead shall not have died in vain; that the nation, shall have a new birth of freedom, and that government of the people, by the people, for the people, shall not perish from the earth.</p>
            </div>
        </div>
        <div style="width: 100%; background-color: dodgerblue; color: white">
            Hello Footer!
        </div>
    </div>
    </body>
    </html>
    
    0 comments No comments