简体   繁体   English

CSS:居中相对div问题中的绝对位置

[英]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. 我试图居中包含所有绝对div的相对DIV。 The problem I have is when I try to center my relative DIV, my absolute div "#mainForm" get shrink (height issue). 我遇到的问题是,当我尝试将相对DIV居中时,我的绝对div“ #mainForm”缩小(高度问题)。

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. 在下面的html中,如果删除“ #main”类上的position和margin属性,则会看到页面布局显示正确。

How can I center my relative div without affecting my absolute divs ? 如何在不影响绝对div的情况下居中相对div?

** What im trying to achieve is to have only my #mainForm that is scrollable. 我试图实现的是仅使我的#mainForm可滚动。 My sideBar, mainHeader and mainFooter must be "fixed". 我的sideBar,mainHeader和mainFooter必须“固定”。 Client requirements... 客户要求...

Thanks David 谢谢大卫

Here is my CSS and HTML. 这是我的CSS和HTML。

  • xhtml11.dtd doctype. xhtml11.dtd文档类型。

<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>

I modified your css. 我修改了您的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. 诀窍是固定#main div并将其定位为left:0和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. 我的主要div高度为静态,可以随时将其删除,并在需要的地方添加高度,例如侧边栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM