【问题标题】:background image not displaying in html css背景图像未在html css中显示
【发布时间】:2016-12-28 21:47:40
【问题描述】:

这是我的css代码:

body
{
background-image:url("C:\xampp\htdocs\try\images\bg.jpg");
}

这是我的html代码:

<!DOCTYPE html>
<html>
<head> <title> HOPE Centre </title> </head>
<link rel="stylesheet" type="text/css" href="style1.css" media="all" />
<body>

<div id="wrapper">
<!--header-->
<header>
<div class="header">
<h1><img src="C:\xampp\htdocs\try\images\logo.png" alt="HOPE Centre" style="width:146px;height:83px></h1>
<nav>
<ul id="top_nav">
<li><a href="index.html">Home</a></li>
<li><a href="#">Sign up</a></li>
<li><a href="#">LogIn</a></li>
</ul>
</nav>
</div> <!--end of div header-->
</header>

<!--main navigation-->
<div class="nav">
<a href="index.php">Home</a>
<a href="#">About</a>
<a href="#">How to Help</a>
<a href="#">Contact</a>
</div> <!--end of div nav-->
</body>
</html>

标题中的图片也未显示。
上面的代码只是显示了一个白色背景。我正在使用 Google Chrome 版本 37.0.2062.103 m。 我应该如何解决这个问题?

【问题讨论】:

  • 检查图片是否真的存在于指定位置!
  • 你确定这是图片的正确路径吗?
  • 图片存在,路径也正确。

标签: html css image background


【解决方案1】:

试试

   body{background-image:url("../try/images/bg.jpg");}

   body{background-image:url("../../try/images/bg.jpg");}

在哪里

Start with "/" returns to the root directory and starts here
Start with "../" moves one directory backwards and starts here
Start with "../../" moves two directories backwards and starts here

【讨论】:

    【解决方案2】:

    这一直对我有用。祝你好运!

    body
    {
        background-image:url("file:///C:/xampp/htdocs/try/images/bg.jpg");
    }
    

    【讨论】:

      【解决方案3】:

      使用这个:

      body
      {
          background-image:url("images\bg.jpg");
      }
      

      【讨论】:

      • 这个答案可以通过解释更改并让 OP 知道只有少数有限情况使用以 C:\ 开头的 URL 是合适的。
      【解决方案4】:

      本地页面在加载本地 file:/// 资源时经常出现问题。使用http://localhost/try/images/bg.jpg,因为我看到您使用的是 xampp。

      【讨论】:

        【解决方案5】:

        尝试这样做.. 首先,粘贴 html 文件存储的图像文件,如文件名“html.jpeg”并检查您的图像格式,如 .jpg、.jpeg、.png 等

        类型格式与大写无关。

        body {
                background-image: url("html.jpeg");
        } 
        

        【讨论】:

          猜你喜欢
          • 2020-10-20
          • 2012-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多