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

CSS2 Attribute Selectors

In the last lecture we discovered how to control the colors of actions and links in CSS we are now moving into the world of attributes where we will stay in the next two lectures. In this lecture we will cover the new CSS2 attribute selectors and how to work with them while in the next lecture we will cover the CSS3 ones. By the end of this lecture, you will know how to work with CSS2 Attribute Selectors.

properties: CSS2 Attribute Selectors, =, ~=,|=, E[prop]

Video Transcript

00:00In the last few lectures, we were slowly progressing into the world of CSS selectors.
00:06We've met quite a few CSS 1 selectors and CSS 2 selectors.
00:11And this lecture is still in the CSS 2 world, but it's edging us into the world of CSS
00:163.
00:17We're going to start off looking at attributes, and it's going to be the first lecture in
00:20that topic, and the next lecture will still be about attributes, but in CSS 3.
00:25So attribute selectors really cut down the complexity of code, because they enable us
00:30to basically select elements based on their attributes as well.
00:34We're going to see a few of those, basically all the ones that were introduced in CSS 2,
00:39and then move on in the next lecture to take a look at what was evolved and what was added
00:44in in CSS 3.
00:46So let's jump right into CSS attribute selectors.
00:51Let's continue our journey into CSS selectors.
00:54And now we're ready to move on to a new type of selector, which is an attribute selector,
00:59which has been available since CSS 2 has been expanded in CSS 3.
01:05It is widely available in most modern browsers, but make sure to check our references where
01:10you'll find a link with availability based on browsers and the selector versions.
01:15But we're going to start with the 2.0, and to make this happen, I'm going to go into
01:18my HTML and just grab one of our paragraphs.
01:22I'm going to go ahead and just grab this paragraph, and I'm going to go ahead and add a new data
01:27attribute.
01:28In HTML 5, you can literally add any attribute.
01:32If you start with the word data dash, and then give it a name.
01:35In this case, I'm going to call it data type.
01:37I'm just going to create a variable.
01:39It doesn't really do anything, HTML will ignore it.
01:42It's wonderful if you need to add stuff that you want to use in JavaScript, very, very
01:47useful.
01:48Or in this case, we're going to take advantage of it to use it with CSS.
01:51I'm going to go ahead for this data type, and I'm going to configure a data type.
01:55For the data type, I'm going to go ahead, and I'm going to start it off with calling
01:58it accessible.
02:02The idea behind this is that this data type is the further accessibility features.
02:06We're going to make it a lot more legible for users that need that type of technology.
02:12Right now, we're going to do it all over for our site because we're not using JavaScript,
02:15and we're not using any advanced splitting or separating of our logic, but I'm going
02:20to go ahead and literally create this attribute.
02:23I'm going to go ahead into my style sheet, and I'm just going to find a nice empty spot
02:27here right after all the last few items that we've created.
02:31I'm going to go ahead, and I'm going to set that attribute.
02:34I could go ahead and just define it based on its position.
02:37In this case, we could go ahead and set it inside of our article body, I believe.
02:41No, it's right out of the article.
02:44It's inside of our body.
02:46It's inside of a ... Basically, just inside of our body.
02:49I could go ahead and show us that our body and p and configure it on and on, or I could
02:55just go ahead to my paragraph and say, hey, paragraph, but not every paragraph.
02:59Only paragraphs that have a data type, which basically means this is the attribute that
03:04we created.
03:05Only paragraphs that have this data type would then be controlled by this rule.
03:13In this rule, what I want to do, I want to go ahead and set our foreground color
03:17to be ... Let's make it black, or let's make it white.
03:22I want to make my background color ... I want to make that background color to be black,
03:29making a very sharp difference.
03:31I could go ahead and just use the background directly.
03:33If I go ahead and save this and go into my browser and click on refresh, we'll see that
03:37one of our paragraphs now is going to be highlighted, making it a lot easier to read.
03:43Now, let's take a deeper look into this.
03:45This is, by the way, it's called an attribute selector.
03:49It's been available since CSS 2.0, so I'm going to go ahead here and just add that rule
03:54right here.
03:57What if that this was not good enough?
03:59In this case, it really isn't, because here, we're just going to any attribute.
04:03Let me just go ahead and mark it down, or even I'll leave it there.
04:05We're going to override it with the next one.
04:08The next one, I want to say, okay, paragraph that has a data type attribute, but we
04:12also want to know its value, and its value needs to be accessible.
04:20In this case, again, it's also available in CSS 2.0.
04:24How about in this case, if we're in the accessible, then I'm going to switch it around, and I'm
04:28going to switch the color around, and this time, the color will be black, and the background
04:34will be white.
04:36In this case, this should override the item above it.
04:39If we go ahead and click on refresh, we should have here override where it's going
04:44to be white, and it's only white, because we actually defined it based on a name that
04:48exists.
04:49If we put here accessible2, accessible2 does not exist, and for accessible2, it would not
04:53match that new rule that we had right below there.
04:56There's an exact matching of our ruling.
05:01Let's continue.
05:02Beyond that, what would happen, and this is all nice, but this we could have done easily
05:08with just setting a dot, basically a class attribute.
05:12Basically this would probably do the exact same thing, right?
05:15So if I'm just going to comment that out, this would probably do the same thing.
05:20So we'd have any paragraph that is dot accessible.
05:24Speaking of which, we could have deducted out that P, or put there a star, which would
05:28be basically the same thing, saying any attribute, all right?
05:31But I'm going to leave that with that paragraph for now.
05:34Let's go on to the next one.
05:35Let's go on to the next attribute rule, which is very similar to this first rule that
05:40we created, but really where it stands out the feature of this component.
05:46Beyond our capability of setting it based on an explicit value like we've done in line
05:5038, we could also define it with white spaces.
05:54So in this case, imagine that we had here not only a paragraph that has here an accessibility,
06:00what if it was accessible, light, large?
06:02All right, so maybe it's accessible, light, and large.
06:08How do we fetch only the accessible, or how do we know it's accessible?
06:12How do we make sure that if it is accessible, still at this stage, if I just go ahead here
06:16and just delete this just for a second, and I click on refresh, we'll see that it's not
06:19going to recognize it anymore because it really, literally really isn't that value anymore.
06:26Because our value is not explicitly accessible.
06:29It is accessible, but it is kind of accessible, and we use that squiggly, which you'll find
06:34right next to your number one when you press on shift if you're typing in English.
06:40So this value basically enables you to say kind of about basically including white spaces
06:45basically break down the content into distinct modules that will then have that separation.
06:52So if I go ahead and I click here refresh, we'll see that now it will recognize it.
06:56Not only will it recognize that, I could go ahead and also say, hey, by the way,
07:00if you're light in this case, if you have a property which is called light, then switch
07:05it around and make it light.
07:07Or maybe in this case, let's make it light.
07:10And if we say we want it to be light, then we're going to set here the color to be yellow,
07:16and we're going to set the background to be black.
07:20Or we won't even touch the background.
07:22So if we have a light color, it will be light.
07:25And we can do the same thing for the large.
07:27So if I go ahead here and set here another rule, and that other rule will say any item
07:31that says on it large, then go ahead and set how about our font size, we'll change
07:37our font size to be three times its normal size, or its bounding area normal size.
07:45So we're going to duplicate the size of that normal font size.
07:49If we go ahead and we click on refresh here, we should see that our font size will grow
07:53dramatically, our text will be yellow, and our background will remain black.
07:57So if we take a deep look at this, we'll see that because we're accessible, then our
08:01color type is black.
08:03Because we're, well, actually technically, because we have a data type, then our background
08:09is black.
08:10We're not accessible, so we're skipping this item, because it's not explicitly accessible,
08:14so that rule is not applied.
08:16But because we're light, our color will be yellow.
08:18And because we're large, our font size is larger, and we're using that with that,
08:22I don't even know how to call that, I'm just going to call it the squiggly line.
08:26Before we complete our conversation about the CSS2 selectors, there's one more selector
08:31left.
08:32Now to take advantage of it, I want to go back into our HTML, and I'm going to go ahead
08:36into a new paragraph, and I'm going to give it a new data type as well.
08:41And in this data type, what I want to do is I want to basically configure it in a hierarchy.
08:46Now this hierarchy is very similar, or it is actually exactly the same, like the hierarchies
08:50you have in CSS, where you have a font, and then you have a dash size.
08:55So in this case, I want to set it to be accessible.
09:00That's basically accessible is basically the family type, you could say, or the hub of it
09:05all.
09:06Now what's beneath it, how about we say that beneath it would be a large light, or light
09:12large.
09:13So how about we set it to be light, large.
09:17So light large, or if we could have an accessible light, and we could then have a space and
09:21say accessible, accessible.
09:24But let's start with X, how about we'll just make it a hierarchy where we say accessible
09:30light and large.
09:31So it's basically a subcategory.
09:33So an item for it to be light, it must be accessible, an item for it to be large, it
09:38must be accessible and light.
09:39So that way, it's basically a sub configuration of the element.
09:44What that means for us when we're creating our CSS rules that would apply to this item,
09:48if we're adhering it as a class itself, or we're setting it as a data type, it really
09:53doesn't matter in this case, if we want to configure the rules and define them to try
09:57to basically call them only in specific scenarios, we could easily call this a class as well
10:03if we wanted to, and approach that item, I just write here class.
10:06But what I want to do next is I want to show you that in this case, for example, the large
10:11item, it might work for the top item, but it's not going to work for the bottom item
10:17no longer.
10:18So I'm going to go ahead and create one more item, and I'm going to position it
10:21right underneath here.
10:23And I really want this item to be larger, but I only want it to be larger if it is accessible.
10:30Or in this case, how about let's start with accessibility.
10:34So with the case of accessibility, I want to ask if it's accessible, right?
10:38But I'm not trying to ask is it accessible, period, I'm trying to ask is it accessible
10:43in that separated list that is separated by those dashes.
10:49Now to do that, I'm going to start my item with a pipe.
10:51So we saw without a pipe, is it exactly a match with the squirrely line or the wiggly line
10:57we saw that it was, what did we see it was?
11:03I don't remember.
11:05Do you remember?
11:06Of course, I remember.
11:08So with the squiggly line, it was based on a space.
11:10So if the item itself was in a space separated item, just like we have with classes, then
11:16it would find it based on that space separated separation.
11:20The last item that we have in CSS 2.0 is this controller that basically is if it's in
11:25part of that list that is separated with dashes.
11:31So I'm going to go ahead here and just click on save for a second.
11:33Click on save in my CSS as well.
11:35What I'm expecting is for this new item to be accessible.
11:39And what does that mean?
11:40It means that the font size would be crazy large, but really in this case, I want to
11:43make sure that let's say our background would be a dark color.
11:48I'm going to set it to be gray and I'm going to set my foreground color to be very light.
11:53So I'm going to set it to be yellow, making it very accessible, a yellow and gray, very
11:58legible.
11:59So I'm going to go ahead here and click on refresh and we should hope to see that our
12:02item is now yellow on gray.
12:05In this case, it's kind of a very dark black, but let me just close this so we could see
12:09everything here.
12:10And I'm just going to go ahead here and also make sure in this case, I'm going to
12:16make sure what are we approaching?
12:17I think this is our item.
12:18Oh, we did yellow on both.
12:19So let me set here this one to be red and I'm going to set the background to be yellow.
12:24So it'll be a very clear separation.
12:26Let's go ahead and give it another shot here.
12:28And you know what?
12:29Let me also make that font size much smaller because I while I was testing things out,
12:33I made it way too big.
12:35So let me go ahead there and make it smaller.
12:37And here we go.
12:38So now we have our font that was larger in our black container and right underneath
12:42it are yellow, our red copy in a black container.
12:47So notice how this accessible this time around is completely different because it's
12:51derived out of these dashes.
12:54Now if you go ahead and say, Hey, I want to set here only items that are light in the
12:58dash rule, that is an option, but you must also take into consideration if you're just
13:03setting it to be light and I'll click here on refresh, you'll see that this rule will
13:07not get applied.
13:09Now the reason why this rule is not getting applied is very simple.
13:13It's because it assumes that you're going through that priority.
13:16So if it's not accessible light, which would be the value that you would want to
13:20put here, it would not find it literally in many ways.
13:23This pipe sign is basically in many, many ways.
13:27It's really starting with in many, many ways, although maybe it's a current
13:32interpretation of Firefox.
13:34But if I go ahead here and I click on refresh, we'll see that it will work
13:37even if it's starting in this hyphen separated list of values.
13:43All right, so I'm going to go ahead and move on to CSS3 and I'm going to see you
13:46in the next lecture, but really you would hope that there would be a more
13:50plausible way, such as maybe putting here a star and saying anything that is
13:54star light.
13:55And if I go ahead here and click on refresh, that would be wonderful if that
13:59would be implemented if that was, if there was a way for us to do that
14:02currently, but currently I don't have a specific way using this comma
14:08delimitation to just select this item that is light.
14:11I have to incorporate that full path, which could be one of the reasons why
14:15it's not going to be as popular and not going to be as used as features such
14:19as these separated items that would be a lot more useful.
14:23So that's it for this lecture.
14:25And we're going to see you in the next lecture where we continue with this
14:27conversation, but we're going to cross over into CSS attribute selectors.
14:32So see you in the next lecture when we do that.

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!