css - 为什么我的 div 没有脱离父 div?

标签 css html

我正在尝试脱离父 div,以便让颜色 div 覆盖浏览器的宽度。

但是,出于某种原因,它将 block 推向左侧。

这是我的 site.

这是我的代码:

HTML:

<div class="aboutTop"></div>

CSS:

.aboutTop{
   width: 100%;
   height: 600px;
   background-color: black;
   margin-left: -100%;
   margin-right: -100%;
}

我哪里错了?

最佳答案

要使您的 div 从其父级“脱离”,您必须使用 position: relative;

HTML:

<div class="aboutTop">
    <div>break out!</div>
</div>​

CSS:

div
{
    width: 100px;
    height: 100px;
    border: 1px solid #000;
}

.aboutTop div
{
    position: relative;
    top: 50px; 
    left: 50px;
}

这是因为子元素被限制在它们父元素的边界内。使用定位将元素从文档流中移除。使用相对定位将其从流程中取出,但使用其在父级中的原始位置作为引用点。 Absolute 使用浏览器窗口的左上角作为引用。 :)

http://jsfiddle.net/qkU7F/

无论如何,宽度将始终引用父 div。所以你可以使用jQuery根据窗口宽度来设置元素的宽度。

var winWidth = window.innerWidth;

$('.aboutTop div').css("width", winWidth);

http://jsfiddle.net/qkU7F/3/

关于css - 为什么我的 div 没有脱离父 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12704714/

相关文章:

html - 在页面中心可见部分显示加载动画

javascript - 翻页响应式

jquery - 如何在 iframe 中设置 base64 pdf 的文件名?

jquery - 中心 3 div 制作幻灯片

css - rails : How to set width for a textarea in bootstrap 3 panel?

css - 自定义 Css 在 magento 2 中不起作用

css - IE8 是否有可能在 Windows 7 和 Windows XP 中呈现不同的 CSS

html - Safari 没有加载我的 styles.min.css

html - 如何选择放置在未知级别的元素

html - 在 Bootstrap 3 中垂直居中 div 元素