Viewers: in countries Watching now:
As I said in the beginning of this course, as you design and build a WordPress theme, you should always keep in mind that it can be viewed on many different screen sizes. In other words, you need to make sure that your theme is responsive mainly through media queries. Media queries are a type of CSS rules that turn on and off depending on things like, screen width. There are many different schools of thought when it comes to adding media queries to your style sheets. Some like to add them at the top or bottom of the stylesheet, others like to add them as separate style sheets all together.
I personally like to add them as close to the original styles as possible. This means your style sheet will have many media queries in different locations, but it also means it'll be easy to understand what each of the queries do, and what rules they relate to. If we look at our theme right now, you'll see it's kind of responsive when I resize the width of my window here. You'll see that the main content changes shape, and the sidebar stays the same width, which is exactly what I wanted to based on my design.
The problem is when I scale down far enough, you see that the sidebar is taking over all the space, and there's no space for the content. And when people visit your site, they're probably looking for the content and not what's in the sidebar. The reason why this is happening is because I broke my own rule. The rule that I told you in the very beginning of the course was, you should always design and develop for mobile first, and then accommodate for larger screens. But, if we look at the code in our current style sheet, you'll see that we're designing and developing for wide screens.
And then assuming that things would get fixed on smaller screens. So, I went about this the wrong way. What I need to do is take this first rule, the one that allows for space for the sidebar on the right hand side, and wrap it in a media query. So that it only kicks in when the screen is big enough to actually have the sidebar on the right-hand side. But before I can make that rule I need to know when the sidebar is going to kick in. So I need to search for the break point. I'll go back to my browser, and here I'm going to open the developer tools, so I'll hit Ctrl+Shift+i on my Windows computer.
You can also just right-click and inspect element or open it in another way. And when I do this, and I start resizing my window. You'll see up here in the right hand corner I'll have a little display that tells me how wide the window is. So what I need to do now is figure out exactly where I want the breakpoint to happen. So where I want the sidebar to disappear and fall down to the bottom of the page. And if I move this back and forth around this area. You'll see that there's a certain place where the contents starts reshaping, around 1160, 1170.
So I think that it makes most sense to place that break point at 1160 exactly. So that means when the screen is less then 1160 pixels wide. We should only see the main content, and the sidebar should be on the bottom. If the screen is wider than 1160, we should also see the sidebar on the right-hand side. So what I'm going to do is take my content area rule, cut it out, move down to the bottom of my style sheet, paste it back in again, and then I'm going to wrap it in a media query.
So here I'll start by saying @media. Because it's a media query, it targets screens. So I'll say screen, and, and then I'm going to save. This kicks in only when the screen is at 1160 or wider. So I'll say min width 1160 pixels. And then I need to place my rule set inside, then I'll highlight it all and hit tab to move it in. So now, this rule set that allows for space for the sidebar will only appear once the screen is above 1160 pixels wide.
I'll save my layout file and reload my page here, and you'll see that now that we have a small screen. There's no sidebar, and then, pop. The sidebar appears right at 1160, which is exactly what we want. But, if I scroll down, you'll see that on smaller screens, the sidebar looks really weird, because it's still 380 pixels wide, and is still jammed to the right-hand side. So, now that my first media quarry works, I have to also deal with the sidebar.
So I'll go back to my style sheet here. And I'll make another media query, screen and, only this time, I'll say, I want this media query to affect any time before the width of the screen is 1160. So I'll say, max width. So this is up until this width, 1159 pixels. So that means anything before 1160, whatever is in here will kick in. And what we need to do is take the widget area style. So I'll copy it, and I'll paste it in here, and shift it in.
And what we have to do here is make it so that on these smaller screens the width is the full width. So I'll set the width here to auto. I'll also set a max width of 780 pixels, and you'll see why in a second. And then I want to set the float to none. So that it doesn't float to the left or the right. Now I don't actually need to change padding and background because they're already defined in the main style so I'm going to leave that alone, because I don't want to repeat myself. But I am going to add some margin here, so I'll set the margin to top at 1M's and then I'll set left and right to auto, so that the sidebar will float in the middle.
And I'll set the bottom to zero. Now I can save this rule set, and reload my page and you'll see what happens. Now my sidebar has the same width as the main content, and it floats in the middle, as I resize the screen here. And when we get to a wide enough screen, it pops up to the side, and from here it's 380 pixels wide. Now there's two more things I want to do. If you look at the original design, you'll see that the content is floating to the right hand side.
And this is an important design feature in this particular theme because I want the content to always be in the center of the screen, or as close to center as possible. And you'll notice right now that the contents is currently centered within this content space, so it's not centered. So what I want to do is shift the content over to the right hand side, and then when the screen is smaller I want the content to be in the middle. So I'm going to make a small change to one of my rule sets. You'll see that right now this rule set here that covers all of the elements that are within the content, have margin set to auto.
Meaning that it will float in the middle of the container. So what I am going to do is copy out this rule set, and paste it in inside my min-width media query here, so that this kicks in when the screen is wide enough. And then I'm going to remove all the styles except for margin, and here I'll set margin right, to zero. So basically what happens is when the screen is big enough the content will have a margin right of zero, meaning it will jam against the sidebar.
It sounds subtle but check out what happens when I reload the page. Do you see how the content shifted? Well look what happens when I resize the window now. Do you see the content is floating to the right, and sticking to the sidebar, and then, when we get to the break point, it folds in the middle. This is actually an important part of the design. And it's not that apparent when you're working with a small screen like this, but if you were on a much larger screen you would see why this is important. Because, when you're on a much larger screen this sidebar is supposed to stick to the right hand side, and the content is supposed to float in the middle.
So if you go to the code snippets right now. You'll see there's a couple of changes to the media queries. And there's one extra media query that's been added. And those media queries apply to very large screens and what they do is, exactly what I've said. They place the sidebar on the far right hand side, and place the content in the middle. So if you have a larger screen, go grab the extra code snippets, and place them into your file and then, try to resize your window to a much larger screen. And you'll see what I'm talking about with the sidebar floating all the way to the right and the content in the middle.
It's really neat little feature. And it's important because a lot of people have very large screens and you want to utilize the space in as smart a way as possible. And putting the content right in the middle, is one way of doing that. Now that we have the layout we want, it's time to start working on the individual components of our theme.
There are currently no FAQs about WordPress: Building Themes from Scratch Using Underscores.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.