PHP cannot directly change a CSS value in a page that is already loaded in someone’s browser. PHP runs on the server, so it can decide which HTML, classes, or inline styles to send in its response. To change a style after the page loads—such as when someone clicks a button—use JavaScript. If PHP is validating a form, keep that server-side validation even when JavaScript provides faster feedback.
What PHP can and cannot change
When a browser requests a PHP page, the server runs the PHP code and sends the resulting response—usually HTML, along with references to CSS and JavaScript. The browser then builds and displays the page. Once that response has been sent, PHP is not running inside the browser and cannot reach into its live document to alter a CSS property.
PHP can still determine the page’s initial appearance. If a condition is known while rendering the response, PHP can include or omit an element, choose a class, or output an inline style. The browser applies the CSS to the HTML it receives.
Putting a <script> tag in PHP output does not make PHP execute browser behavior. PHP sends the script text in the response; the browser executes it after receiving the page. A PHP function call and a JavaScript function running in the browser are different things. The SitePoint discussion behind this question makes that distinction and recommends rendering the appropriate state when the condition is known at submission time: SitePoint Forums: “Changing css with javascript”.
#1 Best Overall
Choose the approach based on when the state is known
| Need | Use | What happens |
|---|---|---|
| The condition is known while PHP renders the response | Conditional HTML or a class/style attribute | PHP sends markup in the desired initial state; the browser displays it. |
| A page needs its own styling | A stable page identifier and ordinary CSS selectors | PHP outputs the identifier; CSS styles that page’s elements. |
| The change should happen after load, without a reload | JavaScript | Browser-side code changes a class or style in response to an event. |
Render a server-known condition with PHP
For example, after a form submission, PHP can determine that a field is invalid and render a message with a class that makes it visible:
<?php
// $hasError is set by server-side validation.
?>
<div class="message<?= $hasError ? ' message--visible' : '' ?>">
Please complete this field.
</div>
.message { visibility: hidden; }
.message--visible { visibility: visible; }
Here PHP selects the class while building the response; CSS controls what that class looks like. On a form submission that reloads the page, validate the submitted value in PHP and render the response with the relevant error message or class.
Rank #2
If a class depends on submitted data, validate that data and map it to known class names rather than inserting arbitrary input into a class, selector, or style fragment. A SitePoint forum example demonstrates conditional classes for selected inputs, but it is an illustration rather than a security specification: SitePoint Forums: “Change css of selected inputs when form submitted”.
Scope ordinary CSS to a particular page
When the goal is a different design on one page—not a change triggered by an interaction—have PHP output a stable page identifier and write normal CSS against it. For example, a body element with id="AboutUs" can be styled with a selector such as #AboutUs .header. The CSS remains ordinary CSS; PHP supplies the page identity in the HTML.
Recommended Free Tools
This avoids needing to run PHP inside a regular CSS file for a page-name use case. The approach is described in the related SitePoint discussion: SitePoint Forums: “PHP in CSS using $thisPage”.
Use JavaScript for changes after the page loads
If a style must respond immediately to a click or another browser event without a page reload, JavaScript can add or remove a CSS class. PHP will run again only if the browser makes another request to the server; a click by itself does not make server-side PHP execute.
Rank #4
<button type="button" id="show-message">Show message</button>
<div class="message" id="message">Please complete this field.</div>
<script>
document.getElementById('show-message').addEventListener('click', () => {
document.getElementById('message').classList.add('message--visible');
});
</script>
This example changes the browser’s presentation only. If the message reflects whether submitted data is valid, the server must still check that data when it receives the form.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Keep PHP validation authoritative
JavaScript validation can make a form feel more responsive by showing feedback before submission, but it should supplement server-side validation, not replace it. Browser-side code cannot be relied on to run for every request. PHP should enforce the rule when processing submitted data; JavaScript can improve the experience while the user is filling out the form. The SitePoint thread on this point likewise advises retaining PHP validation: SitePoint Forums: “Changing css with javascript”.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




