Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Sass lets you nest media queries directly inside the initial rules you're modifying. This keeps media queries local to the original selector, and makes writing and maintaining them easy.
-
0:00
All right, make sure you launch the Workspace with this video to get
-
0:03
all the latest files for this lesson.
-
0:05
In the previous video, I asked you to practice creating partials
-
0:08
by breaking mainstyles.scss out into related chunks of code.
-
0:13
Well, in the latest Workspace, you'll see how I organized and
-
0:16
sorted the partials into multiple directories for the base
-
0:22
component and layout styles, as well as a directory for
-
0:27
project utilities like variables, mixins, and proper placeholders.
-
0:32
And over in style.sass,
-
0:34
I imported each group of related partials using a separate import directive.
-
0:39
Feel free to pause the video and have a closer look at the partial
-
0:42
structure before moving on to media queries.
-
0:45
Media queries are an important part of web design in front-end web development
-
0:49
because they let you adapt the presentation of your content to a specific
-
0:53
range of devices and screen sizes without having to change the content itself.
-
0:58
Currently, our project uses media queries, just like you would with regular CSS.
-
1:04
Now, one way developers managed media queries in Sass is by creating a partial
-
1:08
in the layout directory for each media query break point.
-
1:11
And one of the benefits of doing this is that you write and
-
1:14
edit all your media query styles in one place.
-
1:17
Well, when you write media queries this way, you're also repeating selectors
-
1:22
by creating separate rules that refer to the same element.
-
1:25
Well, Sass's job is to help you avoid repeating code, right?
-
1:28
So, you can take advantage of features like nesting,
-
1:31
variables and mixins to make media queries easier to work with.
-
1:36
So instead of repeating selectors inside media query rules,
-
1:39
you nest media queries directly inside the initial rule.
-
1:43
And what this does is,
-
1:44
it keeps media queries local to the original selector you're modifying.
-
1:47
And it makes writing and maintaining them a breeze.
-
1:49
So, for example, in our components images partial, there's a media query that hides
-
1:56
the display of the featured image when a view port is 575 pixels or narrower.
-
2:02
And in the layout containers partial, there's a media query that centers
-
2:07
the main content container when the view port is 760 pixels or wider and below
-
2:12
that, a media query that sets the intro divs width to 45 percent, at 992 pixels.
-
2:18
So now, let's take full advantage of nesting
-
2:21
to keep all related styles within the same selector.
-
2:23
So first, go over to the components folder and open the file images.scss.
-
2:29
Then nest the media query that sets the images display to none directly
-
2:34
inside the image featured rule.
-
2:42
Now remove the image featured selector and curly braces inside the media query,
-
2:47
leaving the display none declaration only.
-
2:52
Next, open the file containers.
-
2:55
.scss inside the layout directory.
-
3:00
Then, at the top of the file, I'll go ahead and cut and
-
3:04
paste the media query that adjusts main-content's layout at 768 pixels,
-
3:10
directly inside the main-content rule.
-
3:14
Then once again delete the main content selector in curly braces inside
-
3:18
the media query.
-
3:25
And while we're working in this file, let's scroll down and
-
3:29
nest the media query for the intro container.
-
3:45
So, by nesting the media queries,
-
3:46
we're doing all the styling within the same selectors.
-
3:47
Some are just under a different context.
-
3:55
So now, when you save and compile the latest changes,
-
3:58
you'll see that Sass outputs each nested media query as a separate rule.
-
4:03
So they get bubbled up to the root level of the style sheet,
-
4:05
just like nested selectors do.
-
4:13
Now, if your project requires lots of media queries,
-
4:16
you can avoid repeating the same min width or max width values in your media query
-
4:21
expressions by creating variables for them.
-
4:25
So go ahead and open the variables partial inside the utilities folder, and
-
4:31
at the bottom of the file, let's declare a variable for each break point value.
-
4:35
So first, I'll write a comment for my break points, and
-
4:39
we'll start with the variable break dash xs, for the extra-small break point.
-
4:45
And we'll set the value to 575 pixels, right below,
-
4:50
we'll create the variable break dash s.
-
4:54
Set this value to 576 pixels.
-
4:56
Then we'll create break dash M for medium break points.
-
5:01
Set it to 768 pixels.
-
5:03
And finally,
-
5:07
break-l, which will be 992 pixels.
-
5:13
So by storing the values in variables,
-
5:16
we're able to configure all our media query break points in one place.
-
5:20
Now, let's include a variable in place of the repeated value.
-
5:23
So, we'll first replace the max-width value
-
5:27
of the image-featured rule with the variable break-xs.
-
5:34
Give that a save.
-
5:36
And then, back in my containers partial, I'll scroll up and
-
5:42
replace main content's break point value with break-m.
-
5:47
Then, right below, we place the intro rules min width value of 992 pixels,
-
5:53
with our break-l variable.
-
5:58
All right, so let's give this a save.
-
5:59
And have a look in the browser to make sure our layout still looks good.
-
6:07
And it looks like our layout is still adjusting to all break points as expected,
-
6:12
perfect.
-
6:18
So next, why don't you try nesting the remaining media queries for card and
-
6:23
header using the break point variables we just created?
-
6:27
You can see how I added the media queries when you download the project files with
-
6:32
this video.
-
6:32
In a later video, I'll teach you a more advanced and
-
6:35
efficient way to manage your media queries with mixins.
You need to sign up for Treehouse in order to download course files.
Sign up