html - 如何使 HTML 背景图像的宽度扩展到父 DIV 之外

标签 html css position repeat

我有一个标题图片,我想将其水平放置,横跨网站顶部。宽度需要与浏览器窗口相同。

这与 StackOverflow 顶部的黑色条完全相同。

我的包装 DIV 具有固定宽度,因此我的图像仅针对 DIV 的宽度出现。我怎样才能让它成为浏览器窗口的全宽?

最佳答案

简短的回答是你不能。背景图像将始终包含在其父元素中。您可以尝试做的是不使用图像作为父元素的背景,而是将其放入它自己的元素中并将该元素带出容器。

例如:

<div class="container">
    <div class="headerImage">
        <ul class="navigation">
            <li><a href="">Link One</a></li>
            <li><a href="">Link Two</a></li>
        </ul>
    </div>
    <div class="otherContent"></div>
</div>

然后在你的 CSS 中:

.container {
    position: static; // This allows the image to break outside of it's container.
    margin: 0 auto;
    width: 960px;
}

.headerImage {
    background: url(yourImage.png) center top no-repeat transparent;
    display: block;
    position: absolute; // This breaks it out of the normal document flow.
    top: 0; left: 0;
    width: 100%; // This will expand to fill the width of the window.
    height: 65px; // Or whatever height you want.
    z-index: 10; // Play with this number for layering.
}

祝你的元素好运。

关于html - 如何使 HTML 背景图像的宽度扩展到父 DIV 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23946379/

相关文章:

jquery - 将固定的 div 向上滑动并离开屏幕

javascript - Google ReCAPTCHA 回调函数不工作

没有切割段落的css3列

c# - 如何在 Windows 窗体 C# 中伪造鼠标光标位置?

Javascript DOM - 将文本元素对齐到中心?

html - 无法在 Firefox 中滚动网站

css - li 元素内的 div 显示 block 使其有空白空间

javascript - 创建图像模态

html - Bootstrap 文本 nowrap 类没有响应

HTML/CSS : Common border around overlapping texts