我被击败了。我只是想开发自己的画廊,但一开始我就失败了:在 a 中显示图像作为背景<div>。我已经把我的代码颠倒了,搜索了网络,搜索了 stackoverflow,尽管我无法解决问题。
<div>
这是我的代码:
HTML/PHP
<div class="imageholder" style="background-image:url(<?php echo $picture->thumbPath; ?>);"></div>
CSS
.imageholder { width: 150px; height: 150px; float: left; background-size: fill; background-position: 50% 50%; background-repeat:no-repeat; }
我正在迭代数组$picture-objects。每个对象都有一个thumbPath其拇指图像。路径是正确的。检查了多次。
$picture-objects
thumbPath
这是 Firebug 的输出:
<div class="imageholder" style="background-image:url(./users/basti/img/20130825155015-tb-From Peth to Darwin/thumb/20130825155015-tb-DSC_0316_REZ.jpg);"></div>
我已经想到路径的格式错误,尽管我在其他一切正常的项目上使用相同的文件路径。甚至显示相同的图片background-image of a div-tag;) 很搞笑,不是吗?
background-image
div-tag
你知道我的代码有什么问题吗?谢谢!
您的图像路径中似乎有一些空格字符,请尝试将您的路径放在引号中:
url('./users/basti/img/20130825155015-tb-From Peth to Darwin/thumb/20130825155015-tb-DSC_0316_REZ.jpg');
作为替代方案,您可以将空格转换为%20,简单地通过str_replace() http://www.php.net/manual/en/function.str-replace.php:
%20
background-image:url(<?php echo str_replace(' ', '%20', $picture->thumbPath) ?>);
(rawurlencode() http://www.php.net/manual/en/function.rawurlencode.php是对 URL 进行编码的方式,但在这种情况下所有正斜杠将被转换。)