[–] 3 points 9 months ago

As others have mentioned, the :has() approach is cleaner, but if you need to get this to work on older browsers (i.e. that don't support Baseline 2023 yet), you can use the ~ approach by moving the checkbox up to the top of body and putting all the body content into a div that follows it.

Basically, something like:

<body>
    <input style="display:none" type="checkbox" id="hidden-checkbox"></input>
    <div id="main-content">
        <label for="hidden-checkbox">
           ...

That way you can use #hidden-checkbox:checked ~ #main-content (followed by additional selectors) to modify children of #main-content only if the checkbox is checked.

Note that you still need a tiny bit of JS to make the choice persistent between page loads even with CSS checkbox toggle tricks.

  • source
  • [–] 2 points 9 months ago (3 children)

    Ah yeah, I guess, users would expect some action to happen when they click that toggle, not just for it to change from automatic-dark to manual-dark.

    Perhaps the simplest non-JS and non-persistent solution would then be to have it pick the color-scheme automatically by default, but if the checkbox is checked, then set the colors to the opposite.

    So, probably something like this:

    body {
      color-scheme: light dark;
    
      color: light-dark(black, white);
      background-color: light-dark(white, black);
    }
    @media (prefers-color-scheme: dark) {
      body:has(#theme-toggle:checked) {
        color-scheme: light; /*opposite*/
      }
    }
    @media (prefers-color-scheme: light) {
      body:has(#theme-toggle:checked) {
        color-scheme: dark; /*opposite*/
      }
    }
    

    You could probably even theme the checkbox to show a sun or a moon, depending on the current color scheme. 🙃

  • source
  • parent
  • context
  • [–] 2 points 9 months ago* (last edited 9 months ago) (4 children)

    Indeed you can, just like this (see below). I just tried it in a codepen and it seems to work. Although do note that if you put light dark as the default value, once you toggle the switch off again, it'll choose whatever mode the user agent wants, which might still be dark mode. So if you want it to be light by default, you'll need to use a value that enforces that.

    body {
      color: light-dark(black, white);
      background-color: light-dark(white, black);
    }
      
    body:has(#theme-toggle:checked) {
      color-scheme: dark;
    }
    
  • source
  • parent
  • context
  • [–] 2 points 9 months ago (5 children)

    I haven't tried it, but I'm guessing to implement a manual toggle with color-scheme, you could then do this:

    body {
        color-scheme: light dark;
    }
    body:has(#theme-toggle:checked) {
        color-scheme: dark;
    }
    
  • source
  • parent
  • context
  • [–] 2 points 9 months ago (6 children)

    Check this simple example out:

    body {
      color-scheme: light dark;
    }
    
    p {
      color: light-dark(black, white);
      background-color: light-dark(white, black);
    }
    

    You don't need any fancy selectors with this method. Will this not work in your browser due to the anti-fingerprinting feature?

    I will say, avoiding JavaScript completely will make the value reset after each reload, so might not be very useful perhaps, depending on your needs. You could store the value in localStorage, perhaps.

  • source
  • parent
  • context
  • [–] [S] 1 point 9 months ago (7 children)

    I got a piece of code from c/programming that works Using body:has(#theme-toggle:checked)

    I prefer doing it that way because I use a browser with anti-fingerprinting which hides the preferred theme so prefers-color-scheme doesn't work as well

  • source
  • parent
  • context
  • [–] 1 point 9 months ago* (last edited 9 months ago) (1 child)

    CSS variables yes, that's fine. But using this selector in this way, especially with body and html as the "subsequent sibling", won't work (well). The body and html elements can't be siblings of a checked element.

  • source
  • parent
  • context
  • [–] 3 points 9 months ago* (11 children)
    #theme-toggle:checked ~ body {
      background-color: #eff1f5;
      color: #fff;
    }
    
    #theme-toggle:checked ~ html {
      background-color: #eff1f5;
    }
    
    #theme-toggle:checked ~ .content {
      background-color: #eff1f5;
    }
    

    I don't think this will work. Read up on what the "subsequent sibling" selector really does and double check that it's what you really want here.

    Also check out the color-scheme property and the articles listed under the See also heading.

    https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/color-scheme#see_also

    Maybe that can give you some ideas where you need to brush up on modern dark mode styling. 👍

  • source
  • [–] 1 point 9 months ago* (last edited 9 months ago) (1 child)

    You can add a flex-direction: row to the hourly-forecast.

    Something else you can do is see if you have any extensions and try turning them off one by one to see if it's one of them causing it.

    Last you can try to get the devtools working and investigate with them why theyre not rendering as expected.

  • source
  • parent
  • context
  • [–] [S] 1 point 9 months ago (2 children)

    Thanks for the idea. Tried that, went through all of the related CSS lines. No changes in the display in FF.

    I also tried adding text inside the html elements where I expect to see the data displayed, like this:

    <div class="hourly-forecast">  
                    <div class="hour-column">  
                        <div class="hr-temperature">1</div>  
                        <div class="hr-sun"></div>  
                        <div class="hr-time"></div>  
                    </div>  
                    <div class="hour-column">  
                        <div class="hr-temperature">2</div>  
                        <div class="hr-sun"></div>  
                        <div class="hr-time"></div>  
                    </div>  
                    <div class="hour-column">  
                        <div class="hr-temperature">3</div>  
                        <div class="hr-sun"></div>  
                        <div class="hr-time"></div>  
                    </div>  
    </div>  
    

    That "1" (etc) does display, in a stack, one number below the next. No idea why though still.

  • source
  • parent
  • context
  • [–] 5 points 9 months ago (3 children)

    Find out which property is causing the issue by commenting them out one by one. Then once you know which property it is you can google for a workaround.

  • source
  • [–] 4 points 9 months ago

    XML is very good for documents and other structured content formats. The problem is people took XML and used that for things that aren't really documents. That led to a lot of people hating XML for the things it wasn't good at and thus hating XML even for the things it is good at.

    I wish we could find a reasonable middle ground. I'd love to have a job where I work a lot more with XML to be honest!

  • source
  • [–] 4 points 9 months ago

    It wasn't even just that. In the companies I worked, it seemed like nobody other than me understood how to escape things for XML. I couldn't even convince them to just use the right libraries/functions that auto-escape and parse things properly. They keep deciding they were smart enough to do it by hand. And it always ended up biting them.

    JSON, like Javascript (vs C++ or Java) is a lot more forgiving. It either works or it doesn't, and the values are just strings or numbers. And some people even do the numbers as strings.

  • source
  • parent
  • context
  • [–] 7 points 9 months ago (2 children)

    XML lost because it pretended to be human readable but it's actually a soup of punctuation, and pretended to be good for machines but is heavy to parse and transmit.

    If there was an explanation for why XML lost to the likes of Avro and Thrift I didn't spot it.

    I do think it's a shame that the only real alternative to JSON is the much more complex yaml, but I'll take it for most purposes.

  • source
  • view more: ‹ prev next ›