Ultimate CSS Grid & Layout Techniques, v3

Changing Themes with Style Queries

Jen Kramer

Jen Kramer

Ultimate CSS Grid & Layout Techniques, v3

Check out a free preview of the full Ultimate CSS Grid & Layout Techniques, v3 course

The "Changing Themes with Style Queries" Lesson is part of the full, Ultimate CSS Grid & Layout Techniques, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Jen demonstrates container style queries by creating a dark mode toggle. When a checkbox is toggled, JavaScript code updates a CSS custom property. A container style query styles the page based on the value of the custom property.


Transcript from the "Changing Themes with Style Queries" Lesson

>> Now that you understand exactly how container queries are working in terms of size and so forth, let's take a look at how they work in terms of style. And what I would like to do now is I would like to set up a dark theme for my web page.

So you've probably done this kind of thing before where there's maybe a checkbox on the page and it's light theme by default, you check the checkbox and it turns into a dark theme. We are gonna do that as well, and we're gonna do it using container style queries.

So let's get started with that. We are going to start here with our example, as you see it here. And the first thing that I'm going to do is, as you take a look at our HTML up here on the top, we have our card-container and we have a series of cards afterwards, right?

Okay, so there's all of our cards. So far so good. Around these particular cards, what we're going to do, is we are going to add a div with a class of color-test, and we're doing this, of course, because you can't query the container. And I'm going to want to query my card-container here.

So I'm going to put in, color-test is gonna be the div that comes first. color-container is the div that comes second, and we'll close the div, of course, down here at the bottom. Alright, so that's the first thing to do. Inside of our div, we are going to add a checkbox.

So the way to do this with accessibility is to use your label element. So if you have never made accessible form elements, here's how you do it. We're gonna start with a label element. That's what I thought. We start with the label element, and we are going to give this an input type of checkbox.

And we're gonna give this an id of theme-box. And then I'm going to say, check for dark theme, Okay? And by putting the label element around your checkbox, you make the whole thing clickable. So I do not have to just hover over and check that tiny little box, I can also hover over the label and check It here as well, make sense?

And I kinda think I've put it in the wrong spot. I think I actually wanna put this checkbox inside of color-test. There we go. Or even outside of color-test. Let's put it outside of color-test. Let's put it just after the h1, Because that's prettier, okay? So so far we've got a checkbox here in place.

After that we have our div with a class of color-test, our div with a class of card-container, and then we have a whole bunch of cards, awesome. In JavaScript now, we're gonna add a little bit of code. So in the JavaScript panel, if you uncomment what's there, I will tell you all about it.

Okay, so in the JavaScript box, we are doing the following. First of all, we are gonna set up a couple of variables. I'm gonna call them checkbox, and I'm gonna call them color-test. First thing I'm gonna do is check the value of color box, and that is the id of theme-box that happens to correspond with the checkbox that I just made.

So we're gonna do a query on color-test, and I'm gonna show you what we're going to do with color-test here in a moment. We're gonna setup a custom property on that, as soon as I'm done going through this JavaScript. Then we're going to add an event listener. So we wanna listen for the checkbox, and we're just listening for change.

So we don't care if it's checked or it's not checked, we're just gonna listen for when it happens to change in time. When it does change, we're gonna run the color-theme function. And the color-theme function looks like this. So if the checkbox is already checked, okay, or if it's not checked, we're going to set it to false.

And if it is checked, we're gonna set it to true. Basically, we're just making a toggle here, that happens. And we're switching the value from false to true. You'll notice that we're testing for a thing called --dark-theme, yes? That is a CSS property. So we're actually using JavaScript to change the value of a CSS custom property, the CSS variable in this context.

So very, very cool way of using JavaScript. What I love about this particular piece of JavaScript is this is what JavaScript is for, everybody. This is not for styling. This is for, let's listen to the page. When things change on the page, let's make something happen. And it leaves all of the styling inside of our CSS.

So I am a very happy person today to write this particular JavaScript. And so when we go back into our CSS, what I'm going to do is the following. I am going to make these changes. I'm going to my color-container, we are going to set that variable --dark-theme, false, okay?

So this is the variable that we're gonna call down here in our JavaScript later, okay? That's what we're querying up here at the beginning. We're changing the value of it down here a little bit later. Make sense? Okay, and if you just do this watch so far and you put this in DevTools, you'll see that this is in fact working.

The box is changing from false to true in terms of its value, whether it's checked or not, okay? And then now to this, We will add our container style query. So @container style(--dark-theme) is true. So if it's true, what do we wanna do? We're gonna say .color-container {background-color: black; color: white}.

And now, when we check our checkbox, this should be working.
>> Is that what I did?
>> Yeah, it's supposed to be [INAUDIBLE].
>> And above.
>> Here we go. Okay, or color tests. So what I did was I put the two things together. That's what I did.

Okay, color-test. Thank you. So now it's gonna work. No, that's the container for the container query. So it is card-container.
>> Line two also color-container or card-container?
>> It is, I'm very consistent in my errors.
>> [LAUGH]
>> Okay, here we go. Now it's totally gonna work. There we go, okay?

And if we write even better HTML, we can get the whole page to do this, but I haven't written better HTML at the moment, okay? In my answer, I have it working on the whole page. Okay, so make sure that you actually write the names of your classes and the places you should write the names of your classes.

So this should be card-container is set to --dark-theme: false, and then inside of your container query, the card-container will be set to black and white for the display. That's what happened here at the end. And we can take a look at the finished version of this codepen and you'll see that we've got it working on the whole page.

Just a little bit of restructuring here of my HTML, okay? How cool is that? Okay, so we're actually using these variables here inside of CSS, something we created on the fly, this thing called dark-theme, which as we change the value with our checkbox, that's JavaScript monitoring that, then we can just load in a different set of colors here for the page.

So we go from black text and white background to the reverse. You don't always necessarily need JavaScript involved for this particular container query to work, you don't necessarily need to have custom properties that are equal to false and true. As I said, you could have, say, a custom color here.

My custom color of primary color has a value of red, then execute this particular style query. That would totally work, okay? That would be fine. You don't have to do things with false and true, but it was a really convenient thing to do in this particular example.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now