代码之家  ›  专栏  ›  技术社区  ›  Wasteland

通过css从php变量中的值设置元素的背景图像

  •  1
  • Wasteland  · 技术社区  · 6 年前

    有没有一种使用PHP变量来操作CSS的方法?

    我想把用户在WordPress帖子(特色图片)中选择的图片添加到特定分区的背景中。我知道如何混合使用PHP和HTML,但是,有没有一种方法可以使用CSS?

    我得到的图像是:

    $img = get_the_post_thumbnail_url($post_id, 'mySize');
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   brandizzi    6 年前

    <style> <head>

    <head>
        <style>
            div#myDivId { background-image: url("<%= $img %>"); }
        </style>
    </head>
    
        2
  •  0
  •   Mihail Minkov    6 年前

    <head>
      <style>
        .user img { background-position: center center; background-repeat: no-repeat; background-size: cover; }
      </style>
    </head>
    ...
    <div class="user">
      <img src="images/trans.png" width="50" height="50" style="background-image:url(<?php echo $img; ?>);" />
    </div>