0

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.

4
  • Unless you can use some other method to target those divs in CSS, then no or define your styles inline, no. Commented Mar 24, 2015 at 14:18
  • @mituw16 Nope, I don't want them to be inline as they are now, I need it in CSS. So there is no other way of doing that? Well, damn. Thx for -1 tho.... Commented Mar 24, 2015 at 14:20
  • I didn't down vote you. I thought it was a legitimate question. I will however give you an upvote to get back to 0 :) Commented Mar 24, 2015 at 14:22
  • @mituw16 yeah that was a general thanks, not aimed at you, sorry if it looked that way. But thank you. Commented Mar 24, 2015 at 14:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hmmm, thats weird. I know you should not be able to use PHP in <style></style> but it actually works now. When did they change that?
If you are writing you your styles within a php file, then that should work just fine. That's what I mean by inline styles where you would have to define them in your php file, not some external styles sheet
@mituw16 Ah, I meant inline by definind it to div itself <div style="HERE"></div> Cool, then this works. I'll play with it a bit if it works exactly as I want. If yes, then I'll accept it as answer.
Ok, played with it and it works. Just add to answer that the <style> has to be inside the PHP file and an warning from me: CSS names CAN NOT start with number... I had to edit my random generator to always set first character to letter.
0

Are you looking for something like this?

<?php

    $id_1 = "id_".rand(5,10);

    echo $cascading = "<style>#".$id_1."{padding:20px 10px 0px 10px}</style>";

    echo "<div id='".$id_1."'></div>";

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.