02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Focusing on a target with CSS3

One of the coolest new selectors to CSS3 is the target selector. The target selector enables you to control the styling of an item that is the focus of a hashtag link. HTML links can have a #name applied at the end of them. These anchor names help the browser jump to areas of a page based on anchors. With the new CSS target selector we can now help viewers by highlighting the selected area making it a much easier find for them. By the end of this lecture you will know how to work with the new: target selector in CSS3.

properties: :target, css3 selector

Video Transcript

00:00If you put a gun to my head and ask me what is my favorite CSS selector, my answer would
00:06be the target.
00:08No pun intended.
00:09Oh wow, I didn't even realize that I was just a target with that virtual gun and all
00:14that stuff.
00:15Anyways, in this lecture what we're going to do is we're going to dive a little bit
00:18deeper, or not deeper, we're going to be very shallow, as we explore a really fantastic
00:23feature added into CSS3, enabling us to basically talk to specifically a target that
00:29is being highlighted.
00:30Now if you're asking yourself what is a target, the target in the CSS3 terminology is
00:35really a hashtag area in the page, or basically an area that the user has just been sent to.
00:43In HTML you'll find those in a URL, a hashtag, and then an ID, which would then jump the
00:48user to a specific area in the page.
00:50We're going to see it all in action in this lecture, and by the end of it, you're
00:53going to have a really, really cool feature that many people don't even know exist,
00:58and is one of my favorites.
00:59So let's jump right into it.
01:01To showcase the next feature, I created a list with a few of our chapters, with anchor
01:07links, that will take us to different sections in our site, where there's actual
01:11content that correlates to it.
01:13I want to show you that in HTML for a second.
01:16So if we jump over into our HTML, we'll see that we have your lists, an unordered
01:21list, and each item has a link that has a hashtag and an ID, connecting to a section
01:27in the page.
01:28If we scroll all the way to the bottom, we'll find a few sections here, for
01:31example, the uni one, which was the first one that correlates to it.
01:35Thus, when the user clicks on and the URL changes to the hashtag, you and I, it will
01:41jump to that section in our page.
01:44Beautiful.
01:45And now it's time for us to showcase this amazing feature that is available as a
01:50selector since CSS3, and it is a sudo class, which basically a sudo class,
01:54which basically just means that it is not a hands-on class, but a generated
01:59class that is being created.
02:02Now basically, it's called the target selector, or at least I'm calling it the
02:05target selector, because it seems the most appropriate name.
02:08And the way it works is incredibly simple and incredibly beautiful.
02:12You write the word target, and that will make sure that it's going to focus on
02:16the item that is currently being focused in the URL.
02:20So if I click on to the hashtag, into uni, then this uni element, this section, if I
02:25highlight the section, this is what the target means, because this is the target
02:29currently in our scope, making it very easy for us to highlight the section that
02:34the user just clicked on.
02:36For example, I'm going to set the background color to be yellow.
02:42If I go ahead and save this, click on refresh, we'll see that now that our
02:45target is selected, if I click on refresh, that target is now going to be
02:50highlighted.
02:51Not only that, dynamically as we click on to different areas.
02:54So if I go to, let's say the linking and actions, we'll see that now the
02:58linking and actions will be selected and dynamically will change those selections.
03:04Incredibly powerful, incredibly cool.
03:07What I want to do next before we wrap up this lecture, is I really want to
03:10also maybe dim out all the sections that are not the target.
03:14There currently isn't as we make this video a way to say, hey, everything
03:18that is not the target, because it could be really anything.
03:21But in our case, we know that we only have sections that relate to our
03:25targets.
03:26So I could go ahead and just add here a section, and I'm going to say not
03:31leveraging that capability of saying we don't want, and hey, I don't want
03:34a target.
03:35And what's not target, I could go ahead here and set a background to all
03:39of them if I wanted to.
03:40But in this case, I want to go to all my H2s, set their font size to be
03:44maybe one and a half EM, and I'm going to go ahead and also set the
03:47color of the font itself to be gray.
03:51And let me go ahead and duplicate this because I really want to change
03:53the paragraphs as well.
03:55So I'm going to go ahead here into our paragraph.
03:58And for our paragraph, same deal applied.
04:01I'm just going to set it to its size of the dot eight, let's say.
04:05And I'm going to set it to color gray as well.
04:07Let's keep it at that.
04:08Oh, that's not P. There you go.
04:11And that's it.
04:12That's all we have to do to make sure that the item itself will be selected
04:16and everything that is not that item will be dimmed out, making it very
04:19easy for us to then jump to certain sections, highlighting those
04:22sections and making the items that are not basically fade away.
04:29Amazing stuff.
04:30Really cool.
04:31Brand new to CSS3 selectors.
04:33We're almost done.
04:34We still have a few more things to cover in CSS selectors.
04:37So I'm going to see you right really soon in the next lecture.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!