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

Taking a deeper look into the CSS selector rule

In the last few lectures we focused on positioning our code and learning how we can re-use it in the document. In this lecture, we will take a deeper look into the process of defining CSS rules. By the end of this lecture, you will know how to define rules for HTML elements.

Video Transcript

00:00While our last chapter was focused on where should we put the CSS if it's going to be
00:06Directly on the thing you want to style if it's going to be in the header of the file if it's going to be in
00:11An external file now we we turn our attention to the actual CSS and even before we start manipulating properties
00:19We want to get familiar with the three most common ways to define a selector now a selector is basically when you select
00:26Something that you want to manipulate we've seen one of them
00:29but in this lecture we're gonna take a look at it one more time where how do you select something inside of an HTML to be
00:34Able to define it and throughout the next three lectures
00:37We're gonna meet the three most common selectors starting with just writing down the name of the thing that you want to select
00:44Such as an h1 or a div or a paragraph. So let's see it in action
00:50So far all of our CSS rules were really flat
00:53We talked directly to specific items if it was our body if it was the a tag if it was one of our
01:00Headlines h1 2 & 3, but I want to take us a step deeper into CSS and see that CSS is really a
01:09language that is about
01:12structure
01:13So really in our case we could define rules that are based on
01:17Specificities on the location of an element for example, if I had your another h1 and I'm just gonna go ahead here and create it
01:26Just for the sake of this presentation. This will be our first
01:31h1
01:32But go ahead here and save it go back into my application and click on refresh
01:36We'll see that we'll have your our first headline followed by our
01:41Internal headline now, what if I wanted to have a different rule and a different size?
01:46For example for my first headline versus my internal headline even though they have the exact same name
01:52Now the difference between them is that this first headline is sitting in this global h1 position our body right into it
02:00It's our h1
02:02Contrary to that our h1 that is inside is sitting inside of a div inside of a div and it's inside of there
02:09What I could do is go ahead and set here another h1
02:13So I'm gonna go ahead here and just define a global h1 that let's say this one will be 60 pixels
02:19But go ahead and define an internal one saying hey if you're a div inside of a div inside of that an h1
02:26Then define that to 45. So only if all these rules apply
02:31Then define your font size to be 45 if I go ahead and click on refresh
02:36You'll see that now we'll have your one h1 that is much larger and one that is smaller
02:43And that's really about traveling inside of your CSS
02:45So instead of having only flat CSS I could have rules that help me filter out content
02:52now I didn't have to put the full path if I wanted to I could have said only just one div and
02:58Because our h1 inside of our element any h1 that is inside of a div
03:03Would adhere to this rule and if I go ahead and click on refresh
03:07We'll see that even though I change that rule
03:09I'm still in a larger position for my first element that makes it stand out
03:15Compared to my second element now notice if I go inside and I inspect this element that we're getting that 60
03:23Inside of it in our element and if I go inside of this element
03:26We'll see for our h1 that internal law that says that specific specification and notice how it's
03:33Overriding the global rule of our h1 now
03:37You might ask yourself what would happen if you put this h1
03:41Above our h1 that is underneath it now in this case
03:45What's happening is you have this first rule that is defined for internal h1s?
03:50Followed by a global rule for h1s in general that are sending it to 60 pixels
03:56If I click here on refresh notice that might what is the size of my global element now?
04:01It's still 60 notice the size of my internal element. It's still
04:0645 with that said if I had the same exact rule
04:11Although I like setting it in this way because it shows us that this is really gonna override that and above item
04:17But it's not necessarily automatic if I have another h1 rule by mistake that I forgot somewhere
04:24And I set that internal rule for the example to 20 pixels
04:27Then yes the last item that has been called is gonna be the rule that is going to be applied and literally we're gonna have
04:34Two items being overridden so really the rules in CSS are defined by what is the closest rule to our item and
04:42Then followed by what is the last item so it's not automatically the last last item
04:47It's the last item in the most closest rule to our specific element all right
04:53So that's covers that covers this topic and that leads us into the next topic in our chapter
04:59Which is going to be about classes see you in the next lesson when we talk about classes

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!