【问题标题】:CSS, HTML layout: relative div inside absolute div flows outside its containerCSS、HTML 布局:绝对 div 内的相对 div 在其容器外流动
【发布时间】:2012-02-08 04:09:42
【问题描述】:

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

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

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

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

  • #mainsection 的底部超出 #centercontainer 约 20 像素。
  • bottom、left、right、top 没有效果。使用 bottom: 20px 将底部向上抬起不起作用。
  • 如果我在#mainsection 上没有高度:100%——我觉得这是错误的——它和里面的 div 没有高度!在#mainsection 的顶部只能看到一小部分#contentsection。

我只对这个在 Chrome 和 IE9 中的工作感兴趣。希望这会让事情变得更容易。

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

【问题讨论】:

  • 如果我理解你想要做什么,那么你就会理解 relativeabsolute 的作用相反。您为容器提供相对定位,以便子项可以绝对定位在其中(否则绝对定位会“冒泡”,直到找到有效的容器,通常是页面本身)。
  • 值得一提的是,简单地翻转两种定位类型并不能解决问题;它会改变布局模型,你基本上需要重写。 ;-) 我根本不相信你需要绝对定位。
  • @GregPettit - 感谢您的解释。 HTML/CSS 布局让我感到莫名其妙,我仍然在犯这些错误。 flexbox 要容易得多,但我想要一些可以在较旧的 IE 浏览器上运行的东西。你是说我不需要对整个布局进行绝对定位?
  • 差不多。有了布局,总有几十种方法可以给猫剥皮。当然,有很多方法可以在不使用绝对定位的情况下做你想做的事情。我认为绝对定位是有时间和地点的,但通常没有它你会有更灵活的布局。

标签: html css layout css-position


【解决方案1】:

我对您的 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;
}

【讨论】:

  • @Greg:谢谢你的小提琴,我没时间做一个。
  • 我不认为这很有效。当您垂直缩小浏览器窗口时,#mainsection 不会保持居中并显示内容的滚动条。它在页脚后面向下滚动。
  • 我只为小提琴视图修复了它。需要清理整个 CSS。我不认为整个设计方法很有意义。
【解决方案2】:

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

#mainsection 延伸到 #centercontainer 下方的原因是 #breadcrumbcontainer 将其向下推 40 像素(#breadcrumbcontainer 的高度)。它只向下延伸 20 像素,因为#mainsection 也有 20 像素的底部填充。

如果您将#centercontainer 的底部填充设置为#breadcrumbcontainer 的高度(包括#breadcrumbcontainer 的顶部和底部边距),则应该可以解决问题。这可能不是修复它的最优雅的方法。但它应该可以工作。

【讨论】:

  • 谢谢。我将此标记为答案,尽管我认为其他人也可以,并且同意我的布局并不理想。这是一个可能会发生很大变化的原型,所以我正在调整另一个布局。感谢您的帮助和解释。我尝试了没有帮助的边距,也没有考虑尝试填充。
猜你喜欢
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
相关资源
最近更新 更多