Combining logic with combinator selectors
Combinator selectors have been around since CSS2 and have been enhanced in CSS3. Through the usage of combinator selectors we can drastically reduce the amount of classes we use in our application. Combinator selectors enable us to define rules based on the relations of an item to its parent, immediate adjacent item or even a further removed preceding item. By the end of this lecture, you will know how to work with the Combinator selectors.
properties: Combinator selectors, Child selector, adjacent selector, preceding CSS3 selector, CSS2
Video Transcript
00:00In the last lecture, we've met the negative, and got that out of the system, so we're not going to be negative again.
00:06Now we're going to talk about combinator CSS rules, basically CSS selectors that enable you to basically combine relationships.
00:15Basically, well, basically this is a CSS3 thing, very cool, enabling you to basically ask for an item based on a relationship it has with items near it,
00:24such as its siblings, such as its parent, let's see it immediately.
00:27Contrary to regular CSS where it's not a direct parent, what if you want to ask for something that is directly the child of a certain element?
00:35Let's see it in action, we're going to meet three of those different types of combinator or combinators.
00:41Let's do it. Let's continue our journey in the world of CSS selectors.
00:45While you are away, I added also a boldness to our item here, really to showcase one of the combiners or combinators that we're going to be working on right now.
00:57We're going to cover three new combinators, selectors that belong to CSS2 and CSS3, so let's get right to them.
01:05I'm going to go ahead and just go into my editor, and just before we start, I just want to show you what I'm trying to do.
01:10Notice how I set here the B tag around my span outside of it.
01:15The reason is because I want to show you one of the properties, and even before I do that, I want to make sure that we dehighlight our elements.
01:23I'm going to go right away here and just unmark our background selection,
01:29and then I'm going to go right to the end of our new code that we were adding in,
01:32and I'm just going to paste it right back in there.
01:35Let me just find the end here, and I'm going to go ahead and just paste the code,
01:39and we're going to start working on CSS combinator or codes of children.
01:43Notice this code, I just put it in, it's a little bit different.
01:46Notice how instead of having here a star that we had here before that enables us to select any item inside of an article,
01:55and inside of it it has a span, instead of them saying any paragraph,
01:59I could have also left it at star, that has a child, this is the child controller of span.
02:07Now what this does is a little bit different than if we just said here just a star instead of the paragraph,
02:12or remove that altogether, this means explicitly that the span belongs to the paragraph.
02:18Now if we look in the samples that we have above here,
02:20we'll see that the span does not belong directly to the paragraph anymore,
02:24because it's wrapped around a B tag.
02:27So if I go ahead and I click on refresh after I save the files,
02:30you'll notice that now this item is not getting highlighted.
02:34Now this becomes really useful in scenarios where you want to be very explicit
02:38about if something belongs exactly as a child of an element and not a descendant or later descendant,
02:45such as different spans that do different things depending on what layer of depth they're in.
02:51So let's move on and continue to the next item, and the next item is adjacency.
02:56And the adjacency enables us to basically know when an item is right near another.
03:02And the way we do that, we first start with the item we want our item to be next to.
03:06So for example, our H2 is our headline,
03:08and I really want every paragraph that is right next to an H2
03:12to have an extra padding around it, mainly in its margin top.
03:16And I could go ahead there and just set a really big margin top,
03:19making sure just so it's very obvious the example here.
03:22And if I go ahead and save this, go back into my application,
03:25those in that plus, this basically said any P that is right after an H2.
03:30If I go ahead and I click on refresh here,
03:31and notice first all these H2s that are right next to a paragraph.
03:35We have one here, we have another one here, another one right there.
03:38Let's click on refresh.
03:40And notice how now we have this extra nice big spacing between our H2 and our paragraph.
03:47All right, now the most important thing to note here,
03:49notice how the following paragraphs don't have that extra spacing.
03:53And there's a reason behind it.
03:54There is an attribute that enables you to do that,
03:56and that is from the world of CSS3 combinators.
04:00And with the world of CSS3 combinators,
04:03then I would have basically the option, the possibility of making sure
04:08that every single paragraph after an H2, after a certain element,
04:12really not necessarily only H2, that are right after,
04:15I'm just going to go ahead and copy this and I'm going to comment it out
04:19because I want to make sure that we don't have any competing things
04:23going on at the same time for this example.
04:26And I'm going to go ahead here, instead of putting a plus here,
04:28I'm going to go ahead and put that squiggly line that is from our shift
04:32and the value that is right next to the one.
04:35What that will do is basically every element, every paragraph
04:39that is underneath an H2, so here's one of them,
04:42and it doesn't have to be immediately.
04:43So for example, inside of the div class, if there's an H2,
04:47then everything underneath it that has a paragraph will be in that rule.
04:50Same applies for this item.
04:52There's an H2, so everything underneath it in the same parent
04:55and the same cluster of items, then it would wrap it around inside of it.
05:00Now just notice that if I put, for example, this H2 inside of a div
05:04for any reason, so I'm going to go ahead here and put it inside of a div.
05:09Notice how this last div, this last paragraph H2 that is with that div
05:15will not apply to the paragraphs below them.
05:18But because there is already an H2 going on above here,
05:23it would be applied.
05:24So let me make sure, let me just quickly create here a separation
05:28that will make sure that we could sample an example of an item
05:32that is not explicitly.
05:33So I'm going to go ahead here, and for this last paragraph,
05:36I'm going to set it to be a div inside of a div,
05:39and I'm just going to make sure that that item is completely separate
05:42just for the sake of this presentation and example,
05:45and then this, oops, and then this item, oops,
05:48and then this item will not be part of that chain
05:51because it's outside of that boundaries.
05:54So I'm going to go ahead here and save this item,
05:56click on refresh, and let's see it in action.
05:58And you'll see every single paragraph that is one after each other.
06:02So every single item, I don't think I saved it actually.
06:06Let me go ahead here and just save this item as well.
06:09And go ahead there and click on refresh
06:11because I would expect to have a break in here, I believe.
06:14So let me just click on this item to inspect it to see,
06:17is it getting that rule and it surely it does not.
06:20And if I go ahead here and inspect this item, it sure does.
06:24So in this case, this item is inside of the cluster.
06:27And let's see why you see because here there's a div
06:29so it's not adhering to that item.
06:31So that makes sense, but let's find here.
06:33So here we have a paragraph and another paragraph.
06:35But if I'd go ahead here and just because I don't have here enough paragraphs,
06:38if I go ahead here and I just undo all these HTML changes that I created here,
06:43just putting all those paragraphs in the same context,
06:46in the same area as the item in that H2,
06:49you'll note that all these paragraphs that follow that H2,
06:52even if they're further away and there could be things in between them,
06:56we'll still have that extra indentation.
06:59We'll still have that extra spacing.
07:00So in this case, you'll see that now this item is in that rule
07:03because it's not sitting in a child container.
07:06So that's it.
07:07That covers our combinator CSS styles.
07:12In this lecture, we've met two combinator or combattinaurs.
07:17We've met two in CSS2, the one which was the immediate child.
07:22The next was a brethren, an adjacent item,
07:25while the last one was basically all the things that are right
07:28and all the things that are underneath a specific element.
07:31So if there's any time that you put a H2
07:34and you want all paragraphs underneath it to be a different size,
07:36then you could control the sizes of them once we pass that paragraph.
07:40This could be incredibly useful in scenarios where you have your main copy
07:44and once there's another headline or there's something that happens,
07:48all the content below that area would then be resized to a smaller size.
07:52So then you could have a lot more detailed control
07:57without needing to use a lot of classes.
07:59Very cool features.
08:00So we're not done.
08:01There's still a lot more selectors to go through.
08:04So we're going to see in the next lecture where we continue to select HTML.
