php - 当在地址栏中输入路径时,浏览器会打开图像,但不会将其显示为背景图像。为什么?

标签 php html css url path

在此 SSCCE 中,我使用了两个 div,其中 background-image 的值指向同一图像,但使用不同的路径。图像未显示在第二个 div 中。

第二个 div 演示了我真实代码中的问题。 URL 的 C:/xampp/htdocs 部分是动态出现的,我使用 PHP 将其附加到路径的其余部分,URL 的其余部分始终保持不变。

如您所见,我在开发期间使用本地 XAMPP 服务器,但未显示此图像。

当我复制粘贴此路径时,即 C:/xampp/htdocs/Tests/Test44/index.png,在 Chrome 的地址栏中,它会将其更改为 file :///C:/xampp/htdocs/Tests/Test44/index.png,并显示图片。

我该怎么做才能解决这个问题?

标记:

<!DOCTYPE html>

<html>

<head>
    <title>Test</title>
    <meta charset="utf8" />
    <link type="text/css" rel="stylesheet" href="index.css" />
</head>

<body>

    <div class="the-div" style="background-image:url(index.png); background-repeat:no-repeat; background-position:center center;">Instagram</div>

    <div class="another" style="background-image:url(C:/xampp/htdocs/Tests/Test44/index.png); background-position:center center; background-repeat:no-repeat;">Twitter</div>

</body>

CSS:

body {
    background-color:black;
}

div {
    font: 0px/0 a;
    border: coral 5px solid;
    margin:5px;
    height:150px;
    width:150px;
}

enter image description here

最佳答案

如前所述,文件路径与 URL 不同。

只需删除“C:/xampp/htdocs”部分,背景图像就会显示出来。

Difference between absolute and relative URL

关于php - 当在地址栏中输入路径时,浏览器会打开图像,但不会将其显示为背景图像。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33647622/

相关文章:

javascript - 创建 jsFiddle/jsbin.com 样式代码编辑器的工具?

php - 为什么我的网站会出现两次 'loading'?

php - 如何从使用 PHP 和 MYSQL 的嵌套模型集中删除节点

c# - WordPress WooCommerce ASP.net API WebHookHandler : The WebHook request must contain an entity body formatted as HTML Form Data

html - 如何对齐 Pill 元素并总体上改进我的代码?

html - css pdf 页面 - 标题与内容重叠

css - 在 CSS 中,如何更有效地选择多个嵌套元素?

PHP 基础 - PHP 使用的 MySQL 密码存储在哪里

javascript - angularjs 在指令中绑定(bind)到 mouseenter 不起作用

html - CSS 根据屏幕大小更改内容