CSS,HTML布局:绝对div内的相对div在其容器外流动

时间:2012-01-11 19:30:02

标签: html css layout css-position

(如果在其他地方得到解答,请提前道歉。我环顾四周,找不到任何似乎适用的东西。)

我有一个绝对和相对定位的布局,让div坐在适当的位置,几乎可以工作。

http://jsfiddle.net/dex3703/TZzKN/

#centercontainer(红色)旨在让#mainsection(orange)位于所有其他内容中。我有绝对和相对的定位,我认为应该有效,但我有这些问题:

  • #mainsection的底部超出#centercontainer约20px。
  • 底部,左侧,右侧,顶部没有效果。使用底部:20px从底部向上抬起不起作用。
  • 如果我没有高度:100%在#mainsection上 - 我感觉是错的 - 它和里面的div没有高度!在#mainsection的顶部只能看到一小段#contentsection。

我只对Chrome和IE9中的这个工作感兴趣。希望能让事情变得更轻松。

我确定这是一些菜鸟问题,所以我要提前感激。谢谢。

2 个答案:

答案 0 :(得分:1)

当您将#mainsection的宽度和高度设置为100%时,您告诉它与#centercontainer的宽度和高度相同,不包括填充。每侧有60px,因为#centercontainer的填充是左右60px。

#main section扩展到#centercontainer下面的原因是#breadcrumbcontainer正在将它推低40px(#breadcrumbcontainer的高度)。它只向下延伸20px,因为#mainsection还有20px的底部填充。

如果将#centercontainer的底部填充设置为#breadcrumbcontainer的高度(INCLUDING#breadcrumbcontainer的顶部和底部边距),那么应该解决问题。它可能不是解决它的最优雅方式。但它应该有用。

答案 1 :(得分:0)

我对你的CSS进行了一些更改。它仍然没有漂亮的样式表,但设计不再破坏。

@charset "utf-8";

/*
HTML4 layout
using absolute positioning to get layout to work without HTML5/CSS flexbox

colors:
#1E242D - html frame bg
#F3F3F3 - left nav
#FFFFFF - main content, inner menu
#267EB9 - drawer bg

#DBDBDB - nav and left nav border
#4F9DD7 - selected item blue

#FFFFFF - white text
#4A4A4A - darkest text, dividers
#767676 - middle dark text
#C1C1C1 - light text

*/

html {
    font-size: 62.5%;
    height: 100%;
    background: White;
    margin: 0;
}

body
{
    font-size: 1.2em;
    font-family: "Segoe UI";
    height: 100%;
    margin: 0;
}

p
{
    margin: 0;

}

/* from html5boilerplate */
nav ul, nav ol  
{
    list-style: none;
    list-style-image: none;
    margin: 0;
    padding: 0;
}

#layoutdiv
{
    z-index: 100;    
    height: 100%;
    position: relative;

}

#maindiv
{
    margin: 10px 60px;
    position: absolute;
    top: 60px;
}

header
{
    position: absolute; /* change to absolute positioning */
    width: 100%;
    top: 0;

    height: 60px;

}

header p
{
    color: #FFFFFF;
}

header span
{
    vertical-align: middle;
}

#logodiv
{
    width: 300px;
    display: inline-block;
}

#notifypanel
{
    width: 40px;
    position: fixed;
    left: 0;
    top: 110px;
    bottom: 70px;
    background-color: LemonChiffon;
}

#notifypanel img
{
    width: 24px;
    height: 24px;
}

.left
{
    float: left;

}

.right
{
    float: right;

}

ul
{
    list-style-type:none;
    list-style: none;
    margin: 0;
    -webkit-padding-start: 0;    
    -webkit-margin-before: 0;
    -webkit-margin-after: 0;

}

li
{
    display: inline-block; /*need inline-block or height==0! */    
    cursor: pointer;
    vertical-align: middle;
    text-align:center;

}

#topnav
{

    /* position: absolute;*/
    /*width: 100%;*/
    /* top: 55px; with absolute positioning, move its top to the header's bottom */
    display: inline-block;
    vertical-align: top;
}

#topnav ul
{

    /*margin-left: 15px;*/
    /*height: 100%;*/
}

#topnav li
{
    margin-left: 15px;
    padding: 5px 10px;
    font-weight: bold;
    color: #767676;
    height: 100%;
    /*line-height: 5em;  for vertical alignment */

}

#topnav li:hover
{
    color: #1E242D;
    background-color: #F3F3F3;
}

#topnav .selected
{
    background-color: #4F9DD7;
    color: White;
}

#centercontainer
{
    position: absolute;
    overflow:hidden;
    top: 60px;
    bottom: 60px;
    padding: 0 60px 20px 60px;
    left: 0;
    right: 0;

    border: 1px solid red;

}

#breadcrumbcontainer
{

    top: 0;
    height: 40px;
    width: 100%;
    margin: 0 0 10px 0;
    font-family: Segoe UI Light;
    font-size: 3.6em;
    line-height: 0.8em;

}

#breadcrumb
{
    display: inline-block;
}

#viewcontrols
{
    display: inline-block;
    width: 320px;
    text-align: right;
}

#mainsection
{

    width: 100%;
    height: auto; /* mainseciton won't display unless 100%, but height is off, doesn't quite sit in containing div */
    position: relative;
    top:30px;
    background-color: Aqua;

    border: 2px solid orange;

}

#contentsection
{

    position: absolute;
    left: 220px;
    bottom: 0;
    top: 0;
    padding: 5px;
    box-sizing: border-box; /* makes padding, margins behave like XAML */
    overflow-x: auto;
    overflow-y: auto;

    background-color: AliceBlue; /* remove later */
    display: inline-block;

}

#leftnav
{
    width: 200px;
    /*position: absolute;*/
    left: 0;
    bottom: 0;
    top: 0;
    background: #F3F3F3;
    overflow-y: auto;
    display: inline-block;
}

#leftnav li
{
    display: block;
    text-align: left;
    padding: 7px 0 7px 20px;
    border-bottom: 1px solid #DBDBDB;


}

#leftnav img
{
    display: inline-block;
    vertical-align: middle;
    width: 24px;
    height: 22px;
}

#leftnav p
{
    display: inline-block;
    vertical-align: middle;
    margin: 0 0 0 10px;
}

#leftnav .selected
{
    background-color: #4F9DD7;
}

#leftnav .selected:hover
{
    background-color: #4387B7;
}

#leftnav li:hover
{
    background-color: #FdFdFd;
}


footer
{
    background: #267EB9;
    height: 60px;
    position: absolute;
    bottom: 0;
    width: 100%;
    line-height: 1em;  /* vertically centers header content .. see http://phrogz.net/CSS/vertical-align/index.html */
    text-align: center; /* centers the center button div */
}

footer img
{
    margin-right: 15px;
    height: 24px;
    width: 24px;
}

footer p
{
    display: inline-block;
    margin: 0;
    vertical-align: super;
    color: #FFFFFF;
    font-size: 0.8em;
}

.footerleft
{
    margin-left: 20px;
    position: relative;
    top: 17px;
}

.footerright
{
    margin-right: 10px;
    position: relative;
    top: 17px;
}

.footercenter
{
    display: inline-block;
    position: relative;
    top: 13px;
}

.footercenter div
{
    display: inline-block;
    margin-right: 20px;
}

.footercenter p
{
    display: block;
}

.footercenter img
{
    margin: 0;
}