-1

I have some variables in php which i want to use in my css, how would I go in using these in CSS ?

-webkit-clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);
-moz-clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);
-o-clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);
clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);

This doesnt seem to work, with or without the quotes around the .

2
  • use inline style with same css property to html, it will work. Commented Jul 21, 2015 at 4:36
  • Apparently I just removed the quotes and it works now Commented Jul 21, 2015 at 20:47

4 Answers 4

1

You can't render PHP inside of a .css file. You should use inline styles in your HTML that's generated by a PHP file.

That is the direct answer to your question. The indirect answer:

Use SASS or LESS. They're designed for doing exactly what you want here: using variables in your CSS.

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

Comments

0

You can do use it like this, inline in the HTML:

 <style>
-webkit-clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);
-moz-clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);
-o-clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);
clip-path: inset(0 1500px ("<?php echo $y; ?>") 0);
</style>

2 Comments

Opps, i forgot to mention that i have already included the style tags around it, however the values within my php variable $y (containing something like 500px string) doesnt seem to work in the css
@Nightflare11 You need to provide more data then. It's a PHP file or an HTML5? Can you post your code?
0

Rename your style.css to style.php and linking it with your html file like this

<link rel='stylesheet' type='text/css' href='css/style.php' />

At the top of your new style.php file set the Content-type back to CSS:

<?php
header("Content-type: text/css; charset: UTF-8");
?>

Now you are good to go... Set up and use your variables

Comments

0

You can :
1) Insert the value in the inline CSS directly (refer How do I dynamically insert a CSS class using PHP?)
2) Insert the value from PHP into CSS via jQuery (refer Add CSS class based on ID)

P.S. : Search for existing question-answers before you post

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.