【问题标题】:How do I set a background image in Chrome?如何在 Chrome 中设置背景图片?
【发布时间】:2014-07-04 15:16:21
【问题描述】:

如何使用可在 Chrome 中使用的 CSS 和 HTML 设置背景图像?以下代码在 IE 中有效,但在 Chrome 中无效?

body 
{
    background-repeat: no-repeat;
    background-color: transparent;
    background-image: URL("\Backgrounds\bg.jpg");
}

【问题讨论】:

  • :对我来说效果很好:JSFIDDLE

标签: html css internet-explorer google-chrome background-image


【解决方案1】:

此代码有效!

body {
        background: url("../img/img_bg.jpg") no-repeat center center fixed;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
        width: 100%;
        height: 100%;
}

【讨论】:

    【解决方案2】:

    将背景图像路径中的反斜杠替换为正斜杠

    所以代码是

    body 
    {
        background-repeat: no-repeat;
        background-color: transparent;
        background-image: URL("/Backgrounds/bg.jpg");
    }
    

    【讨论】:

      【解决方案3】:

      您在 URL 中使用了反斜杠而不是正斜杠。这对 URL 无效,因此 Chrome 不会找到它。但是,IE 与文件资源管理器相关(在某些版本的 Windows 中,它们是相同的),因此它会按照您的方式解释文件路径。使用格式正确的 URL 将确保图像文件可以在所有浏览器上找到。

      body 
         {
            background-repeat: no-repeat;
            background-color: transparent;
            background-image: URL("/Backgrounds/bg.jpg");
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-05
        • 2017-12-01
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2017-10-30
        • 1970-01-01
        相关资源
        最近更新 更多