3

I want to control the CSS for a third-party website using PHP. Say I want to change font size of the website which I will take as input from the user. Can I do that?

5 Answers 5

2

You could have the entire .css file generated by PHP each time, but that's a bit of a waste of CPU. Instead, why not just take advantage of the cascading nature of css? Have your PHP output a small snippet of CSS into each page to override the font set in the .css file:

<head>
    <link rel="stylesheet" href="styles.css" type="text/css">
    <style type="text/css">
       #some_element {
          font-size: <?php echo $user_selected_font_size ?>px;
       }
    </style>

    etc...

That way you don't have to dynamically generate the entire .css each time and it can be cached by the client's browser, and you can still give them selectable font-sizes.

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

3 Comments

thank you for that,i already did this,but here wouldnt my css would be exposed if source viewed? then again, in each page I have to put this snippets of code.
If your css is so important that it can't be viewed, then don't use CSS... nothing stops someone from downloading your .css file to begin with. Wanting secret .css is like pushing a website but not wanting people to get your HTML - an absolute contradiction in terms.
Processing is cheap, compared to sending and receiving stuff. It actually makes good sense to process the whole CSS file with PHP each time.
2

You can parse and rewrite the current css file. Probably a better choice will be to load 2 CSS files in your main page, one will be the default one, and the other will be generated by PHP after user add his input. All the second CSS file will have instead are properties that have to be altered (for example font, or font instead of divs, etc).

Another solution will be to store in a DB users input and generate CSS code from that data in your file.php:

print "
    <style type=\"text/css\">
      body {
        color: purple;
        background-color: {$color} }
      </style>
";

while $color is an local variable created from the DB.

Comments

1

The preferred way to do this is to use JavaScript to modify the styles of certain elements on the fly. Look into jQuery for that.

Alternatively (and less preferred), you could make your CSS a PHP file (use a .php extension but include it like a .css file) and toss some variables in there.

2 Comments

I tried latter this one, but couldnt make it work properly,I kept getting a warning which I couldnt make go away
@xen_nsu Could you explain the exact problem you're having? Are you linking the stylesheet like this: <link rel="stylesheet" type="text/css" href="style.css.php">?
1

yes, you can create mystyle.php and link it as <link rel="stylesheet" type="text/css" href="[yourdomain]/mystyle.php">
inside mystyle.php you can do usual php stuff like this:

html, body{
    font-size: <?php mysql_... ... ?>
}

Furthermore you can add some mod_rewrite features to your .htaccess file in order to give 3-rd party site a possibility to access to your mystyle.php as mystyle.css (but this is of course an optional feature and everything will be ok even without .htaccess)

Comments

0

You can only do this if the external css is being served in some way by PHP.

I would recommend that the easiest approach might be to override the CSS definitions by either manipulating the DOM with Javascript or (even easier!) including a <style> tag inline within the page that you're rendering with PHP already.

2 Comments

Can you please tell me more about manipulating DOM using Javascript? Like where can i get tutorial on that?

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.