CSS、HTML布局:绝对div内的相对div在其容器外流动
(如果在别处得到答复,我会提前道歉。我环顾四周,找不到任何适用的答案。) 我有一个带有绝对和相对定位的布局,可以让div坐在适当的位置,这几乎是可行的 #centercontainer(红色)用于让#mainsection(橙色)位于所有其他内容中。我有绝对和相对定位,我认为应该有效,但我有以下问题:CSS、HTML布局:绝对div内的相对div在其容器外流动,html,css,layout,css-position,Html,Css,Layout,Css Position,(如果在别处得到答复,我会提前道歉。我环顾四周,找不到任何适用的答案。) 我有一个带有绝对和相对定位的布局,可以让div坐在适当的位置,这几乎是可行的 #centercontainer(红色)用于让#mainsection(橙色)位于所有其他内容中。我有绝对和相对定位,我认为应该有效,但我有以下问题: #mainsection的底部超出#centercontainer约20px 底部、左侧、右侧、顶部没有效果。使用bottom:20px将底部向上提升不起作用 如果我没有高度:100%在#ma
- #mainsection的底部超出#centercontainer约20px
- 底部、左侧、右侧、顶部没有效果。使用bottom:20px将底部向上提升不起作用
- 如果我没有高度:100%在#main部分——我感觉这是错误的——它和里面的div没有高度!在#main部分的顶部只能看到一小部分#contentsection
我相信这是一个noob问题,所以我提前表示感谢。谢谢 当您将#main section的宽度和高度设置为100%时,您告诉它与#centercontainer的宽度和高度相同,不包括填充。因为#centercontainer的填充为60px左右,所以每侧都有60px #main section延伸到#centercontainer下方的原因是#breadcrumbcontainer将其向下推40px(即#breadcrumbcontainer的高度)。它只向下延伸20px,因为#main部分也有20px的底部填充
如果将#centercontainer的底部填充设置为#breadcrumbcontainer的高度(包括#breadcrumbcontainer的顶部和底部边距),则应该可以解决此问题。这可能不是最优雅的修复方法。但是它应该可以工作。我对你的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;
}
如果我理解你想做什么,你就理解了
relative
和absolute
的作用。你给容器相对定位,这样子容器就可以被绝对定位(否则绝对定位“气泡”直到它找到一个有效的容器,通常是页面本身)。值得一提的是,简单地翻转两种定位类型并不能解决问题;它将改变布局模型,使您基本上需要重写我并不认为你需要绝对定位。@GregPettit-谢谢你的解释。HTML/CSS布局让我感到困惑,我仍然在犯这样的错误。flexbox要简单得多,但我希望它能在旧的IE浏览器上工作。你是说我不需要整个布局的绝对定位?差不多。有了布局,猫的皮总有几十种方法。当然,有很多方法可以在不使用绝对定位的情况下完成您想要做的事情。我认为绝对定位是有时间和地点的,但通常没有绝对定位,布局会更灵活。@Greg:谢谢你的小提琴,我没有时间做一把。我觉得这不太管用。垂直收缩浏览器窗口时,#mainsection不会居中显示内容的滚动条。它在页脚后面向下滚动。我只为fiddle视图修复了它。整个CSS需要清理。我不认为整个设计方法很有意义。谢谢。我把这个标记为答案,尽管我认为其他的也可以,并且同意我的布局并不理想。这是为一个原型,可能会改变很多,所以是适应另一个布局我有。谢谢你的帮助和解释。我试过边距,但没用,也没想过要填充边距。