Check out a free preview of the full Complete Intro to Web Development, v3 course

The "Pseudo-classes" Lesson is part of the full, Complete Intro to Web Development, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Brian discusses using pseudo-classes to conditionally select which class to use for CSS styling. The pseudo-classes :hover and :first-child are demonstrated in this segment.


Transcript from the "Pseudo-classes" Lesson

>> We are gonna talk about Pseudo-classes and pseudo elements. This is more for completeness, a little bit more of an advanced topic when it comes to CSS, but I just want you to be aware that they're out there. So what happens if you wanna do something conditionally, right?

So for example, I have this like box here this is hovering your mouse over me, right. Now if you hover your mouse over it, it changes color and it changes size. This here is called a Pseudo class. So it has this base style of width 100, height 100, background-color and color, right?

And then if you hover over it with this colon, the colon being the key part of this here, right. This applies this class only conditionally if that hover state is true. Changes the background color width and height and you end up with this. Kind of cool somewhat interactive thing, right?

There are many CSS pseudo classes I'm just showing you one of them. So one thing to keep in mind when you're doing like hover for example, how do you hover on iPhone? You don't, right? There's no hover, right? You don't have a mouse. It does kind of apply some trickery like if you touch it sometimes it requires you to touch it.

To twice the first actual hover the second touch will click on it but for all intents and purposes you can't hover on mobile. So if you're intending for your site to work on mobile avoid hover states. That it is useful sometimes, what's a good example I'll show you one a good example.

Here on this web course website if you hover over the, you can see this little stripes in the corner changes color. That's done by using a CSS hover effect. And it's just like a little nice little touch if a person on mobile never sees that it's totally fine, it's just like a little flair to it.

That's when you should use something like cover it's should never be for core functionality otherwise you're excluding all your mobile users which most of us don't wanna do. But if you're doing like corporate intranet where it's like someone like accountants are always gonna use on their desktop computers.

Then it doesn't matter, right? They're always gonna be doing that they're not gonna be on their iPhones, browsing yours, spreadsheet app or something like that. In other words, just in general, with web dev, your audience is always the most important thing. Who are you building this website for?

You need to continually ask yourself that question. Okay, Let's talk about another one. So I have this first child example and the color is crimson, right? So I have this First, Second and Third. And then I have this one here, where it says first-child example so matches this exact same first class, but then I have this colon, first-child.

What color our Second and Third gonna be?
>> Crimson.
>> Crimson, what color is first gonna be?
>> Limegreen.
>> Yeah, so first-child is gonna be select only the first one in the list, right? The first one that matches this, so you can see here this is First.

So what's cool about this is now this is kinda like resistant to change over time. So let's say, suddenly we wanna start counting from 0 so we can put 0 there. I didn't have to go change anything else, my CSS just worked right away. Because the other way you could have done this as you could have just put First here and unstyled based on the first class, right?

Totally would have been valid but here styling by the structure of this makes some sense that so that. If later this changes which it did when I added 0, everything just worked right away. What happens if I change this too? Last-child. Same thing, right? It only will do the last one.

So that's another Pseudo-class, it ends up being super useful. So another really good example of when you might wanna use this. Let's say, I wanna have a nice black line between all of these. So if I put a border bottom which is like border but it's only gonna do the bottom border, right?

One pixel solid black, So they all have this like bottom black line, but what happens if I only wanted it between them and not at the end? In other words I wanna drop this one down here at the bottom. Easy to use first-child example and you just say border, bottom, none.

And now they have this like dividing line we could make it look better we'd have to put some padding on bottom, right. So we'd have to say, padding bottom 10 pixels or something like that? Maybe 20 pixels, 30 pixels, 50 pixels. There we go, that looks a little bit more plausible now, right?

And now we have this like nice dividing line, whereas if I didn't have this line, right. There'd be this weird last line, and it doesn't look as nice. Another thing I'll probably notice here is, I guess and check a whole lot. The way that one writes CSS is try this, did it work?

No, try it again. And just keep doing that over and over again until you get what you want. Okay? So I put a link here to Pseudo classes here. This is on CSS tricks again. There's so many, focus that's another one like active. This is another one. If you're pressing a button while you're pressing light holding the button, that's active.

Focus can be, if you click on a input and that they're cursors in the input that's focused, right. If you have an a tag and you want your visited tags to look different than your unvisited tags, you can do that with a selector that way. Target, enabled, there's so many of these that I'm just not even gonna get into them first nth-of-type, first-of-type, okay?

I'm gonna stop there because there's a whole article here but you get it, right? There's these conditional Pseudo class selectors.

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