Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:05:59+00:00 2026-05-29T12:05:59+00:00

tl;dr – I’d like to know if it is possible to pass an object

  • 0

tl;dr – I’d like to know if it is possible to pass an object into a PHP file with CSS headers, such that I can use this object to manipulate various CSS attributes.

What I’m attempting to do, is allow my PHP/CSS file to interact with the other objects/php files in the webpage, e.g. menu item objects. My ultimate goal is to use PHP in the CSS file to count the number of menu items, and apply the appropriate width value in order to space them out evenly on the page.

I use a very simple color based example below to demonstrate my understanding so far…

I understand that for basic usage of PHP in a CSS file, one can do something like:

<?php header("Content-type: text/css");
$dkgreen = '#008400';
body {
 background:<?=$white?>;
}
?>

I also understand that OO PHP can be used to achieve a similar thing, e.g.:

class css {

    function __construct($args=array()) {
        foreach($args as $key => $field) {
            $this->{"$key"} = $args["$key"];
        }
        return $this;
    }
}

$css = new css(
    array(
        bgcolor => '#00FF00',
        fgcolor => '#000000',
    )
);

body {
  background: <?php echo $css->bgcolor; ?>;
  color: <?php echo $css->fgcolor; ?>;
}

Results of experimentation

1) OO style

I firstly attempted to make my css class create a singleton object for the CSS, which I tried to retrieve using $css = css::singleton(), along with the getCss() function, instead of $css = new css(...). The idea was that I wouldn’t simply initialise another css object which would be useless to me. Attempts to get the values for bgcolor and fgcolor using:

$css = css::singleton(); 
$css->getCss()->bgcolor;

were unsuccessful.

2) altering the href in the link tag à la style.php?bgcolor=00FF00&fgcolor=000000

This worked beatifully, when I could easily type $bgcolor = $_GET['bgcolor'];, but doesn’t seem to me an elegant solution.

Ideally, I’d like to retain an Object-Oriented approach, but if that’s not possible, I’ll happily settle for a POST approach, (i.e. allow me to use $bgcolor = $_POST['bgcolor'];) to avoid filling up the source code with ugly parameters in the link tag.

I’d also wish to avoid creating multiple .css files, if that is at all possible.

Any tips?

Many thanks,
Owen.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:06:01+00:00Added an answer on May 29, 2026 at 12:06 pm

    I don’t think it’s possible, and that doesn’t fit the purpose of CSS.

    Edit:
    Well basically, CSS is suppose to contain data that apply a style on a well defined structure. So CSS should not even have variables ( this is a big debate ). The “good theorical way” to solve your problem is to generate html code with proper id and classes, so that you don’t have to make any calculation using CSS: you only have to apply a style.

    Furthermore:
    CSS file are made to be cached. If they change all the time, you may have cache problem, or need to ask the file not to be cached. The you might need to generate inline CSS using PHP, but not a CSS file itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a PHP application that stores PHP datetime to my MySQL table. I
I am getting notice on line 30 that twetout is undefined variable <?php $username
I've got the following login script.. <?php $name = $_POST[name]; $password = $_POST[password]; $query
Have a webpage that will be viewed by mainly IE users, so CSS3 is
I have a simple restful service that transforms a JAXB-anntotated beans to response XML
i have this code: <?php $valid_ext = array(pdf, doc); $args = array( 'post_type' =>
i have a text file with the following data: Calculated Concentrations 30.55 73.48 298.25
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I am getting the value from web service like &gt;&lt;&amp;&nbsp; etc. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.