This course will be retired on March 24, 2020.
Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
What if you simply want to repeat rules, without having to duplicate them? This is where @Extends come into play.
You'll be working in Sassmeister for this course.
Like what you see and want to read more? Check out 'Sass in the Real World' for more on Advanced Sass!
-
0:00
Mix-ins are amazing, for getting dynamic results in repeatable CSS rules.
-
0:05
Functions are fantastic, for getting all that logic out of the way so
-
0:10
you can focus on the rules.
-
0:12
But, what about, when you simply want to repeat rules,
-
0:16
without having to duplicate them.
-
0:18
This is where selector inheritance, or extends, come into play.
-
0:23
Extending CSS rules has been around for a very, very long time.
-
0:28
This is not a new concept invented by Sass, but
-
0:30
one that was made much easier to accomplish with Sass.
-
0:34
But there are trap doors and potential pitfalls when using extends.
-
0:39
One of them being selector puke.
-
0:41
Let's explore, some of these and
-
0:43
talk about how to best use extends and avoid puking, all over your CSS.
-
0:49
>> Aside from functions and mix ins, the next big thing,
-
0:53
that's being used when you're trying to dry up your code.
-
0:56
Is the add extend directive or selector inheritance.
-
1:00
So, starting with this example that we have here.
-
1:03
So I have an h1 that's defined, I have an h2 that's defined and
-
1:06
then I also have a selector called .large-copy.
-
1:11
So in order for large-copy to take advantage of any of these things,
-
1:14
I could use @extend h1, right.
-
1:20
And we look at our compiled CSS to the right we see h1 and
-
1:22
large-copy share the exact same rules, and then h2 is still sitting here by itself.
-
1:27
'Kay, so now lets say that I wanted to do
-
1:30
something interesting like a text transform.
-
1:33
So I'm gonna put that text transform inside of my h1, so
-
1:39
[SOUND] text-transform and then, uppercase.
-
1:43
All right, perfect.
-
1:45
But I want that text-transform to be used by my h2, right?
-
1:50
So I'm gonna come down here and I'm gonna say @extend h1.
-
1:56
Okay.
-
1:56
Now we look at our output over here.
-
2:00
So we have h1 and h2, and large-copy all using the same three rules.
-
2:05
And then my h2, is using the font size.
-
2:08
And basically by using.
-
2:11
Inheritance in the cascade, so the h2 is being set to a 3.
-
2:15
3 em, but the h2 down here is being redefined to that two, 'kay?
-
2:20
So we can totally do this.
-
2:21
And this, this looks great.
-
2:22
I mean, what could possibly go wrong?
-
2:25
So, moving on with life, and we get further down into doing some stuff.
-
2:29
So I'm gonna create a selector called foo, right, and then.
-
2:34
This is gonna have a, you know, say a border of 1px solid red, right?
-
2:41
Give me a little bit more room here.
-
2:44
But then, and this is all looking great so far, so but then nested in here I'm gonna
-
2:48
have an h1, and then in this h1, I'm gonna say color white.
-
2:55
Okay, so, what's interesting to note, is that I have my h1,
-
3:00
h2, large-copy rules, my h2 rules, my foo roos, fool, foo rules.
-
3:06
Now I have this foo h1, foo h2,
-
3:09
foo and large-copy has all been turned to color white.
-
3:14
Why did that happen, 'kay?
-
3:17
And the reason this happened is because the way extends work.
-
3:20
I mean, extends is going to loop through everything and
-
3:23
it's gonna look for every possible match.
-
3:26
And it's basically because, you know, in this, over here in this example.
-
3:31
That, you know, I extended such a,
-
3:34
you know, a vanilla based HTML element, and we're gonna reuse this element again,
-
3:39
and again, and again to try to redefine styles throughout our designs.
-
3:43
So when I came in here, and did the h1 color white, Sass picked up on that with
-
3:48
the extend, and it looped through it and it thought it was an option that I wanted.
-
3:52
Hey, so this can really trip you up and this is one of the main reasons and
-
3:57
how people kinda end up with concepts like selector puke,
-
4:00
right Cuz all I did was foo h1, I wanted to be color white.
-
4:04
And then everything became color white and that's not what I wanted.
-
4:08
So, what I can do here to make this better and more you know.
-
4:13
More specific to the output that I'm looking for.
-
4:15
I wanna actually turn this h1 into what's called a placeholder selector.
-
4:21
Now a placeholder selector is much like a regular selector, except it's invisible,
-
4:25
until it's actually called into, another selector using the @extend directive.
-
4:31
So I'm gonna call this base-heading, 'kay?
-
4:36
So that's great.
-
4:37
Now what I need to do is I need to redefine my h1 again.
-
4:39
So I'm gonna say h1, and then I want @extend, base-heading.
-
4:48
Perfect.
-
4:50
So now I need to do the same thing down here again with this h2.
-
4:52
I don't wanna extend h1, I want to extend the base-heading.
-
4:58
Perfect.
-
4:59
Now, when I come down to here, extend, again, with the large-copy,
-
5:03
I don't wanna extend the h1, I want to extend the base-heading.
-
5:07
So, great.
-
5:08
So now if we looked at our output CSS here on the right, my h1, my h2 and
-
5:13
my large-copy all share those same rules,
-
5:15
of which I'm very specifically stating in the way this code is written.
-
5:19
My h2, it has its specific rules.
-
5:22
Foo has its specific rules.
-
5:24
And then now when we get down to foo h1,
-
5:27
since the h1 is not being the thing that's extended but it's actually the placeholder
-
5:31
that's been extended, the only rule being applied to foo h1 is color white.
-
5:38
So this is definitely a potential pitfall that you're gonna wanna look out for,
-
5:41
when you're working with extends.
You need to sign up for Treehouse in order to download course files.
Sign up