Bootstrap fixed header and footer with scrolling body-content area in fluid-container

后端 未结 3 1340

I was surprised I could not find a simple answer to this problem by Googling, but most responses to scrolling content panels either did not work properly, or did not work wi

相关标签:
3条回答
  • 2020-12-08 02:01

    Another option would be using flexbox.

    While it's not supported by IE8 and IE9, you could consider:

    • Not minding about those old IE versions
    • Providing a fallback
    • Using a polyfill

    Despite some additional browser-specific style prefixing would be necessary for full cross-browser support, you can see the basic usage either on this fiddle and on the following snippet:

    html {
      height: 100%;
    }
    html body {
      height: 100%;
      overflow: hidden;
      display: flex;
      flex-direction: column;
    }
    html body .container-fluid.body-content {
      width: 100%;
      overflow-y: auto;
    }
    header {
        background-color: #4C4;
        min-height: 50px;
        width: 100%;
    }
    footer {
        background-color: #4C4;
        min-height: 30px;
        width: 100%;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
    <header></header>
    <div class="container-fluid body-content">
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
    </div>
    <footer></footer>

    0 讨论(0)
  • 2020-12-08 02:09

    Until I get a better option, this is the most "bootstrappy" answer I can work out:

    JSFiddle: http://jsfiddle.net/TrueBlueAussie/6cbrjrt5/

    I have switched to using LESS and including the Bootstrap Source NuGet package to ensure compatibility (by giving me access to the bootstrap variables.less file:

    in _layout.cshtml master page

    • Move footer outside the body-content container
    • Use boostrap's navbar-fixed-bottom on the footer
    • Drop the <hr/> before the footer (as now redundant)

    Relevant page HTML:

    <div class="container-fluid body-content">
        @RenderBody()
    </div>
    <footer class="navbar-fixed-bottom">
        <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
    </footer>
    

    In Site.less

    • Set HTML and BODY heights to 100%
    • Set BODY overflow to hidden
    • Set body-content div position to absolute
    • Set body-content div top to @navbar-height instead of hard-wiring value
    • Set body-content div bottom to 30px.
    • Set body-content div left and right to 0
    • Set body-content div overflow-y to auto

    Site.less

    html {
        height: 100%;
    
        body {
            height: 100%;
            overflow: hidden;
    
            .container-fluid.body-content {
                position: absolute;
                top: @navbar-height;
                bottom: 30px;
                right: 0;
                left: 0;
                overflow-y: auto;
            }
        }
    }
    

    The remaining problem is there seems to be no defining variable for the footer height in bootstrap. If someone call tell me if there is a magic 30px variable defined in Bootstrap I would appreciate it.

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

    Add the following css to disable the default scroll:

    body {
        overflow: hidden;
    }
    

    And change the #content css to this to make the scroll only on content body:

    #content {
        max-height: calc(100% - 120px);
        overflow-y: scroll;
        padding: 0px 10%;
        margin-top: 60px;
    }
    

    See fiddle here.


    Edit:

    Actually, I'm not sure what was the issue you were facing, since it seems that your css is working. I have only added the HTML and the header css statement:

    html {
      height: 100%;
    }
    html body {
      height: 100%;
      overflow: hidden;
    }
    html body .container-fluid.body-content {
      position: absolute;
      top: 50px;
      bottom: 30px;
      right: 0;
      left: 0;
      overflow-y: auto;
    }
    header {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        background-color: #4C4;
        height: 50px;
    }
    footer {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #4C4;
        height: 30px;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
    <header></header>
    <div class="container-fluid body-content">
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
      Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>Lorem Ipsum<br/>
    </div>
    <footer></footer>

    0 讨论(0)
提交回复
热议问题