Search code examples
csstwitter-bootstrapbootstrap-4fluid-layout

How to build a 2 Column (Fixed - Fluid) Layout with Twitter Bootstrap?


Is it possible to create a 2 Column Layout (Fixed - Fluid) Layout ( http://www.dynamicdrive.com/style/layouts/item/css-liquid-layout-21-fixed-fluid/) with Twitter Bootstrap (http://twitter.github.com/bootstrap/)?

Do you have any solutions?


Solution

  • - Another Update -

    Since Twitter Bootstrap version 2.0 - which saw the removal of the .container-fluid class - it has not been possible to implement a two column fixed-fluid layout using just the bootstrap classes - however I have updated my answer to include some small CSS changes that can be made in your own CSS code that will make this possible

    It is possible to implement a fixed-fluid structure using the CSS found below and slightly modified HTML code taken from the Twitter Bootstrap Scaffolding : layouts documentation page:

    HTML

    <div class="container-fluid fill">
        <div class="row-fluid">
            <div class="fixed">  <!-- we want this div to be fixed width -->
                ...
            </div>
            <div class="hero-unit filler">  <!-- we have removed spanX class -->
                ...
            </div>
        </div>
    </div>
    

    CSS

    /* CSS for fixed-fluid layout */
    
    .fixed {
        width: 150px;  /* the fixed width required */
        float: left;
    }
    
    .fixed + div {
         margin-left: 150px;  /* must match the fixed width in the .fixed class */
         overflow: hidden;
    }
    
    
    /* CSS to ensure sidebar and content are same height (optional) */
    
    html, body {
        height: 100%;
    }
    
    .fill { 
        min-height: 100%;
        position: relative;
    }
    
    .filler:after{
        background-color:inherit;
        bottom: 0;
        content: "";
        height: auto;
        min-height: 100%;
        left: 0;
        margin:inherit;
        right: 0;
        position: absolute;
        top: 0;
        width: inherit;
        z-index: -1;  
    }
    

    I have kept the answer below - even though the edit to support 2.0 made it a fluid-fluid solution - as it explains the concepts behind making the sidebar and content the same height (a significant part of the askers question as identified in the comments)


    Important

    Answer below is fluid-fluid

    Update As pointed out by @JasonCapriotti in the comments, the original answer to this question (created for v1.0) did not work in Bootstrap 2.0. For this reason, I have updated the answer to support Bootstrap 2.0

    To ensure that the main content fills at least 100% of the screen height, we need to set the height of the html and body to 100% and create a new css class called .fill which has a minimum-height of 100%:

    html, body {
        height: 100%;
    }
    
    .fill { 
        min-height: 100%;
    }
    

    We can then add the .fill class to any element that we need to take up 100% of the sceen height. In this case we add it to the first div:

    <div class="container-fluid fill">
        ...
    </div>
    

    To ensure that the Sidebar and the Content columns have the same height is very difficult and unnecessary. Instead we can use the ::after pseudo selector to add a filler element that will give the illusion that the two columns have the same height:

    .filler::after {
        background-color: inherit;
        bottom: 0;
        content: "";
        right: 0;
        position: absolute;
        top: 0;
        width: inherit;
        z-index: -1;  
    }
    

    To make sure that the .filler element is positioned relatively to the .fill element we need to add position: relative to .fill:

    .fill { 
        min-height: 100%;
        position: relative;
    }
    

    And finally add the .filler style to the HTML:

    HTML

    <div class="container-fluid fill">
        <div class="row-fluid">
            <div class="span3">
                ...
            </div>
            <div class="span9 hero-unit filler">
                ...
            </div>
        </div>
    </div>
    

    Notes

    • If you need the element on the left of the page to be the filler then you need to change right: 0 to left: 0.