Search code examples
htmlcsspositioning

CSS: absolute position inside centered relative div issue


I have an issue with absolute and relative positioning.

Im trying to center a relative DIV that contains all absolute div. The problem I have is when I try to center my relative DIV, my absolute div "#mainForm" get shrink (height issue).

In the html below, if you remove the position and margin attribute on the "#main" class, you will see that the page layout is displayed correctly.

How can I center my relative div without affecting my absolute divs ?

** What im trying to achieve is to have only my #mainForm that is scrollable. My sideBar, mainHeader and mainFooter must be "fixed". Client requirements...

Thanks David

Here is my CSS and HTML.

  • xhtml11.dtd doctype.

<head>      
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />

    <style>
        html {
          box-sizing: border-box;
        }

        body {
            font-family: Helvetica,Arial,sans-serif;
            font-size: 8pt;  
        }

        *, *:before, *:after {
          box-sizing: inherit;
        }

        #main {
            position: relative;  /* if I removed this, page is not centered but mainForm height is ok */
            margin: 0 auto;     /* if I removed this, page is not centered but mainForm height is ok */
            width: 960px;       
        }

        #sideBar {    
            position: absolute;
            top:0;
            bottom:0;
            left:0;
            width: 180px;   
        }

        #mainContent {   
            position: absolute;
            top:0;
            bottom:0;
            right:0;
            left:180px; /* Width of #sideBar. */
            width: 780px;         
        }

        #mainHeader {
            position: absolute;
            top:0;
            height:40px;
            width:100%; /* Mandatory. With is 100% of parent div. */

            border: 1px solid blue; /* For developing purpose */
        }

        #mainForm {
            position: absolute;
            overflow:auto;   
            top:40px;
            bottom:40px;
            width:100%; /* Mandatory. With is 100% of parent div. */

            border: 1px solid yellow; /* For developing purpose */
        }

        #mainFooter {
            position: absolute;
            bottom:0;
            height:40px;    
            text-align:right;
            width:100%; /* Mandatory. With is 100% of parent div. */
        }

        #topSideBar {
            position: absolute;
            top:0;
            left:0;
            background-image: url("../images/pas/contactLogo.png");
            background-repeat: no-repeat;   
            height:110px;
            width:100%; /* Mandatory. With is 100% of parent div. */
        }

        #middleSideBar {
            position: absolute;
            top:110px;
            height:200px;
            width:100%; /* Mandatory. With is 100% of parent div. */
        }

        #bottomSideBar {
            position: absolute;
            bottom:0;
            height:100px;
            width:100%; /* Mandatory. With is 100% of parent div. */
        }

        /* clearfix */
        .clearFixaa:after {
            content: ".";
            display: block;
            clear: both;
            visibility: hidden;
            line-height: 0;
            height: 0;
        }               
    </style>

</head>

<body>

    <div id="main" class="clearFix">

        <div id="sideBar" >
            <div id="topSideBar">
                <!-- Contact Logo css backgound. -->
                &nbsp;
            </div>
            <div id="middleSideBar">
                middleSideBar
            </div>
            <div id="bottomSideBar">
                bottomSideBar
            </div>              
        </div>

        <div id="mainContent">
            <div id="mainHeader">
                mainHeader
            </div>
            <div id="mainForm">

                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                mainForm
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                mainForm
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                mainForm
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                mainForm
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>
                mainForm
                <br/><br/><br/><br/><br/><br/><br/><br/><br/>                                                           
            </div>
            <div id="mainFooter">
                mainFooter
            </div>      
        </div>

    </div>

</body>


Solution

  • I modified your css. This will do the work:

    html {
        box-sizing: border-box;
    }
    body {
        font-family: Helvetica, Arial, sans-serif;
        font-size: 8pt;
    }
    *, *:before, *:after {
        box-sizing: inherit;
    }
    #main {
        position: fixed;
        /* if I removed this, page is not centered but mainForm height is ok */
        margin: 0 auto;
        /* if I removed this, page is not centered but mainForm height is ok */
        width: 960px;
        height: 960px;
        left: 0;
        right: 0;
    }
    #sideBar {
        position: absolute;
        top:0;
        bottom:0;
        left:0;
        width: 180px;
        border: 1px solid red;
        /* For developing purpose */
    }
    #mainContent {
        position: absolute;
        top:0;
        bottom:0;
        right:0;
        left:180px;
        /* Width of #sideBar. */
        width: 780px;
        height: 100%;
    }
    #mainHeader {
        position: absolute;
        top:0;
        height:40px;
        width:100%;
        /* Mandatory. With is 100% of parent div. */
        border: 1px solid blue;
        /* For developing purpose */
    }
    #mainForm {
        position: absolute;
        overflow:auto;
        top:40px;
        bottom:40px;
        width:100%;
        /* Mandatory. With is 100% of parent div. */
        border: 1px solid yellow;
        /* For developing purpose */
    }
    #mainFooter {
        position: absolute;
        bottom:0;
        height:40px;
        text-align:right;
        width:100%;
        /* Mandatory. With is 100% of parent div. */
    }
    #topSideBar {
        position: absolute;
        top:0;
        left:0;
        background-image: url("../images/pas/contactLogo.png");
        background-repeat: no-repeat;
        height:110px;
        width:100%;
        /* Mandatory. With is 100% of parent div. */
    }
    #middleSideBar {
        position: absolute;
        top:110px;
        height:200px;
        width:100%;
        /* Mandatory. With is 100% of parent div. */
    }
    #bottomSideBar {
        position: absolute;
        bottom:0;
        height:100px;
        width:100%;
        /* Mandatory. With is 100% of parent div. */
    }
    /* clearfix */
     .clearFixaa:after {
        content:".";
        display: block;
        clear: both;
        visibility: hidden;
        line-height: 0;
        height: 0;
    }
    

    The trick was to fixed the #main div and position it with left:0 and right:0. I had a static height for the main div, feel free to remove it and add height where you need like for the sidebar.