Force sidebar height 100% using CSS (with a sticky bottom image)?

后端 未结 15 1177
再見小時候
再見小時候 2020-12-02 05:40

I\'ve been banging my head against the wall for hours trying to figure out this issue and think it must be something small I\'m missing. I\'ve searched online, but nothing

相关标签:
15条回答
  • 2020-12-02 06:14

    UPDATE: As this answer is still getting votes both up and down, and is at the time of writing eight years old: There are probably better techniques out there now. Original answer follows below.


    Clearly you are looking for the Faux columns technique :-)

    By how the height-property is calculated, you can't set height: 100% inside something that has auto-height.

    0 讨论(0)
  • 2020-12-02 06:15

    I have run into this issue several times on different projects, but I have found a solution that works for me. You have to use four div tags - one that contains the sidebar, the main content, and a footer.

    First, style the elements in your stylesheet:

    #container {
    width: 100%;
    background: #FFFAF0;
    }
    
    .content {
    width: 950px;
    float: right;
    padding: 10px;
    height: 100%;
    background: #FFFAF0;
    }
    
    .sidebar {
    width: 220px;
    float: left;
    height: 100%;
    padding: 5px;
    background: #FFFAF0;
    }
    
    #footer {
    clear:both;
    background:#FFFAF0;
    }
    

    You can edit the different elements however you want to, just be sure you dont change the footer property "clear:both" - this is very important to leave in.

    Then, simply set up your web page like this:

    <div id=”container”>
    <div class=”sidebar”></div>
    <div class=”content”></div>
    <div id=”footer”></div>
    </div>
    

    I wrote a more in-depth blog post about this at http://blog.thelibzter.com/how-to-make-a-sidebar-extend-the-entire-height-of-its-container. Please let me know if you have any questions. Hope this helps!

    0 讨论(0)
  • 2020-12-02 06:16

    Position absolute, top:0 and bottom:0 for the sidebar and position relative for the wrapper (or container) witch content all the elements and it's done !

    0 讨论(0)
  • 2020-12-02 06:17

    Until CSS's flexbox becomes more mainstream, you can always just absolutely position the sidebar, sticking it zero pixels away from the top and bottom, then set a margin on your main container to compensate.

    JSFiddle

    http://jsfiddle.net/QDCGv/

    HTML

    <section class="sidebar">I'm a sidebar.</section>
    
    <section class="main">I'm the main section.</section>
    

    CSS

    section.sidebar {
      width: 250px;
      position: absolute;
      top: 0;
      bottom: 0;
      background-color: green;
    }
    
    section.main { margin-left: 250px; }
    

    Note: This is an über simple way to do this but you'll find bottom does not mean "bottom of page," but "bottom of window." The sidebar will probably abrubtly end if your main content scrolls down.

    0 讨论(0)
  • 2020-12-02 06:26

    Try this. It forces navbar to grow as content added, and keeps main area centered.

       <html>
            <head>
                <style>
                        section.sidebar {
              width: 250px;
              min-height:100vh;
              position: sticky;
              top: 0;
              bottom: 0;
              background-color: green;
            }
    
            section.main { position:sticky; top:0;bottom:0;background-color: red; margin-left: 250px;min-height:100vh; }
                </style>
                <script lang="javascript">
                var i = 0;
                function AddOne()
                {
                    for(i = 0;i<20;i++)
                    {
                    var node = document.createElement("LI");
                    var textnode = document.createTextNode(' Water ' + i.toString());
    
                    node.appendChild(textnode);
                    document.getElementById("list").appendChild(node);
                    }
    
    
                }
                </script>
            </head>
            <body>
                    <section class="sidebar">
                        <button id="add" onclick="AddOne()">Add</button>
                        <ul id="list">
                            <li>bullshit 1</li>
                        </ul>
                    </section>
                    <section class="main">I'm the main section.</section>
            </body>    
        </html>
    
    0 讨论(0)
  • 2020-12-02 06:28

    I was facing the same problem as Jon. TheLibzter put me on the right track, but the image that has to stay at the bottom of the sidebar was not included. So I made some adjustments...

    Important:

    • Positioning of the div which contains the sidebar and the content (#bodyLayout). This should be relative.
    • Positioning of the div that has to stay at the bottom of the sidbar (#sidebarBottomDiv). This should be absolute.
    • The width of the content + the width of the sidebar must be equal to the width of the page (#container)

    Here's the css:

        #container
        {
            margin: auto;
            width: 940px;
        }
        #bodyLayout
        {
            position: relative;
            width: 100%;
            padding: 0;
        }
        #header
        {
            height: 95px;
            background-color: blue;
            color: white;
        }
        #sidebar
        {
            background-color: yellow;
        }
        #sidebarTopDiv
        {
            float: left;
            width: 245px;
            color: black;
        }
        #sidebarBottomDiv
        {
            position: absolute;
            float: left;
            bottom: 0;
            width: 245px;
            height: 100px;
            background-color: green;
            color: white;
        }
        #content
        {
            float: right;
            min-height: 250px;
            width: 695px;
            background-color: White;
        }
        #footer
        {
            width: 940px;
            height: 75px;
            background-color: red;
            color: white;
        }
        .clear
        {
            clear: both;
        }
    

    And here's the html:

    <div id="container">
        <div id="header">
            This is your header!
        </div>
        <div id="bodyLayout">
            <div id="sidebar">
                <div id="sidebarTopDiv">
                    This is your sidebar!                   
                </div>
                <div id="content">                  
                This is your content!<br />
                The minimum height of the content is set to 250px so the div at the bottom of
                the sidebar will not overlap the top part of the sidebar.
                </div>
                <div id="sidebarBottomDiv">
                    This is the div that will stay at the bottom of your footer!
                </div>
                <div class="clear" />
            </div>
        </div>
    </div>
    <div id="footer">
        This is your footer!
    </div>
    
    0 讨论(0)
提交回复
热议问题