douxin8383 2015-03-24 14:17
浏览 40
已采纳

使用PHP的随机CSS类/ id名称

I am generating random ID's for 2 divs, and I would like to use those randomly generated names in CSS too, as now I have to use inline styles.

Also, I don't want to use separate PHP file with CSS header to use that. Is there some other way to do it?

Lets say I want to have that padding set to randomly generated ID $id_1 in CSS and not as inline.

$id_1 = generate_function(rand(5,10));
$id_2 = generate_function(rand(5,10));

echo "<div id='".$id_1."' style='padding:20px 10px 0px 10px'>";

Thank you.

  • 写回答

2条回答 默认 最新

  • dougan4663 2015-03-24 14:21
    关注

    You could create a <style>...</style> somewhere on the page which you fill with the $id_1 and $id_2. Taking the variables with you to a CSS file is sadly not going to work.

    <style>
      .<?php echo $id_1; ?> {
        padding:20px 10px 0px 10px
      }
    
       .<?php echo $id_2; ?> {
        padding:20px 10px 0px 10px
      }
    </style>
    

    Note: I'm not sure if you can use PHP within style-tags! So you might need to echo the complete piece of code including style-tags.

    Another option is to give each of the divs another class and target that class with CSS instead of the randomly generated class.

    Could you let me know why you want a randomly generated class? It seems like a pretty weird decision, as classes are made so you can target them in your CSS and creating random classes will make it impossible to target them from an external CSS-file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析