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
CSS Selectors
62:31 with Estelle WeylEstelle Weyl is a freelance web developer, speaker, and internationally published author. In this talk, Estelle discusses some of the nuanced and lesser known aspects of CSS selectors. This includes CSS specificity and some of the more advanced pseudo class selectors that can be helpful when building complex websites and web apps.
-
0:00
[MUSIC]
-
0:04
So, now you get to start learning CSS.
-
0:08
Who here knows all the CSS selectors?
-
0:10
[LAUGH] Okay, someone yesterday said they, they knew CSS selectors.
-
0:16
I'm like, no you don't.
-
0:18
So, The, the slidedeck is up on my GitHub, and that's estelle.github.com/selectors.
-
0:26
So if you just go to estelle.github, it's actually .io.
-
0:30
My name's Estelle.
-
0:32
There aren't that many people under the age of 90 named Estelle.
-
0:37
There aren't that many Web developers under the age of 90.
-
0:39
So I got my own name, which is kind of cool.
-
0:42
But my GitHub page actually has about 20 tutorials on CSS and HTML5 and JavaScript.
-
0:48
It's all open source.
-
0:49
So feel free to, I keep talking into this thing and I really don't need to.
-
0:54
Feel free to go there and do all the
-
0:57
sessions and move this down here, okay, that's better.
-
1:01
So that's me, but you know who I am, the one thing I wanna point
-
1:05
out is this is my Twitter account, and I'm a Twitter whore, please follow me.
-
1:09
And other than that, that's it for marketing myself.
-
1:12
So what we are going to learn today is what goes here.
-
1:17
[BLANK_AUDIO]
-
1:21
Right?
-
1:21
[BLANK_AUDIO]
-
1:23
That's pretty, you guys, that looks familiar to everyone, right?
-
1:29
No one's lost?
-
1:30
Okay, good.
-
1:32
The thing is, it's not just what a selector
-
1:35
is but we also have pseudo-classes and pseudo elements.
-
1:39
And we're also going to learn attribute selectors, which you may or may not know.
-
1:43
And relational, well relational things.
-
1:48
Does everyone know what the tilde is?
-
1:52
Who?
-
1:53
Okay, I'll do the who knows cuz that's more paws than the who doesn't know.
-
1:55
Who knows what the tilde is?
-
1:57
Okay so you guys are gonna learn a lot today, cuz
-
1:59
you're just like ooh I have no clue what that is.
-
2:01
I don't know why my slide deck is not working.
-
2:04
Okay, so hopefully this slide is familiar to
-
2:08
everyone, we have IDs, classes, and tag names.
-
2:12
And that is about what, 99.8% of developers use and that's all
-
2:17
they use, is IDs which start with the pound sign, classes which start
-
2:22
with the period, or the tag name which you just put the
-
2:25
tag name with spaces in between if you want to do relational selectors.
-
2:29
That's what you guys already know so let's start working on stuff you.
-
2:36
There we go.
-
2:36
If you didn't know you could play with it.
-
2:38
We're not gonna play with it cuz you guys know this, right?
-
2:41
You guys know that if I put a, myID, this probably has an ID of myID.
-
2:47
Make sense?
-
2:48
Okay.
-
2:50
So, one thing I want to point out which has nothing to do
-
2:54
with CSS but has a lot to do with CSS is the selectors API.
-
3:00
So, you might be thinking that CSS is
-
3:02
really cool, I mean, selectors are really cool for
-
3:05
when you're doing CSS, but all the selectors we're
-
3:07
gonna learn today you can learn in your JavaScript.
-
3:09
Who here uses JQuery?
-
3:12
Almost everyone.
-
3:13
So you know how you've been using selectors
-
3:14
in your JQuery to, to target an element?
-
3:17
You don't need to pull in JQuery to do that.
-
3:20
It is native in CSS.
-
3:22
It's called query selector, and query selector all.
-
3:25
So, query selector finds the first one of whatever selector you put in here.
-
3:32
And you don't just have to put an ID selector.
-
3:34
You can put any selector that we're gonna talk about today
-
3:37
in there, and it will find the element that you're looking for.
-
3:39
But first I'd like to return the first one and query
-
3:43
selector with the all here capitalized will find all of them.
-
3:48
So return basically a node list for you, and it's a static node list.
-
3:53
It doesn't update.
-
3:53
If you update your page, like you, you do a var that's
-
3:57
supposed to, stands for child, children equals all new query selector all.
-
4:02
It will get that list now, but it will not update it if you update your dom.
-
4:06
So you have to get it again.
-
4:08
Unlike JQuery.
-
4:08
JQuery is a live node list and will update.
-
4:11
So that's query selector and query selector all.
-
4:13
So don't think of what we're learning today just
-
4:15
for your CSS, think of it also for your JavaScript.
-
4:18
So then we have relational selectors and combinators.
-
4:22
So we have ul li, and ol li, which is just a space.
-
4:27
It means li, er, any li that is either
-
4:31
a descendant of an unordered list or an ordered list.
-
4:34
It doesn't have to be the child.
-
4:36
It can be, you know, like, the great, great, great, great grandchild.
-
4:39
And as you see here when I click on ol li,
-
4:43
everything gets highlighted and this item gets highlighted twice, cuz it is
-
4:51
a child of both the ul or the ol and the ul.
-
4:54
So
-
4:57
it's hit twice.
-
4:57
If I put a, a greater than sign
-
5:00
in there, that is the direct parent/child relationship.
-
5:04
So that means it has to be an li that is the direct child of an ordered list.
-
5:09
And that is why remember when I hit this one right here?
-
5:15
I should probably highlight those on the left, so you can see it.
-
5:17
When I hit this one right here, the a, b, and c get highlighted twice.
-
5:21
And here they only get highlighted once because it's not matching, a, b,
-
5:25
and c because those are not direct
-
5:27
children, they, they're great, they're grandchildren basically.
-
5:31
Then we have the plus which is the
-
5:33
adjacent sibling selector and most people still know
-
5:36
this one because this was in IE6 and we've been using this for a long time.
-
5:41
Which is that, what happens is it basically finds
-
5:44
the element, which is number four, that has a class.
-
5:47
And then, the plus li means, find the element li that
-
5:51
has a class, and then find the li that's immediately after it.
-
5:55
If it doesn't come immediately after it, then don't look any further.
-
5:59
That's the only element I'm looking for.
-
6:01
If the element right after that li is not an li, stop looking.
-
6:07
Whereas the tilde, which is the one that
-
6:09
half the people knew is the general siblimg selector.
-
6:12
And that means find any li that comes after it.
-
6:16
So here we've targeted, li has a class which is number
-
6:19
four, and then tilde li says find all the adjacent sibling selectors.
-
6:24
So it has to have the same parent.
-
6:26
Once you leave the parent and go to the next li, it's going to stop finding them.
-
6:29
So it's basically a parental, you know, it has
-
6:32
to do with what it is a direct child of.
-
6:38
Okay.
-
6:39
I'm not gonna cover CSS 4, but we.
-
6:42
This is for when you want to come back here.
-
6:45
CSS 4 selectors API is not yet implemented so you don't need
-
6:49
to know it right now, but there's a lot of work being done
-
6:52
on it, and it's gonna be pretty cool because we'll be able to
-
6:55
do relations based on attributes and
-
6:57
stuff, and parent, parental relationships, as well.
-
7:01
Okay.
-
7:03
Well in case you were wondering how I did this previous slide, here?
-
7:07
I use the query selector and query selector all to make those changes.
-
7:11
All I did was change a class based on the on the current [INAUDIBLE] that was here.
-
7:17
Okay, so, that was the basic stuff.
-
7:20
Right?
-
7:20
You guys probably knew 90, percent, 90 percent of you inew 99% of that.
-
7:25
This is what we're gonna cover today.
-
7:28
Does all this look familiar to everyone?
-
7:30
[BLANK_AUDIO]
-
7:33
Okay then well then I can just go.
-
7:35
>> [LAUGH] >> So the browser support.
-
7:42
You'll notice that this is Safari 3.0.
-
7:46
Cuz that's the last time that Safari didn't support all of these.
-
7:50
3.0 is before the iPhone.
-
7:52
The iPhone was 3.1 in 2007.
-
7:54
Every other line is completely green.
-
7:58
With those three pretty little red lines, which is IE6, IE7, and IE8.
-
8:02
But notice IE9 is solidly green.
-
8:08
So this is on my, on my blog, and I just, I haven't updated this in years
-
8:12
because ever since IE9 came out, every single
-
8:15
browser supported everything that we're gonna talk about today.
-
8:19
So if you're supporting IE8 still, who here is still supporting IE6, anyone?
-
8:25
I'm so sorry.
-
8:26
>> [LAUGH]
-
8:26
>> But you know what, there's people that have job openings.
-
8:29
>> [LAUGH] >> Just saying.
-
8:37
Oh, I can't get out of this slide today.
-
8:40
Okay, so next is attribute selectors, and for a long time
-
8:43
since IE7, the attribute selector just as an attribute has been supported.
-
8:49
And it's basically any element in CSS can have an attribute.
-
8:54
the, an ID and a class, those are both attributes.
-
8:57
But like the image tag can have the alt
-
8:59
attribute and, in fact, it should have the alt attribute.
-
9:01
Never include an image without the alt attribute.
-
9:04
Your site is no longer accessible if, if you
-
9:06
that image is not accessible if you don't do that.
-
9:08
So here, I did image no space bracket, alt,
-
9:14
and it basically says target every image that has the alter attribute.
-
9:19
No matter what the value of that alt, alt attribute is.
-
9:22
So here we have an accessible image.
-
9:25
And here we have an inaccessible image.
-
9:28
And it matched the top one and not the
-
9:30
bottom one because the top one has the alt attribute.
-
9:33
And then here we have the form space with the type attribute.
-
9:37
So this is any descendant of a form
-
9:40
element that has a type attribute match that.
-
9:46
But it won't match a select, because select, it doesn't have a type attribute.
-
9:51
Oh, the black swan is because there were really cool black swans by the
-
9:53
lake, so we have a little swan swimming across the screen the whole time.
-
9:57
Just to keep those of you who are bored entertained.
-
10:00
Okay.
-
10:01
So IE7 limited support to this page
-
10:07
but IE8 and older understand these.
-
10:15
So we have an attribute that we have attribute with an exact value.
-
10:20
So, on the previous page we just did is the alt attribute present?
-
10:23
Here, it would be is the alt attribute
-
10:27
exactly equal to accessible and it would match this.
-
10:35
That's an exact value.
-
10:36
The quotes.
-
10:37
Does the quote, do the quotes need to be there?
-
10:38
The quotes don't need to be there unless you have a special character in there.
-
10:43
I just keep 'em in there all the time because
-
10:47
you never know what someone's gonna feed it into it.
-
10:52
Is it case sensitive?
-
10:53
It is only case sensitive if the attribute itself is case sensitive.
-
10:57
So if it's like an input type equals type equals
-
11:04
number, because that is HTML, HTML is not necessarily case sensitive.
-
11:11
Please make it all lower case.
-
11:13
But you're not required to make it lower case.
-
11:15
So if it is an actual value in HTML it
-
11:17
is not case sensitive because HTML itself is not case sensitive.
-
11:20
But if it's an ID or a class, or string that you made up, that is case sensitive.
-
11:26
If you're hitting JavaScript, you need to have it in the exact same case.
-
11:29
That, at that point, that actually be, needs to be case sensitive.
-
11:32
So that's how you figure out whether
-
11:34
it's case sensitive, but just do everything lowercase.
-
11:37
For all your attributes and life will be easy and everyone can read your code.
-
11:40
And if you do that, someone here can hire you.
-
11:44
I'm gonna make fun of you for the rest of the meeting, sorry.
-
11:46
[SOUND] Okay.
-
11:49
The pipe means, begins with.
-
11:55
It does?
-
11:56
No, it doesn't.
-
11:57
It means, begins with, followed by a dash, such as a language.
-
12:01
So it doesn't have to begin with, it can be completely equal to.
-
12:04
So like language equals en, language equals English, works fine.
-
12:09
But then you could also have, it
-
12:11
would also match language equals en-us, or en-uk.
-
12:15
So if you wanna have you know, like you're doing languages and
-
12:19
you wanna put the British flag to show that it's in English.
-
12:22
You can target lang lang equals en, or en-uk, or
-
12:27
even if it says en-us or en-uk, that's the pipe.
-
12:32
That's not used that much.
-
12:34
It could be used way more, but back when this was written,
-
12:38
that's, they weren't thinking about all the crazy things we're doing now.
-
12:42
The tilde equals is any space-separated word.
-
12:50
So if you have a sentence, like a title
-
12:52
attribute, is that word located in it at all?
-
12:55
If you have a plural of a word, it won't match it.
-
13:00
For that, if you actually want to do something like
-
13:03
that, you have the star equals, which is any substring.
-
13:08
It could be anywhere, including spaces.
-
13:10
So here we had, equals, pipe equals, and tilde equals.
-
13:15
Those are the older selector attributes.
-
13:17
And this is the CSS3 attribute selectors that
-
13:20
have been added, are the caret equals which is
-
13:23
starts with, the dollar sign equals which is ends
-
13:26
with, and the star equals which is any substring.
-
13:29
And if you know your regular expressions
-
13:30
they should be really, really familiar to you.
-
13:34
The swan is right behind what I want to talk about right now.
-
13:38
>> [LAUGH]
-
13:39
>> Which is that, you can put more than one attribute on a selector.
-
13:44
So, at the bottom, it says, a maybe if
-
13:47
I highlight it, yeah, you can see it that way.
-
13:49
No you can't.
-
13:50
I can really well, but you can't.
-
13:52
It says a, bracket, title, end bracket, bracket open again, href.
-
13:56
So you can put more than one attribute together to,
-
14:01
to be very specific but did everyone get one of these?
-
14:06
If not I have leftovers.
-
14:07
So this is a specificity sheet and the
-
14:10
thing with doing what's on the bottom which is
-
14:12
a title href, is that an attribute selector is
-
14:16
the same as a, weight as a class selector.
-
14:18
So that might be really useful, but
-
14:20
you have to realize that you're adding specificity.
-
14:22
So that would be a, a zero two one two for two class selectors, and one for the A.
-
14:30
The attribute selector, in most of the pseudo-classes that we're going
-
14:34
to talk about today, are all the same weight as a class.
-
14:39
Okay.
-
14:41
So, here just to recap, we have attribute selectors,
-
14:47
just simply is the attribute is that attribute present?
-
14:51
Is it an exact value?
-
14:54
Is it a space-separated word within there?
-
14:57
Is it a, a string followed by a dash, or no, or the actual value.
-
15:05
Does it start with, does it end with, or is it any string within there.
-
15:09
So, what I would like to do with these?
-
15:11
Let me just give you two examples because I didn't give you the examples before.
-
15:15
One of the things I like to do is, I
-
15:17
like to pull out the title attribute when I'm printing.
-
15:22
So I'll be printing something, and no one can
-
15:24
hover over it to see what the title is.
-
15:27
So with my abbreviations and my links, I like to
-
15:31
let them know cuz, I've been trying for years, but when
-
15:35
I print stuff up I just can't go to whatever page
-
15:37
I was, want to go to, it doesn't work that way.
-
15:39
So when I print, I'll put an ABBR title, colon after.
-
15:44
And then the content.
-
15:46
So when you're doing pseudo elements, the content attribute is required.
-
15:51
And here is a useful content attribute of a
-
15:57
property value rather, which is open and close parentheses.
-
16:02
And do you guys know that CSS can pull out stuff from your HTML?
-
16:06
So
-
16:08
in this case right here, and I'm gonna, should I walk over here?
-
16:11
I can walk over here.
-
16:15
>> And I can echo off.
-
16:16
No, I cannot walk over here.
-
16:18
Okay.
-
16:19
So, I'll just point to things on my computer.
-
16:20
I apologize.
-
16:21
So here, CSS can actually pull out the the value of your attributes.
-
16:28
So, you put attribute.
-
16:29
And you say which attribute you, you want.
-
16:31
And it could pull out that text.
-
16:33
So a lot of people, if you're using a dataset API,
-
16:37
you could actually pull out data dash whatever as the attribute.
-
16:41
So you can put tons of stuff in your HTML.
-
16:44
Don't overdo it.
-
16:46
Be logical.
-
16:47
But you can actually pull out stuff from your
-
16:49
HTML and make your CSS printed with generated content.
-
16:54
Okay, that was a side note.
-
16:57
And again I cannot move my slides.
-
16:58
There we go.
-
17:00
So, using attribute selectors.
-
17:02
The top one, it says, li.has a title, make it purple and bold.
-
17:08
Any link that has an href, put an over-line under, on it.
-
17:13
So, what can we do?
-
17:14
We can say title that starts with
-
17:22
unicorn.
-
17:22
And
-
17:24
you see the top one is, they're both purple and then I add an
-
17:27
s and then the bottom one did not start with unicorns, it started with unicorn.
-
17:32
Just to show the
-
17:36
the tilde, unicorn is an exact match.
-
17:40
Unicorns is not a space-separated word, there's an S at the end.
-
17:45
If I had done starts with, that would have been good, or if I had done also
-
17:51
just the star which means, you know, the sub-string anywhere that matches as well.
-
17:57
So let's look at the href as well.
-
18:00
If I do href starts with http that's kind of like an external link, right?
-
18:09
I could be more specific, I could do a
-
18:10
href starts with http:// Comma, a href, starts with https://.
-
18:19
but, unfortunately, you know, you can't make that into one.
-
18:23
So, to me, this is good enough cuz I never
-
18:25
name any of my folders http because that's just dumb.
-
18:30
Oh.
-
18:30
Did I say that?
-
18:32
Okay.
-
18:34
So doing that.
-
18:35
And then the other thing that's really cool is you can do endswith PDF.
-
18:41
And so what I like to use that with
-
18:43
is I'll put a background image, like an icon of
-
18:45
a PDF file, and put like padding of 20 pixels
-
18:48
on the right, and just put a little icon there.
-
18:51
You can even do it with generated content.
-
18:53
You know, you could say generated content, stick a foreground image
-
18:56
in, or preferably just put padding in a background image on it.
-
19:02
Any questions so far?
-
19:05
Okay.
-
19:08
So those are attribute selectors, and in CSS4,
-
19:11
not supported yet, we will have case insensitivity.
-
19:17
Okay, this is not in the CSS3 spec for selectors.
-
19:23
It's in the CSS3 UI specifications, but
-
19:26
they've put it into CSS4's selector specification.
-
19:29
And it's been supported for eons, it's just that
-
19:32
browsers weren't supporting forms that well, and now they are.
-
19:36
So we can use this now.
-
19:37
And this is one of those things where, just because IEs 8
-
19:42
and 9 don't support HTML5 web forms, doesn't mean you can't use this.
-
19:47
Use it, it's enhancement.
-
19:49
So here we have pseu, a few pseudo-classes, which is
-
19:52
enable, disable, checked, and indeterminate which is a level 4 CSS.
-
19:58
So checked would mean, is this box checked?
-
20:04
Can anyone try to read this to me, what this says?
-
20:07
Anyone wanna take a guess at what that reads?
-
20:12
You guys should all know this now, okay.
-
20:14
>> Select any checkbox input type that is checked,
-
20:19
will select the label that is directly after it checked.
-
20:24
>> Exactly.
-
20:25
It says dir, check the label that follows a checked
-
20:29
checkbox, not a checked radio button, but just a checked checkbox.
-
20:34
And make it red.
-
20:36
So if I check this
-
20:40
for those who are colorblind that just turned red.
-
20:45
And because I have a label attribute when I check it,
-
20:49
when I just click on it, it toggles on and off.
-
20:51
So include your labels when you do forms.
-
20:54
This is just progressive enhancement, it's like, if
-
20:57
it didn't change red, it's still readable, right?
-
21:01
So, there's no reason not to use this stuff.
-
21:05
So that's some of the cool stuff you can do with selectors.
-
21:08
And again, I can't move forward, there I can.
-
21:10
Okay, so, those aren't the only UI.
-
21:14
We have valid, Invalid, required optional, in range,
-
21:18
out of range, read only, read write, and default.
-
21:22
So default, no one knows what it means, I know what it means, so I'm gonna tell you.
-
21:26
Its not that exciting.
-
21:28
But it's so random, I still haven't figure out
-
21:31
how to use it, its just the default value.
-
21:33
So, if you started with 16 radio buttons and one of them was, by default,
-
21:38
checked, you could actually, even though that
-
21:40
is no longer checked, it's still the default.
-
21:46
And in the select, that works as well but you can't style a select so eh, okay.
-
21:51
So these two boxes right here, I basically
-
21:54
said, if it's valid, make the border green.
-
21:58
If it's invalid make the border red.
-
22:00
If it's required make the border five pixels wide.
-
22:04
If it's optional make the border 10 pixels wide.
-
22:07
If it's in range, if it's out of range make it pink.
-
22:09
If it's in range make it light green.
-
22:12
So here a number, and because some people don't know a HTML5.
-
22:17
I put what I I put the HTML below it which is input
-
22:21
type equals number, minimum equals zero step equals 0.1 and maximum equals ten.
-
22:27
So if I put the number six in here we're all good but
-
22:31
if I put the number 60 in here we are no longer good.
-
22:35
No reason not to use this.
-
22:37
No reason not to use the HTML5 form, forms either.
-
22:41
If you're in IE6, this will still show up as an input type, right?
-
22:46
And that's what you wanted.
-
22:47
However, if you are on an Android device or an iPhone or any other.
-
22:54
You know, a TV probably too.
-
22:56
And you have a popup keyboard, it'll show you the number keyboard.
-
23:00
That's an enhancement.
-
23:01
It doesn't take away from the, you know IE6 still works fine with this.
-
23:06
So use all of these.
-
23:09
They're enhancements.
-
23:12
And this is a,
-
23:14
showing up.
-
23:15
It's the exact same thing, but you can play with it.
-
23:16
So you can, you know, if you want to come back
-
23:19
to this, you can just, you know, make it blue or whatever.
-
23:23
Okay.
-
23:24
So these are the, the next I wanted to hit is the structural selectors.
-
23:28
Again, structural selectors have the same weight as a class.
-
23:33
And so did these UI pseudo-classes.
-
23:36
All have the same weight as a class.
-
23:39
So structural selectors are, when you have your HTML, and you
-
23:43
want to target an element, and you don't have access to
-
23:47
your HTML, you can pretty much target any element on the
-
23:49
page without a class or an ID by using the structural selectors.
-
23:54
So who here has done a table with class odd and even?
-
23:59
Don't do that ever again.
-
24:00
We're going to show you how to not do that.
-
24:04
Structural selectors, it's basically when you do odd and even and you
-
24:08
do a sort you have to re-add all the odds and evens.
-
24:12
With using structural selectors you don't have to
-
24:15
re-do anything because it just says, make the first
-
24:18
red, make the second white, make the third
-
24:20
red, make the fourth white, and just go on.
-
24:22
And when you change the order, it just automatically
-
24:25
resets it, cuz the first should always be red.
-
24:27
All the even should be red, or odds should be red, whatever.
-
24:30
So we have lots of structural selectors.
-
24:32
We have nth child nth last child.
-
24:34
You can all read.
-
24:36
We have few special ones like first child.
-
24:38
And first child has a little red star, cuz it's actually supported in IE8.
-
24:42
The other ones are started in IE9.
-
24:46
So I thought the best way to explain this is to start
-
24:48
with first child, last child, first of type, last of type, only child
-
24:54
only of type.
-
24:55
I prefer to do it by example.
-
24:58
So, I'm gonna actually open this in a new window.
-
25:03
And then.
-
25:04
It showed up.
-
25:05
Okay.
-
25:06
So this is a beautiful page.
-
25:07
I spent many, many hours doing the CSS for this.
-
25:11
And what it does is it makes everything blue.
-
25:15
And, if I do last of type.
-
25:19
It's all blue.
-
25:24
If I do first of type, it's all blue.
-
25:27
Anyone know why?
-
25:28
[CROSSTALK] It's a body tag.
-
25:33
So the body tag is the first of it's type and it's the last type of it's type.
-
25:37
But it is not the first child.
-
25:40
So the first child doesn't target everything, it
-
25:43
just targets the first child of any parent.
-
25:47
Because I didn't specify anything here, right?
-
25:49
So its targeting those grey boxes are just divs, and then inside it
-
25:55
says whether it's a paragraph or an h1 or an h2 or h3.
-
25:58
Just assume everything that's not an h1, h2, or h3 is actually a paragraph.
-
26:02
So target the first div so everything inherited the color.
-
26:06
Here this is the second div but I targeted the first child right?
-
26:11
So this, this div is the first child of the body.
-
26:14
This H1 is the first child of the div and if we
-
26:17
were to go down the page it would hit every first one.
-
26:23
If I did first of type, let me just put a body in there.
-
26:32
So I'm talking about children, you know, anything in the page.
-
26:37
This h2 got highlighted.
-
26:39
Why did this h2 get highlighted, when it wasn't highlighted before?
-
26:42
Because before it was first child.
-
26:45
And now it's first of type.
-
26:46
I
-
26:49
could do last of type.
-
26:53
And here it did the last paragraph, not the first paragraph.
-
26:58
And which is often similar to the last child.
-
27:01
But notice the h1, cuz I'm actually doing of type, rather.
-
27:05
I mean I'm doing child rather than of type here.
-
27:09
So, now I just need to make sure that I hit everything I want to hit.
-
27:20
Oh, only child.
-
27:22
I forgot
-
27:24
that.
-
27:24
M'kay, so, if I do only child, there's only
-
27:29
one thing that is gonna get hit and that's
-
27:32
the last one cuz the, that h1 that is
-
27:35
on the bottom, that hopefully you people can read.
-
27:37
Wow, that's way bluer on your screen than it is on mine.
-
27:40
I actually have this really pretty color blue over here.
-
27:41
You guys have regular blue colors over on the screen.
-
27:45
That is the only element that is the only child of
-
27:47
its parent, but had I done only of type, much more
-
27:52
would hit because this one has only one h1 but it
-
27:56
has more than one paragraph, it has a few paragraphs, right?
-
28:00
In this box here there is only one h1.
-
28:03
In this box here, there's an h, only one h1, only one h2, and only one h3.
-
28:06
But, there's several paragraphs.
-
28:08
And that is why.
-
28:09
Do you guys wanna know how I did this?
-
28:12
Like all the JavaScript that I used for this page?
-
28:16
All I did was I declared the head as, as visible.
-
28:21
And then, I hid everything in the head, except for the
-
28:23
last style sheet, and then, I made the style sheet content editable.
-
28:27
And so, I'm editing the actual style.
-
28:31
If you do a view source.
-
28:34
I like to go off-topic cuz going off-topic is fun.
-
28:38
If you do the view page source, this is what I'm editing on the fly right
-
28:43
there oh, right there, right that's what the
-
28:47
page looked like when I first loaded the page.
-
28:49
And so I'm just, it says content editable,
-
28:51
which is an attribute supported ever since IE5.
-
28:55
Use it, it's fun.
-
28:57
And it changes, it updates the dom and then we go back to normal size.
-
29:02
Okay.
-
29:06
Okay.
-
29:07
We're good.
-
29:07
People understand of type versus child?
-
29:11
Any questions?
-
29:12
Yes.
-
29:13
>> Is there any way currently using CSS to select the
-
29:16
first class of what's on the page and not the first element?
-
29:21
You know what I'm saying?
-
29:22
>> I'm gonna try answering that question by
-
29:24
actually typing it in here which would be.
-
29:27
[BLANK_AUDIO]
-
29:37
You could do that.
-
29:38
>> Okay.
-
29:38
>> So, the question was is there anyway to target the first class.
-
29:41
And this would be find the first element with a class selector, right?
-
29:48
And this wouldn't actually work for the whole
-
29:52
page, this would just work based on its parent.
-
29:53
>> Uh-huh.
-
29:55
>> So, so far, like there's no way to target throughout the whole page.
-
30:00
The difference between the JQuery first, and the first child is
-
30:06
in CSS, the first child is the first child of any parent.
-
30:09
And the first colon in JQuery I believe is actually the first one on the page.
-
30:14
It stops looking after that.
-
30:15
So it, it does, it doesn't have that parental relationship if I.
-
30:19
Don't, like, don't quote me on that.
-
30:20
But, like, when you realize that you have a bug.
-
30:22
I'm not a JQuery person.
-
30:23
I love JQuery, I think it's awesome, but I prefer to hand code.
-
30:26
So I don't actually use, JQuery, if I can avoid it.
-
30:31
But that might be confusing for people when they hit JQuery.
-
30:34
Okay, so then, we get a little bit more complicated.
-
30:37
But a little bit more powerful which is we can create our own equation, right?
-
30:41
So far we just had first and last and only.
-
30:44
Right?
-
30:44
That's kinda limiting.
-
30:46
You can say I just want the 27th.
-
30:48
I want every third one, I want every third one but I want to start with number eight.
-
30:54
You can do all of that with CSS.
-
30:57
Because we have the nth of type, or nth child, pseudo-classes.
-
31:02
And also last of, so you can start from the bottom instead of from the top.
-
31:06
So 3n, which would be every third.
-
31:10
Odd, anyone know what that means?
-
31:13
Just kidding.
-
31:14
So we have two keywords odd and even, and then we
-
31:17
have this equation called which is the equation an plus b.
-
31:24
B is the offset
-
31:29
a is the, not multiplier, what is the word for that.
-
31:32
>> Coefficient.
-
31:34
>> Coefficient maybe, we don't know we don't
-
31:37
remember math so we're just going to believe him.
-
31:39
But, you know, it's a multiplier basically.
-
31:44
So you can put any number there.
-
31:46
If you put 5n+4 it'll be, start with the fourth and
-
31:50
then the ninth, and then the fourteenth, and then the nineteenth.
-
31:53
I [UNKNOWN] pick five cuz that's easy to add.
-
31:56
Okay, So here's an example.
-
31:59
We have first child and last child, and they're bold.
-
32:03
So, first child and last child will be the first and last one.
-
32:05
And so we see that item number 1 and item 10 are both bold.
-
32:09
And then, we have first of type, last of
-
32:11
type, which happen in this case because these are li's.
-
32:14
And therefore, they're the only elements inside a ul, or, in this case yeah a ul.
-
32:21
They're both have a line through them.
-
32:24
Then we have every even one has a background color of grey.
-
32:28
Check.
-
32:29
Two, four, six, eight, ten, they're all background grey.
-
32:32
Oops, I said I won't move my sweatshirt, sorry about that.
-
32:36
odd.
-
32:37
Are white, I don't think I actually put that in the
-
32:41
CSS cuz I had the white background, but maybe I did.
-
32:42
I don't know.
-
32:44
Then three.
-
32:46
I only put the offset, so a is basically equal to zero, right?
-
32:52
So 270 times 0 is what?
-
32:56
Zero.
-
32:57
I have second grader she's learning this in math.
-
33:01
It's really hard you know?
-
33:03
So, it will only hit third one.
-
33:05
So, here we look the third one, the color is grey and hard to read.
-
33:10
And we have four n, and it's that blue that to you, to me
-
33:13
looks really pretty and to you guys looks like the regular blue link color.
-
33:17
So it's every fourth one.
-
33:19
And if you see item 4 and item 8 are blue.
-
33:24
And then we have nth of type 3n minus 1.
-
33:28
So what the heck does that mean?
-
33:30
So we first make n zero.
-
33:34
The value is negative one.
-
33:35
So it doesn't matter.
-
33:36
That's, that's not shown.
-
33:38
Then we make n1 three times 1 minus 1 is 2.
-
33:41
Then we make n2 3 times 2 minus 1 is 5.
-
33:46
And now I'm getting into algebra and I'm confusing
-
33:49
everyone but what that really means that number 2, 5,
-
33:52
8, and 11 would be right aligned and as we see here 2, 5, and 8 are right aligned.
-
33:57
Is this making sense to everyone?
-
33:59
The equation?
-
33:59
You're all Good?
-
34:01
Okay.
-
34:03
So that leads to this which is now possible in Flexbox but
-
34:06
this was really cool like five months ago before Flexbox was everywhere.
-
34:12
Which is one of the things we could never figure
-
34:14
out in just CSS was how many rows do we have?
-
34:19
Or how many elements do we have?
-
34:21
And if you have one, make it 100% wide.
-
34:23
If you have two, make it 50% wide.
-
34:25
If you have three, make it 33% wide.
-
34:26
If you have four, you make it 25% wide.
-
34:29
Totally doable in CSS before Flexbox, right?
-
34:33
Except for IE8 and 7 and 6.
-
34:36
So how do you do it?
-
34:37
You do li only of type, making the width 100%.
-
34:41
And the top one is 100% wide, right?
-
34:44
Then we do li nth of type 1, which means the
-
34:48
first one from the top, which is also the last of
-
34:52
its type, number two, which is the second from the bottom,
-
34:55
which means there's exactly two elements, then make the width 50%.
-
34:58
And then you do the other one, which is nth of type two,
-
35:02
which is the second from the top and the first one from the bottom.
-
35:04
So it has to match both of those things, which means there's
-
35:06
only two elements and there's the second one, make that width 50%.
-
35:10
I'm gonna skip the third one cuz we don't have all day.
-
35:13
But the fourth one does the exact same thing.
-
35:15
The fourth one says, if it's the first, and also the
-
35:17
fourth from the end, which means there's exactly four items, right?
-
35:21
Make it 25%.
-
35:25
Kinda cool.
-
35:28
But now we have Flexbox, so.
-
35:29
But I thought this was really neat when, you know?
-
35:32
That's one way to basically target, it gets a little bit
-
35:37
unwieldy after five elements cuz, you know, you have to write five.
-
35:43
You know, basically it's one, two, three, four
-
35:47
I know there is an expression for that when you add one and then
-
35:50
you add two and then you add three and I for, what is it?
-
35:54
>> Factorial?
-
35:58
>> Factorial, perhaps.
-
36:00
Everyone agree with him?
-
36:02
I'm really old, I took calculus in 1984.
-
36:05
Half of you weren't born.
-
36:09
Right?
-
36:10
Yeah, okay.
-
36:10
I don't remember anything.
-
36:13
So then we have structural selectors.
-
36:16
No, it was 1985.
-
36:17
No, that was 1984.
-
36:18
Okay.
-
36:19
I'm so old, I don't even remember, that's so cool.
-
36:22
Okay, so how do we play with this?
-
36:24
So, I'm gonna play with this up here, cuz
-
36:26
not all of you have computers, and those of you
-
36:28
who do have computers might not have much internet
-
36:30
access, though the WiFi's doing really well here, so kudos.
-
36:33
So, here, what I did, is I said, this is just a table and it has cells in it.
-
36:37
Right.
-
36:37
Woo hoo!
-
36:38
So exciting.
-
36:40
You shouldn't be designing your web pages like this, but it's just to prove a point.
-
36:44
So here I did nth of type even, tr nth of type even.
-
36:47
Which is every even row, every even cell in that row
-
36:51
make it, every odd row, every odd cell make it red.
-
36:55
And that's what happened here, right?
-
36:57
And make the text white, but I
-
37:03
could also make it more stripey,
-
37:10
3n, 3n, 3n plus 1, 3n
-
37:16
plus 1, and then 2, 2.
-
37:21
See kind of cool?
-
37:23
So this is just to say you can really figure
-
37:25
out what you want to do and target any element.
-
37:27
And if I wanted to skip the first row, I could do this probably.
-
37:35
no, it would be.
-
37:36
[BLANK_AUDIO]
-
37:44
Okay, this is why I don't shift my code, and we were talking about this earlier.
-
37:49
It would be, okay, I'm just not gonna do it.
-
37:53
Okay.
-
37:54
But it's doable, just my brain is just not an option today.
-
37:58
So when it decides to load, cuz I, I complimented the WiFi, right?
-
38:03
You guys remember me complimenting the WiFi.
-
38:05
This page is not loading.
-
38:08
And that's why I have this.
-
38:09
Everyone who has epilepsy, please close your eyes.
-
38:11
[SOUND] There we go.
-
38:19
It's called a local copy.
-
38:21
Okay.
-
38:22
So here's the U.S. flag done with selectors.
-
38:28
If you look at the code, it's the exact same as the page before.
-
38:34
Do you want proof of that?
-
38:37
View page source.
-
38:40
It's just a table with cells.
-
38:41
The word cell in it.
-
38:43
Table, cells.
-
38:46
Don't show this code to your, to your boss cuz it's bad.
-
38:49
Okay, but what I did is, I said, and this is the hard version.
-
38:52
Cuz I did an easier version and I did a hard version.
-
38:54
This was just to show the power of selectors cuz
-
38:56
it's way, there's a much easier way to do this.
-
38:58
But I said, make the whole thing white.
-
39:00
Just every td and every background color, just make the whole thing white.
-
39:03
Get rid of that text so no one sees it.
-
39:05
And then I said tr nth of type odd, which meant every odd row, right?
-
39:10
Find the eighth cell.
-
39:17
And then, tilde.
-
39:17
What does tilde mean everyone?
-
39:20
Adjacent sibling, find the td that comes after it.
-
39:25
All the tds that come after it.
-
39:26
So what that part did, and and then make those red.
-
39:30
And then start with td, t, start with the row 2n plus 9.
-
39:36
So start with the ninth
-
39:40
row and everyone there after so 9, 11 and 13, make every td red.
-
39:45
And so here I have found the eight one, eight cell
-
39:48
was right here and then I made it red, red afterwards.
-
39:51
I could have just made the whole thing red like every other
-
39:54
row but that wouldn't have been fun, that would've been too easy.
-
39:57
So then I want to do the blue.
-
39:58
And the blue I'd said nth last of type, type n plus 7.
-
40:03
So go to the, start at the end, go seven in, and then make those blue.
-
40:09
And then I did the stars which is the exact same
-
40:13
thing as, you know, this right here and this right here.
-
40:19
Say the exact same thing right?
-
40:21
Generate content, so after put a little star.
-
40:24
In terms of Unicode character the star is 2605 and that's just the content.
-
40:30
When you do CSS and you want to do Unicode you can declare UTF8 and actually just
-
40:36
put the star in there or you can put the slash and put 2605 which is the code.
-
40:41
The slash is basically, you know how you put
-
40:45
the ampersand in HTML, and slash u in, in JavaScript.
-
40:49
You just put the slash in CSS, and then I moved every odd
-
40:54
one over, every odd star over by 14 pixels to make them offset.
-
41:00
So, if anyone was paying attention, this is actually
-
41:02
not the American flag, cuz there are not fifty stars.
-
41:04
There's like forty-eight, but it's good enough for this demo.
-
41:08
Okay, so let's close out that window.
-
41:10
Oh, it loaded.
-
41:11
Okay so, the internet is working, just takes a little bit longer.
-
41:15
Or, yeah, okay.
-
41:17
And this, if you want to take a look at it, just a simpler version.
-
41:21
In CSS level four we're gonna have more stuff, we're gonna have matches, selector.
-
41:27
But we're not there yet so I'm not gonna cover that.
-
41:29
But if you wanna come back to this when your
-
41:31
browser, when browsers actually support this I can show an example.
-
41:34
And it, there will be an example in here at that time.
-
41:37
But right now it's not supported.
-
41:38
So not.
-
41:39
Not is one of my favorite things.
-
41:41
Which is, match all of this stuff and then don't match these things.
-
41:45
So it first pulls out all the divs here.
-
41:51
[SOUND] Sorry.
-
41:58
It first matches all the divs, and then it,
-
42:01
it takes out the ones that say, exclude me.
-
42:03
So it's not like it goes through, so if you were gonna do nth of type
-
42:06
2, like, or odd, it would first find
-
42:09
all the odd ones, and then exclude the exclusions.
-
42:12
So div, so it's e colon not parentheses, and a simple selector in there.
-
42:19
So by simply selector most of the stuff that we've
-
42:24
covered, like all of those nth last of type.
-
42:29
2n plus 7, that's a simple selector.
-
42:34
What they mean by simple selector is no relational.
-
42:37
So you can't put like child of something, or, you know [UNKNOWN] descendant
-
42:43
relationship but any pseudo-class or attribute
-
42:46
selector or ID or whatever is fine.
-
42:49
In terms of specificity this is an exception.
-
42:53
So remember when I said every pseudo-class has the
-
42:56
same weight as a class The not has no weight.
-
43:00
Okay, I'm going to move this because it's just not working.
-
43:03
>> Flip it up so that the [INAUDIBLE]
-
43:06
>> I should not wear very a light cotton t-shirt when I talk.
-
43:09
That's mainly the issue.
-
43:10
Okay, I'm gonna put it back on my sweatshirt.
-
43:17
And try not to move, I'm just gonna like, pretend
-
43:21
I'm Bob Dole and not move my left arm, okay.
-
43:24
That was so wrong, take that out of the video.
-
43:29
Okay, so the not in terms of the specificity
-
43:33
has no weight, but the selector inside it does.
-
43:36
So if you put an ID selector in there you're talking about the weight of an ID.
-
43:39
You can put an ID, there's nothing wrong with doing that.
-
43:41
Well there is, when you're using selectors try to be as,
-
43:45
as non specific as possible cuz that makes it easier to overwrite.
-
43:49
Who here uses Sass?
-
43:53
Next year everyone's hands up, okay?
-
43:56
The only thing is when you're using Sass you can nest 2,712 times.
-
44:00
It will let you do that.
-
44:02
Don't.
-
44:04
Limit to about three or four or two.
-
44:08
Don't keep nesting because your specificity just
-
44:11
goes crazy and then you can't fix anything.
-
44:14
So, the weight of, of the exclusion counts not the not, not the not, okay.
-
44:24
Oh, and then, in CSS4, we'll actually be able to
-
44:28
put a comma in there, and say not a few things.
-
44:32
Okay.
-
44:36
And we don't have this yet, so that's also CSS4.
-
44:38
Then there's the empty pseudo-class.
-
44:42
Anyone here ever use empty?
-
44:48
Yes, what'd you use Empty for?
-
44:49
>> A slider in [INAUDIBLE] >> Okay, so the slide he just did
-
44:57
styles the sections differently whether it's empty, whether it has content.
-
45:00
The, which is similar the only thing which I've
-
45:02
ever used it for, which is WordPress, p empty display
-
45:07
none because I, the less I hate having, like
-
45:09
it just creates like 44 empty ps for some reason.
-
45:16
So it should match
-
45:20
anything that touches, has a comment or that is actually empty and that's it.
-
45:24
In CSS4 I think that it will actually match
-
45:27
if it just has white space, but I'm not sure.
-
45:32
Oh, blank, yes, exactly, okay.
-
45:35
So in CSS4 we'll have blank.
-
45:37
Okay, can we have the links to the classes.
-
45:40
Link visited, anyone need help on this?
-
45:43
Okay, one thing to note with this is that it
-
45:47
used to support everything and when Safari 5 came out
-
45:50
or maybe it was 5.1, I think it was 5,
-
45:51
it didn't support it completely and I was like, what?
-
45:55
Oh my god, they broke the build.
-
45:57
And no it's a security feature.
-
46:00
People could basically scrape and see what styles
-
46:03
they could apply to your links and then
-
46:05
say, oh, you've visited Bank of America and
-
46:07
I grabbed your you know, password just now.
-
46:09
So let me check if that's the same password.
-
46:12
So link and visited don't support all of you know CSS like other things do.
-
46:18
And CSS 4 will have any-link, local-and local link(n) which will let us,
-
46:24
you know, which will let you, based on the hierarchy in the page,
-
46:29
whether it's local or not local, and whether it's, in this current directory,
-
46:34
or like 15 directories down you'll be able to target based on that.
-
46:43
Oh, that's CSS4, so I'm not doing that.
-
46:45
Then we have hover, active, and focus.
-
46:47
Anyone need help with that?
-
46:49
No.
-
46:50
Okay.
-
46:50
One thing to notice, which is focus and active.
-
46:53
Don't remove the little dots around your elements.
-
46:58
Your designers, you know, they'll l say, I hate those little dots that are coming up.
-
47:01
You can remove the dots but replace them
-
47:03
with something else because some people use keyboards.
-
47:08
And when I ke, key through a page, I wanna know what link I'm on, you know?
-
47:16
That's my lecture for today.
-
47:18
And CSS4 will also have drag and drop pseudo-classes.
-
47:21
These are not yet supported.
-
47:24
Then, there's the other pseudo-class which is target.
-
47:26
And we're gonna have scope.
-
47:28
And I just, I passed through this page real fast,
-
47:31
because I'm going to talk about it on this page.
-
47:33
This is Chris Coyer's example, target is awesome.
-
47:38
Let me open in the new tab, so I can actually show you right there.
-
47:43
[BLANK_AUDIO]
-
47:48
Let me open this in this page, so it goes faster.
-
47:51
[BLANK_AUDIO]
-
47:57
Okay.
-
47:58
One more page, and open this.
-
48:01
[BLANK_AUDIO]
-
48:09
Okay, here we are.
-
48:11
This I'm supposed to open and control.
-
48:14
I'm gonna open a new window because obviously this is just failing me.
-
48:17
Okay, so here if you see it says files 07 target.
-
48:21
That is the name, right?
-
48:22
There's nothing after here.
-
48:25
And as you see here the tabs are all gray.
-
48:28
So what does target mean?
-
48:29
Target means when you have the pound sign and tab
-
48:33
three or something like that an anchor link on the page.
-
48:36
So here it says section not the target if the direct child a, you know.
-
48:42
If, if your a is a direct child of a section, but not the
-
48:47
section that is the current target, make the background color C CCC, or grey.
-
48:52
If your a is a direct child of a section that is
-
48:55
the target, make the background color white, and the border bottom white.
-
49:00
And then if your div is a direct child of, of a section
-
49:06
that is not the target, give it a z index of negative two.
-
49:09
But if your div is a direct child of the section that is the target,
-
49:13
give it a z index of negative one, so put it higher, closer to you.
-
49:17
When I click on this tab here, notice that turned white and text changed right?
-
49:22
And notice this one right up here, oop, of course it hid what I wanted to show you.
-
49:25
Tab two, right?
-
49:27
That's the target of the page.
-
49:28
[BLANK_AUDIO]
-
49:32
There's no JavaScript on this page, you can do what you just imported a
-
49:35
huge JavaScript library, well it's actually kind
-
49:37
of small, but it's still a JavaScript library.
-
49:39
And if you're importing on a mobile device,
-
49:41
right, it has to download it, might have it
-
49:44
cached, but even if it has it cached it still has to par, parse it and execute it.
-
49:48
Right?
-
49:49
So don't add a framework just to do stuff you
-
49:53
can do, like, odd and even, or first use CSS.
-
49:58
Oops, there we go.
-
50:02
Okay, so that's target.
-
50:06
Then we have the language pseudo-class.
-
50:08
And we're going to basically, language, and you can put whatever language.
-
50:12
And then CSS Selectors 4 we'll be able to be
-
50:14
more specific with language and sub-languages and direction as well.
-
50:19
Right now you can use the direction
-
50:20
attribute but it's just basically on that element.
-
50:24
So, you can say does this element have the dir attribute?
-
50:28
In CSS4 you'll be able to say does this
-
50:30
page, does this paragraph inherit from some other thing?
-
50:33
So is this left-to-right or right-to-left?
-
50:35
So that's gonna be fairly cool for internass, internationalization.
-
50:40
So root in HTML, colon root, is the H, usually the HTML element.
-
50:48
So why do we have this?
-
50:50
One, it's a great way to hack against IE8, IE7, and IE6, cuz you can just put it in.
-
50:55
But that does add specificity.
-
50:57
But the main reason that I use it is, that is where I declare my
-
51:01
rem units, or my font size for the page that I can use my rem units.
-
51:07
Do people know what a rem unit is?
-
51:09
Anyone not know?
-
51:11
Give me quizzical looks or something.
-
51:14
Okay, who knows what a rem unit is?
-
51:16
Not everyone, okay.
-
51:17
So, a rem unit is a a root em unit, or a relative em unit.
-
51:23
It's, it's, it's relative to the root, em size.
-
51:25
So, you know how when you do em 0.8.
-
51:28
And then you accidentally nest a 0.8 inside a 0.8.
-
51:32
You've got 0.64.
-
51:35
And that just gets smaller and smaller until you can't see anything any more.
-
51:39
Or bigger and bigger, if you did 1.2 and 1.2.
-
51:41
I can't do math up.
-
51:42
I can only do it down.
-
51:45
You've got a problem.
-
51:47
So, rems instead of staying relative to the thing
-
51:50
that it inherited from, it inherits just from root.
-
51:54
So if you declare 16 pixels on root, which is the default for most browsers then,
-
52:00
when you say one rem, it'll be 16 pixels.
-
52:05
If you say 0.75, it will be 12 pixels.
-
52:11
And then if someone increases the font of
-
52:12
their page, everything increases at the same rate.
-
52:15
So you could actually control it much better than an em unit.
-
52:20
So it's the way to go.
-
52:21
And it's been supported since IE9, I believe.
-
52:27
The other use for root is, in the future we're going to have variables in CSS.
-
52:31
We actually have a lot of browsers already supporting that.
-
52:32
You would declare the variables on the root.
-
52:35
And I can't go too deep into that because
-
52:37
I haven't actually played with calc in a long time.
-
52:42
Or variables.
-
52:43
Okay.
-
52:43
So, specificity I kinda covered it but just let me cover a few more things.
-
52:48
It's specifishity in this case but the picture
-
52:52
says specificity cuz that's a really really old picture.
-
52:57
Or no, actually, my website is broken with that.
-
53:01
so, specificity, what I want to point out is the less obvious on the sheet.
-
53:05
You can put this on your desk and there's
-
53:08
two things to note about this, well, a few things.
-
53:11
The star, I always thought the star was
-
53:14
like an element, it actually has no value whatsoever.
-
53:16
I didn't, like, when I was doing this sheet, that's when I discovered that.
-
53:21
No matter how many elements you have, text selectors,
-
53:24
it will always be less weight than a single class.
-
53:27
Those greater than, the tilde, and the plus has no added weight.
-
53:31
It might make your selector more specific but it
-
53:34
doesn't add to the specificity in terms of weight.
-
53:38
[SOUND] I already said that all of the
-
53:44
attribute selectors and pseudo-classes are the same as a class.
-
53:50
And what I didn't say is that I was a little bit political.
-
53:57
So I used plankton, which is kinda non political, and fish and shark.
-
54:01
But then I used a BP oil tanker for style.
-
54:04
I could have used any boat, cuz a boat can kinda kill a shark, right?
-
54:08
But the BP oil tanker's just a, it doesn't have good memories for us.
-
54:12
Don't use style.
-
54:14
Style is for your JavaScript.
-
54:16
And you know what, you shouldn't even be putting style in with your JavaScript,
-
54:18
what you really should be doing is
-
54:19
changing classes with your, with your JavaScript.
-
54:23
So, the only time you should have style in the inspector is when the inspector puts
-
54:27
it in there because you've added it with
-
54:29
JavaScript, but even then try doing class change instead.
-
54:32
And then important, I just put a bomb, the nuclear bomb,
-
54:35
cuz we all think nuclear bombs are really good ideas, right?
-
54:38
No we don't.
-
54:39
So important I use it.
-
54:41
I do use important.
-
54:42
I use it when I am completely screwed and I can't figure out why I can't target an
-
54:48
element you know cuz I put 16 divs IDs on it and 14 classes
-
54:55
and just nothing and then I realized that I was styling an li It looked like an
-
55:01
a, because someone had styled all my lis to look like as or something like that.
-
55:05
Or my spans cross-link.
-
55:07
And looked to me like an a.
-
55:08
And I'm, like, why can't I make this a red?
-
55:10
Because it wasn't an a.
-
55:11
So use, use the elements for what they were intended.
-
55:14
If it's a link, use a.
-
55:17
If it's not a link, don't use a.
-
55:20
If it's not a link, don't make it look like a link.
-
55:26
I got off track again.
-
55:28
So important help, helps me find out that I'm actually not targeting, I should
-
55:32
be targeting an li instead of an a, or a span instead of an a.
-
55:36
And then I take it out, I do not put it into production.
-
55:40
And the person at
-
55:44
at iFreelance, at a company and, they had a lot
-
55:47
of importants, and it was, the guy was out the door.
-
55:50
He's like, I don't care.
-
55:50
And I'm like but I do, be nice to me, you like me, don't you?
-
55:55
So I told him I would have fired him if he wasn't quitting.
-
55:58
I don't have the power to fire him, but I told him I would have fired him anyway.
-
56:01
So, combinators like tilde, greater than, and plus, have no value.
-
56:05
Not, I already went into this, has no value, but the parameter does.
-
56:10
And specificity is not inheritance.
-
56:12
Just because something is very specific doesn't
-
56:14
mean that the child necessarily inherits it.
-
56:18
There's inheritance rules.
-
56:19
I mean, you know, like when you inherit from your
-
56:22
parent sometimes, but other times, on some properties you don't.
-
56:26
Like background image, the nested elements
-
56:28
don't inherit from its background image, cuz
-
56:30
then you'd have a background image nesting,
-
56:33
but color does inherit in many places.
-
56:36
Then we have pseudo elements.
-
56:40
And, four more minutes?
-
56:42
I've no clue, I'm on California time, I don't know when this is over.
-
56:46
Okay, I'm just gonna keep going.
-
56:48
Pseudo elements.
-
56:50
First line, first letter, selection, which is not in the specification.
-
56:54
Before and after.
-
56:56
Selection is not in the specification.
-
56:58
Not because not every browser supports it.
-
57:01
Last time I checked you still had to write the [UNKNOWN] prefix.
-
57:04
The reason it's not in the in the specifications is because they're
-
57:07
still specifying it and they didn't want to hold up the selectors specification.
-
57:11
So they pulled it just so that they couldn't finalize selectors.
-
57:16
So it, it is basically supported.
-
57:19
So notice that there's two colons right here.
-
57:22
That is the correct way of writing pseudo elements.
-
57:24
Not the one colon, but IE, older IEs, IE8
-
57:27
supports all of these, but only with a single colon.
-
57:31
So in practice you'd most likely just write one colon, but that's the
-
57:35
way you know you're dealing with a pseudo element instead of a pseudo-class.
-
57:39
Pseudo elements have the value zero zero one.
-
57:43
But so, you know, the weight is low but the specificity this, but your it's
-
57:49
actually, there's no other element that's targeting that
-
57:52
cuz it's basically creating an element for you.
-
57:54
So, really all is targets, targets it really well.
-
57:58
Okay.
-
57:58
so, I've used pseudo-classes.
-
58:02
As, those are elements that already exist on your page.
-
58:07
Pseudo elements are kind of like creating
-
58:08
these faux tags around things, like the first
-
58:11
line tag around your first line, or your first letter tag around your first letter.
-
58:14
It's not there, but you can pretend in your mind
-
58:17
that it is, and then selection, oh, I'll get to it.
-
58:22
So it's kind of like faux elements.
-
58:23
So we have faux, first letter here.
-
58:26
Anyone know why I would do first of type first letter?
-
58:30
I hear we have designers in here.
-
58:31
It's a design question.
-
58:34
Cuz it would be really ugly.
-
58:35
So, yes first letter will do every single one, but
-
58:43
that's ugly.
-
58:43
I mean, it's ugly anyway.
-
58:45
What I had originally was ugly.
-
58:46
But that makes it uglier.
-
58:47
So basically, it's as if you put a faux, like, first letter around that y.
-
58:53
It doesn't need to know which letter it is.
-
58:56
It's just whatever the first letter is of that element.
-
58:58
It will do whatever you want to it.
-
59:00
You can style it completely
-
59:04
before you couldn't add animation to it but I
-
59:06
believe now browsers are supporting animation on pseudo elements.
-
59:09
I haven't tried cuz you should be shot if
-
59:13
you're animating the first character, you should be shot
-
59:15
if you're putting a swan on the page that
-
59:17
keeps just floating around and covers your thing up.
-
59:19
>> [LAUGH]
-
59:20
>> Okay, then if, after the first letter we also have selection.
-
59:23
And this is selection.
-
59:24
Remember I was selecting text before and it did not do this?
-
59:28
That's because there's actually a selection on this page.
-
59:30
So I just selected this text and turned bright red.
-
59:33
Or hideous red.
-
59:34
Or, whatev, brown?
-
59:37
Because I said this slide star selection.
-
59:43
And I gave it a color.
-
59:44
So you can style whatever is being selected by the user.
-
59:47
And HTML5, who here uses HTML5 boilerplate?
-
59:49
[BLANK_AUDIO]
-
59:52
That's it?
-
59:54
Read HTML5 boilerplate, the notes.
-
59:56
You'll learn so much.
-
59:58
But they'd made their selection hot pink, so
-
1:00:01
they can just see who's using it without
-
1:00:02
actually, like, do you know what you're doing,
-
1:00:05
or are you just putting this in there?
-
1:00:08
You can disable selection.
-
1:00:10
And this is the one place where I talk about properties and values.
-
1:00:13
Because sometimes, you don't want things to be selected
-
1:00:16
if you're on a mobile device and you're playing a
-
1:00:18
game and you don't, when they little element for
-
1:00:21
a really long time, cuz you're not using Web GO.
-
1:00:24
Or Canvas.
-
1:00:26
It'll say, do you want to save this picture,
-
1:00:27
and you're like, no actually I want to flip it.
-
1:00:30
You can do webkit tap highlight color.
-
1:00:33
You can do webkit users select.
-
1:00:35
And, you can do webkit touch callout.
-
1:00:37
And there's also one for MS, and, and basically
-
1:00:43
that makes it so that it doesn't say save.
-
1:00:46
So don't mess with this stuff unless you're doing a game.
-
1:00:50
Cuz you really should keep the highlight
-
1:00:51
colors so people know what they're highlighting.
-
1:00:54
Before and after is generated content.
-
1:00:58
So here I have this paragraph that says the content.
-
1:01:02
And before I put before content with a dash, and I make it bold.
-
1:01:06
And then after I do after content and I make it bold.
-
1:01:10
So here we have the paragraph, and if I
-
1:01:12
try highlighting this you see I can't highlight everything.
-
1:01:17
Oh, here, I can, because it's actually highlighted in the page.
-
1:01:18
But if I just grab here, and go up.
-
1:01:22
I'm trying to highlight it.
-
1:01:24
It won't highlight the actual content.
-
1:01:26
Because the generate content, okay, is not part of the div.
-
1:01:31
so, it's basically, when I was talking about faux elements, it's if you wrote
-
1:01:36
a before and an after and you can style those as much as you want.
-
1:01:41
So we're not gonna play with this, but there's some IE10 specific values.
-
1:01:47
There's a shadow DOM you can actually style like progress bar, except for,
-
1:01:53
so far he's kinda pulling it out because Chrome kinda pulled out you know.
-
1:01:58
There's a whole fight, but you can still style some of those.
-
1:02:02
this, people are like but I still support IE6, sorry over there.
-
1:02:07
You can use most of this with IE6, there's
-
1:02:10
Selectivizer, which if you use Selectivizer plus any library,
-
1:02:13
and there's a few others that do this too,
-
1:02:16
but it makes older processors support most of this.
-
1:02:19
You have to look at the creative support.
-
1:02:21
And just remember that, don't pull in a
-
1:02:23
JQuery just so you can do a query selector.
-
1:02:25
Because you can just do a query selector, right?
-
1:02:29
And that's it for my talk.
You need to sign up for Treehouse in order to download course files.
Sign up