CSS3 Attribute Selectors
In the last lecture we learnt how to work with attribute selectors, they%u2019ve been around since CSS2. A few new powerful features have been added into CSS rules with the birth of CSS3 Selectors. In this lecture we will explore the new CSS3 attribute Selectors. By the end of this lesson, you will know how to work with the new CSS3 Attribute Selectors.
properties: CSS3 Attribute Selectors, ^=, $=, *=
Video Transcript
00:00In the last lecture, we were introduced to the CSS2 attribute selectors, and this lecture
00:06is going to continue talking about selectors, attribute selectors, but we're going to take
00:10a deeper look into the new attribute selectors added in to CSS3.
00:16By the end of this lecture, you will know all the different CSS selectors for attributes
00:20both CSS2 and 3 incorporated.
00:23So let's jump right into it.
00:26We're finally ready to talk about some CSS3 selectors.
00:29We're talking about attributes, CSS3 selectors for availability and browser availability.
00:36Make sure to check our references where we have a few links to browser availability as
00:40well in them.
00:41And I'm going to go ahead and right away start, and we're going to start off with, there's
00:45three new additional attribute variables.
00:49You could say they have rules, selectors really, that's probably the right word for that, right?
00:54So let's go ahead, let's jump right into it.
00:55The first one of those selectors enables us to basically define if an item starts with
01:01a certain value.
01:02It is very easy to work with, and I'm going to grab this or pipe selector that we had earlier.
01:09And I'm going to go ahead and instead of setting that pipe, I'm going to go ahead
01:12and set here, where are you, shift six, which is the triangle, that upper triangle,
01:19I don't need to know how to call it.
01:20If you do, say it in a comment, once and for all, I'll know how to name this whenever
01:24I call it.
01:25All right, I'm going to keep the same rule here, so I'm going to go ahead and just comment
01:29out our CSS selector above here that made our second paragraph yellow.
01:35I'm going to go ahead and set it to the starting with, and if I save this, go back into my
01:40application and click on refresh, we should be seeing the same result.
01:43Only this time around, this rule is basically overriding that basic accessibility because
01:50both items start with the words accessible.
01:53All right, so this rule is a lot more explicit than the pipe rule, which only found items
01:58that were controlled via pipe.
02:00But beyond that one more extra feature of this item that is not available with accessibility
02:05one, which was available in CSS2, I want to show it to you in action.
02:08So let me just undo this for a second.
02:10What if we wanted any item that starts with access?
02:15If I just set it to the word access, because there is no direct reference to access here,
02:21but it's accessible, you'll see that the value would not work.
02:24It would not change the color of our item to be yellow.
02:27Contrary to that, well, basically the only way it would is if we set the full path
02:31name of whatever is between those dashes.
02:34And not only that, it had to start from the beginning of the dashes series.
02:39So that was very limiting.
02:40In many ways, it was extremely limiting.
02:42So let's go ahead and comment this rule out.
02:45And we're going to go back into our rule in CSS3.
02:48Because it starts with does not search for complete words, I could go ahead and say,
02:53hey, anything that starts with the word access.
02:55And it could be accessibility, access, mobility, access, whatever it would be.
02:59Any item that starts with access or even just access as a whole value, it would work for us.
03:05And in this case, if I went ahead here and clicked on refresh,
03:08then we would basically get both items.
03:10Now, I want to take one more look and one more observation at this.
03:15And the observation I want to do is this one.
03:17And that was true to all the other items as well.
03:20If we wanted to define this based on a class, if we wanted this to be part of a class,
03:25we could have gone ahead and easily set it to be in a class.
03:29And all we would have to do is just instead of set it to our data type,
03:32go and set it to our class.
03:34Which would make sense if we're making a rule that would be applied always
03:37anytime there is an access ability class inside of the application.
03:42I'm going to go ahead here.
03:42If I click on refresh, we'll see that now our second item,
03:45which is the only one that has a class defined,
03:48would actually have that accessibility rule inside of the item.
03:51All right, so that starts with.
03:53I'm going to continue with this class rule.
03:55And I'm going to go ahead here and just copy the rule.
03:57But this time around, I'm going to go ahead and say anything.
04:00Not only a paragraph, but anything that has a class that not only not starts
04:05with, not that starts with, but that ends with.
04:08So to make it to end with, I'm going to set there with the end.
04:11And any class that ends with the word large,
04:14then how about we make it really large?
04:16And in this case, I'm going to go ahead here.
04:18Any class that ends with the word large, I'm going to go ahead and set its font
04:23size.
04:25And I'm going to set it to be three times its normal size.
04:29I'm going to go ahead here.
04:31Relatively speaking, it's going to be a lot larger.
04:36Beautiful.
04:36So that worked brilliantly.
04:38That enables us basically controlling based on the ending.
04:40And I didn't have to use a full word.
04:42I could have used just the last three characters if I wanted to.
04:45And it would basically check to the last element.
04:48So if I wanted to, I could have started with the beginning with, ending with.
04:52And last but not least, and the most complex one from a perspective of
04:56complexity of searching, would be a class.
04:59And I'm deliberately doing it a little bit different each time.
05:01Notice how in the first time, it was a paragraph.
05:03Second time was any item.
05:04And now I'm just not putting anything, which really from CSS to and onwards
05:08means anything.
05:09So I don't have to put that star in a case like this.
05:12It would find any class.
05:14And in this case, I want to have any class that has any value.
05:18And any class that has the value of light within it.
05:23So if it has the value light within it, if there's a space, if there's no space,
05:27it doesn't really matter.
05:28If the word light is inside, then I want to set how about what we're going to do.
05:32We are going to set our color of our background to be white.
05:41And only if we have light inside, there we go, we have light.
05:45So if I click here on refresh, we should see now that our yellow text box should
05:49hopefully switch a color.
05:51But in this case, let's see what's going on here.
05:53So in this case, we're searching for contains a value.
05:58And I'm going to go ahead, just for the sake of our testing,
06:01I'm going to go ahead and put just a value from within any kind of mismatch
06:05mix match of values.
06:07And I'm going to go ahead also and just make sure that it's a paragraph
06:10because it might be just not working without setting anything.
06:13So I might just have to set something instead of nothing.
06:18So I'm going to go ahead here and just set here a paragraph, click on refresh.
06:21Let's give it a try.
06:22And here we go.
06:23Here we go.
06:24So that's exactly what my problem was was I set the value to be without
06:28absolutely anything.
06:29And actually that is not probably legal.
06:33So it did not work, but I could put here a star, which is probably what I have
06:37to do, but I do believe that in the future, putting nothing would be
06:40equivalent to putting a star, but let's not live in the future.
06:43Let's live in the now where we have to define something.
06:46And if we don't want to define something, we could just put there a beautiful
06:48star that would act as if anything could be there.
06:51So basically one extra character, not too bad, not too bad indeed.
06:57And that finishes our conversation about CSS attribute selectors in general.
07:02And obviously we're done speaking about CSS3 selectors as well.
07:06I'll see you in the next lecture where we continue talking about selectors.
07:09We're not done with selectors, but we're done with the attribute selectors.
