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条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)