0

related to : How to detect if the OS is in dark mode in browsers?

Currently you can use stylesheets to determine if the user prefers dark mode or light mode using a media query.

For example:

/* Dark mode */
@media (prefers-color-scheme: dark) {
    body {
        background-color: #000;
        color: white;
    }
}

But, is there a way to do it server side in PHP so I can assign class (example: replace bootstrap's "bg-light" class with "bg-dark" when outputting the page) instead of trying to override the possible color classes for all cases

Kender
  • 1,191
  • 3
  • 15
  • 34
  • 1
    If you can pick it up in JS (https://stackoverflow.com/a/57795518/1213708) then you could pass this to your PHP code. – Nigel Ren Jan 20 '20 at 18:36
  • 1
    This question is far too broad for SO, as you're making no attempt to solve the problem yourself. This can't be done by PHP as it's on the server, and the browser does not provide this information in the HTTP request headers. As Nigel suggests, you can use JS to detect the colour of a specific page element styled with media queries, and then relay this back to the server using Ajax. You're better off just having a separate dark-mode stylesheet. – miken32 Jan 20 '20 at 18:58
  • This is answered in [stackoverflow.com/a/62024831/1575941](https://stackoverflow.com/a/62024831/1575941). –  May 30 '20 at 04:08

0 Answers0