简体   繁体   English

PHP echo中的背景图片网址

[英]Background-image url in php echo

So I am using bootstrap css with my PHP website. 所以我在我的PHP网站上使用bootstrap css For some reason I can get an img src to print just fine using the same location. 由于某种原因,我可以使用相同的位置获取一个img src来进行打印。 But when i need to use background-image the images do not show. 但是当我需要使用背景图像时,图像不会显示。 I cannot figure out why. 我不知道为什么。 Here is the section I am trying to get to show the standard bootstrap code. 这是我试图显示标准引导程序代码的部分。

//<!-- Wrapper for slides -->
      echo '<div class="carousel-inner">';
      echo '<div class="item active">';
      echo '<div class="fill" style="background-image:url("http://placehold.it/1900x1080&text=Slide One");"></div>';
      echo '<div class="carousel-caption">';
      echo '<h2>Caption 1</h2>';
      echo '</div>';
      echo '</div>';
      echo '<div class="item">';
      echo '<div class="fill" style="background-image:url("http://placehold.it/1900x1080&text=Slide Two");"></div>';
      echo '<div class="carousel-caption">';
      echo '<h2>Caption 2</h2>';
      echo '</div>';
      echo '</div>';
      echo '<div class="item">';
      echo '<div class="fill" style="background-image:url("http://placehold.it/1900x1080&text=Slide Three");"></div>';
      echo '<div class="carousel-caption">';
      echo '<h2>Caption 3</h2>';
      echo '</div>';
      echo '</div>';
      echo '</div>';

The fill css is this: 填充css是这样的:

header.carousel .fill {
    width: 100%;
    height: 100%;
    background-position: center;
    background-size: cover;
}

When I put it in a normal html file it works fine. 当我将其放在普通的html文件中时,它可以正常工作。 However, I am using php and autoloading, hence the need to use echo statements to print out the html parts. 但是,我正在使用php和自动加载功能,因此需要使用echo语句来打印出html部分。 I don't know why this is not displaying the images. 我不知道为什么这不显示图像。 Any ideas? 有任何想法吗?

Your quotes are conflicting. 您的报价有冲突。

Try echo '<div class="fill" style="background-image:url(\\'http://placehold.it/1900x1080&text=Slide One\\');"></div>'; 尝试echo '<div class="fill" style="background-image:url(\\'http://placehold.it/1900x1080&text=Slide One\\');"></div>';

CSS looks ok, must be a CORS issue. CSS看起来不错,必须是CORS问题。 Assuming you're using https on your site, your urls need to be https too. 假设您在网站上使用https,那么您的网址也必须是https。 Or you could just remove the protocol and let the urls start with "//", then it automatically load on whichever protocol your page is loaded. 或者,您可以只删除协议,然后让网址以“ //”开头,然后根据您加载页面的协议自动加载。

Also, use heredocs, that stack of echos is nasty. 另外,使用heredocs,回声栈令人讨厌。

echo <<<HTM
your
html
goes here
HTM;

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

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