简体   繁体   English

背景图像 <div> 标签未显示

[英]background-image in <div>-tag not showing up

im outquestioned. 我不客气。 I just wanted to develop my own gallery, but then I already failed at the very start: Displaying an image as a background in a <div> . 我只想开发自己的画廊,但是一开始我已经失败了:在<div>中将图像显示为背景。 I have turned my code upside down, searched the web, searched stackoverflow, though I'm not able to solve the problem. 尽管我无法解决问题,但我已经将代码颠倒了,搜索了网络,搜索了stackoverflow。

Here's my code: 这是我的代码:

HTML/PHP HTML / PHP

<div class="imageholder" style="background-image:url(<?php echo $picture->thumbPath; ?>);"></div>

CSS CSS

.imageholder {
        width: 150px;
        height: 150px;
        float: left;
        background-size: fill;
        background-position: 50% 50%;
        background-repeat:no-repeat;
}

I'm iteration through an array of $picture-objects . 我遍历$picture-objects数组。 Each object has a thumbPath for its' thumb image. 每个对象都有一个用于其拇指图像的thumbPath the paths are correct. 路径是正确的。 checked them multiple times. 检查了他们多次。

Here's the output of Firebug: 这是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>

I already thought of the path having the wrong format, though I'm using the same file paths on an other project where everything works. 我已经想到了路径格式错误,尽管我在其他一切正常的项目上使用了相同的文件路径。 even displaying the same picture as a background-image of a div-tag ;) Hilarious, isn't it? 甚至显示与div-tagbackground-image相同的图片;)很有趣,不是吗?

Do you have any idea what's wrong with my code? 您知道我的代码有什么问题吗? Thanks! 谢谢!

It seems there are some space characters in your image path, Try to put your path in quote: 您的图像路径中似乎有一些空格字符,请尝试将路径用引号引起来:

url('./users/basti/img/20130825155015-tb-From Peth to Darwin/thumb/20130825155015-tb-DSC_0316_REZ.jpg');

As an alternative, you can convert the spaces to %20 , simply by str_replace() : 另外,您也可以通过str_replace()将空格转换为%20

background-image:url(<?php echo str_replace(' ', '%20', $picture->thumbPath) ?>);

( rawurlencode() is the way to encode URL, but in this case all the forward slashes will be converted.) rawurlencode()是对URL进行编码的方法,但是在这种情况下,所有正斜杠都将被转换。)

My path contained spaces. 我的路径中包含空格。 Just deleted them and it worked! 只需删除它们即可!

style=" background-image:url(<?php echo get_template_directory_uri(); ?>/image/project1.png);

Write this code before path <?php echo get_template_directory_uri(); ?>/ 在路径<?php echo get_template_directory_uri(); ?>/之前写这段代码<?php echo get_template_directory_uri(); ?>/ <?php echo get_template_directory_uri(); ?>/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM