html - 为什么背景图片会破坏我的布局?

标签 html css

我正在尝试让我的背景图像带有一个分为顶部和底部的透明覆盖层。

昨晚,在 SO Chat 中,我试图为他们提供 JSFiddle,但在发布代码后,JSFiddle 无法正确重现布局。所以这里是预期的效果应该是这样的:

(请注意,这是手绘的,因此您看不到背景图片):

enter image description here

你可以看到页面应该水平分割。蓝色部分应为 50% 高,白色部分应为 50% 高。中间有一个标志。但是,当我添加背景图像时,白色部分被向下推,如下所示:

(请注意,您仍然看不到背景图片,因为它是手绘的):

enter image description here

将背景图像添加到 html 元素、body 元素 任何子容器都会导致白色 div 在其位置被截断顶部或向下推,在蓝色部分的底部边缘和白色部分的顶部边缘之间留下一个间隙

如何让我的背景图像停止影响文档的流动?我不认为 CSS 背景图片会影响布局?

这是我的代码:

<!doctype html>
<html lang="en-us">
    <head>
        <meta charset="utf-8" />

        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <title>Home | Hmmm</title>

        <link rel="stylesheet" href="~/Shared/Assets/Stylesheets/Core.css" />
        <link rel="stylesheet" href="~/Shared/Assets/Stylesheets/Home.css" />
    </head>
    <body>
        <header>
            <img id="key" src="~/Shared/Assets/Images/Icons/Kdfg.png" alt="Sign In | Create an Account" />
            <img id="logo" src="~/Shared/Assets/Images/Logos/JdfgWLSS.png" alt="Hmmm" />
        </header>
        <div id="main">
            <footer>
                <p style="margin-top: 100px; text-align: center; color: white;">&copy; Hmmm 2015</p>
            </footer>
        </div>
    </body>
</html>


* {
    margin: 0;
    padding: 0;
    outline: none;
    outline: 0;
    border: none;
    border: 0;

    font-family: 'Segoe UI Light', Tahoma, Geneva, Verdana, sans-serif;
}

html, body
{
    width: 100%;
    height: 100%;
    overflow: hidden;
}

body
{
    width: 100%;
    height: 100%;
    overflow: hidden;

    background-image: url('../Images/Backgrounds/JWSSB.jpg');
    background-repeat: no-repeat;
    background-size: cover;
}

header
{
    width: 100%;
    height: 50%;

    background-color: #2695D7;

    opacity: 0.8;
}

#main
{
    width: 100%;
    height: 50%;
    background-color: white;
    opacity: 0.8;
}

#key
{
    float: right;
}

#logo
{
    text-align: center;

    margin: 0 auto;

    position: absolute;
    right: calc(100% / 2 - 176px / 2);
    bottom: calc(100% / 2 - 100px / 2);
}

#sections
{
    width: 100%;
    height: auto;
}

.section
{
    width: calc(100% / 3);
    height: auto;
    float: left;
    text-align: center;
    font-size: 10pt;
}

最佳答案

我发现了一个解决方法。我不明白,但暂时没关系:

在白色部分的顶部添加边框:

#main
{
    height: 50%;
    background-color: white;
    opacity: 0.8;

    border-top: 0.1px solid white;
    z-index: -100;
}

然后,通过更改其 z-index 使 Logo 再次出现在顶部:

#logo
{
    text-align: center;

    margin: 0 auto;

    position: absolute;
    right: calc(100% / 2 - 176px / 2);
    bottom: calc(100% / 2 - 100px / 2);

    z-index: 1000;
}

关于html - 为什么背景图片会破坏我的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31738233/

相关文章:

html - 如何在表格的左上角和右上角放置一个按钮?

java - 如何在 Selenium WebDriver 中获取 "li"类的所有 "ul"元素

html - 具有动态宽度的固定背景渐变

javascript - 移动浏览器的自定义 css 动画中断

html - 背景图像:固定适用于除 Firefox 以外的任何地方

html - 多个 CSS 模态框

javascript - Angular 1.5.5 基于 $http 调用的返回值渲染 html

php - 在图像旁边放置文本

html - 你如何设置文本输入的样式,以便在不使用背景图像的情况下在左侧有一个图标?

html - 文本全宽背景没有css但在Wordpress中只有html