dongra1984 2011-11-02 08:27
浏览 44
已采纳

在css中嵌入php

im not sure how to ask this so im just gonna say it. i have the following:

<style type="text/css">
 div.background
   {
   width:300px;
   height:120px;
   background:url(<?php
    echo '<img src="img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg" />'
    ?>) repeat;
   border:2px solid black;
   }
 div.transbox
   {
   width:200px;
   height:100px;
   margin:30px 50px;
   background-color:#ffffff;
   border:1px solid black;
   opacity:0.6;
   filter:alpha(opacity=60); /* For IE8 and earlier */
   }
 div.transbox p
   {
   margin:30px 40px;
   font-weight:bold;
   color:#000000;
   }
 </style>
 </head>

 <body>

 <div class="background">
 <div class="transbox">
 <p>This is some text that is placed in the transparent box.
 This is some text that is placed in the transparent box.
 This is some text that is placed in the transparent box.

 </p>
 </div>
 </div>

where:

<?php
        echo '<img src="img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg" />'
        ?>

it displays, or should i say, its supposed to display an image based to what a user has selected and some text on top of the image. Obviously it doesnt work. my question is, Can i call php from within css embedded in a php page?.... if not is there a workaround? Thank you for reading.

  • 写回答

4条回答 默认 最新

  • douchensou6495 2011-11-02 08:31
    关注

    It does not work not because of any PHP here, but mainly because of your syntax. You cannot inject HTML code into CSS:

    background:url(<img src="img/ships/ship_'.$user_ship['shipclass'].'_tn.jpg" />) repeat;
    

    It will never works. Valid syntax below:

    background:url('img/ships/ship_xxx_tn.jpg') repeat;
    

    However if you want to generate CSS document with PHP, you have to change file's extension to .php. Then server will regard it as PHP file and executes code:

    background:url('img/ships/ship_<?php echo $user_ship['shipclass']; ?>_tn.jpg') repeat;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看