html - 强制相对 DIV 承认他的绝对 DIV child

标签 html css

我遇到一个问题,我的 position:relative DIV 不承认他的 position:absolute child 。因此,它会导致下一个 position:relative div 显示在错误的位置。

CSS:

#header{
    margin: 0 auto;
    position:relative;
    width:740px;
    outline:2px solid black
}

#header #logo{
    position:absolute;
    width:218px;
    height:69px;
    background-image:url('../images/Logo.png');
    top:15px;
    left:30px;
    text-indent: -999px;
    overflow:hidden !important;
}

#header #logo a{
    width:218px;
    height: 69px;
}

#header h1{
    color:#437297;
    font-size:26px;
    font-weight: normal;
    position:absolute;
    top:25px;
    right:15px;
    letter-spacing: 0.5px;
}

/* content */

#content{
    position: :relative;
    margin: 0 auto;
    width:1024px;
    outline: 1px solid red;
    min-height:10px;
}

HTML:

<div id="header">
    <div id="logo"><a href="#">some text</a></div>
    <h1>My Page</h1>
</div>

<div id="content">

</div>

演示问题: http://jsfiddle.net/qBbYR/

问题是,黑色轮廓的 DIV 是应该位于页面顶部的页眉 DIV,而红色轮廓的 DIV 是应该紧跟在页眉 DIV 之后的内容 DIV。

如您所见,标题 DIV 忽略了它的子项,因为它们是绝对定位的,这会导致设计问题。

我该怎么做才能解决这个问题?

最佳答案

绝对定位使元素脱离正常流。如果您希望它们影响容器的高度,请不要定位它们。

改为使用填充、边距、 float 和显示内联 block 。

关于html - 强制相对 DIV 承认他的绝对 DIV child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17514744/

相关文章:

html - 无法让背景填充最小宽度

html - CSS, 2 div, 1 expanding 1 fixed, 但需要换行

javascript - web sql 错误 "current version of the database and ` oldVersion` 参数不匹配”

html - 跟踪链接悬停样式

css - 在函数输出之后对元素选择器应用 less 函数

javascript - 谷歌地理编码地址分割

html - 如何去除 HTML5 中 <svg> 和 <td> 之间的空格?

css - 无法使用 React Native Dimensions 将 <TextInput> 光标定位到屏幕的水平中心

css - Webfonts 在 OS X 和 Windows 之间不匹配

css - 在 bootstrap 中定位 div