I would like to create web page for my portfolio where a user could select a choice (e.g. radio button) and have that change what is in my CSS Style Sheet so that a particular element changes across the board.
The only immediate issue with JavaScript for the use of conditional styles would be if scripting were disabled. While the PHP (server-side) solution may seem like extra work where perhaps it could otherwise be done through active DOM manipulation, I wouldn’t want to push the client-side solution if it’s going to be used extensively and is required to have a set effect. In this case it’s physical features being made active, not just a degrading mechanism, IMO PHP is still the best way forward.
See, what you’re asking for is the CSS to change itself which isn’t possible because CSS isn’t a dynamic language, it’s static like HTML. When HTML changes dynamically it’s not due to the HTML, it’s due to the PHP altering the HTML. Therefore the only way to change the code would be to use scripting or PHP or something to make the dynamic changes as the code is rendered. Which is why you’d need to use scripting - to make the stuff happen as you need it.
I thought it was either Santa (pre-diabetes) or Darwin (looking particularly curious)
Yes it is possible… you would need to set no-caching to the CSS file (as you said) in htaccess (actually perhaps if the code changes it won’t fetch the cached version) and you would use something like this (you could do a style.php file)… http://fwebde.com/css/php-dynamic-css/
Instead of using $_GET which loads variables in the URL string, he could use $_POST which would keep the information clean from the URL and strictly on the server side
For something like this, you don’t even need jquery.
You could use just plain vanilla javascript and probably end up using a lot less code. The demo I posted earlier is fairly basic but it wouldn’t take much to expand that to do more.
Could you use PHP to “build” a CSS stylesheet on the fly?
What I was envisioning - forgetting fast - is an ability to be able to click check boxes or radio buttons in a control panel and then have it change the style of elements.
Not the same as having 3 stylesheets on hand, mind you, but a way to tweak things more.
Say I want to change all control from white to light blue, and make my background dark blue from white, and my text from black to white. I could click those element boxes and voilà, things change.
Next maybe I’d make the control yellow, text red, and leave the background white.
I just think that would be a good way to do a “website color scheme picker” as well as show people the power of CSS!
That’s an interesting caveat. In that case would a Javascript solution actually be better? That would manage the switching on the client-side in script so you wouldn’t have to worry about caching. It is at least another option. Alex has a good bead on the PHP solution. For a JS solution, I’d look at different jQuery options. Changing styles directly is one of the most basic jQuery options, and adding / removing classes is only slightly more complicated. Like the PHP, it would not be terribly difficult. For simple things like switching colors, I would do something like have a color picker (you can build your own, but there are also downloadable plug-ins in the UI libraries). When you pick a color, take the value and pass it to jQuery and change the color inline based on the input color. For more complicated transformations, define classes and use jQuery to add / remove the classes based on your application input.