css - sibling div 不占用位置相对且顶部为负的 div 的空间

标签 css css-position

<div class="wrapper">
<div class="header">

</div>
<div class="featured">

</div>

  </div>

CSS 看起来像这样

    .header { 
    background: green;

    height:620px;
    }
    .footer {
    background: blue;

    height:200px;
    }
    .featured {
    background: yellow;
    width:500px;
    height:420px;
    margin:0 auto;
    position: relative;
    top: -200px;
    }

在插入负顶部时,silbing div“页脚”不会相应地向上移动。这是两个 div 之间的一大片空白区域

这是我的代码 http://codepen.io/adrianmak/pen/qZRqwy

最佳答案

margin-top:-200px 而不是 top:-200px。因为它是相对元素。即使您通过负顶部移动它也会占用空间。

.featured {
        background: yellow;
        width:500px;
        height:420px;
        margin:-200px auto 0;
        position: relative;
}

Working Fiddle

关于css - sibling div 不占用位置相对且顶部为负的 div 的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051708/

相关文章:

javascript - 以密码格式屏蔽信用卡号码的某些数字

html - 绝对位置在滚动条上没有固定位置

css - 用绝对位置和粘性页脚放在一边

html - 表格单元格内 block 元素的完全可点击区域和垂直对齐

javascript - 我可以使用 javascript/jQuery 更改媒体查询吗?

html - 带元素符号的一行中的无序列表项

css - 标题显示在 float 图像上方而不是右侧

css - table-cell、Firefox 和 Chrome 中的绝对定位

css - 'relative' 定位如何将工具提示放置在正确的位置?

javascript - 如何使用 css 样式安全地加载电子邮件内容