【问题标题】:Background image url is not showing the image背景图片网址未显示图片
【发布时间】:2014-07-18 03:18:40
【问题描述】:

我设计了一个 html css 主题。它不在任何服务器或本地主机上运行。我将主题(包括它的所有 css js 文件)保存在一个目录中。它的路径是D->网站->主题。

在 html 文件中,我使用了几张图片。我的图像 html 代码是

 `<img src="images/share.png" class="image-gap">`

此代码完美地在 html 页面上显示图像。我还需要显示背景图片,我使用了这个 html 代码

<div class="background-image-holder">
Only some text here. Background of this text should be a small image
<div>

上述html的CSS

.background-image-holder{
   background-image: url('images/share.png');
}

图像完美显示在 html 页面上,但相同的图像路径和图像不显示为背景图像。当我将鼠标悬停在 firebug css background-image 上时,它会显示此消息

Failed to load the given url

如何将此图像显示为背景图像。为什么我目前的方法不起作用?顺便说一句,我在这个网站上发现了一些问题,但答案对我不起作用。

【问题讨论】:

  • 它应该可以工作。小提琴here.
  • css url() 相对于 .css 文件的位置,而不是相对于加载该 css 的 html 文件。例如如果你的文件是/index.html,并且它正在加载/css/styles.css,那么背景图像将被搜索为/css/images/share.png
  • @MarcB 添加您的答案,我会接受。你拯救了这一天。谢谢

标签: html css background


【解决方案1】:

CSS url() 相对于 .css 文件的位置,而不是加载该 css 的 html 文件。例如如果你的文件是/index.html,并且它正在加载/css/styles.css,那么背景图像将被访问为/css/images/share.png

【讨论】:

  • 救命稻草。我在最后期限,但所有答案都是一样的,除了你解释了正确的事情:)
【解决方案2】:

我认为代码应该是这样的 -

.background-image-holder{
   background-image: url('../images/share.png');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2011-06-07
    • 2013-08-16
    • 1970-01-01
    • 2020-09-24
    • 2020-09-11
    相关资源
    最近更新 更多