Working with the currentColor
So far in the lectures in this chapter we worked with colors by defining them in absolute terms: by name, RGB or HSL values. We now turn our attention to relative colors. The first of them is the currentColor. The currentColor keyword enables us to get a reference to the current color. This can become very handy, we will explore that in this lecture. By the end of this lecture you will know how to work with the currentColor keyword enabling you to create dynamic color assignments.
Keynotes:
- CSS3 doesn't have variables
- wouldn't it be grand to not use the same color all over %u2013 maybe in CSS4?
- For now the first CSS variable around is called currentColor
properties: currentColor,color, css3
Video Transcript
00:00(upbeat music)
00:05- We're back to the technical world of CSS3.
00:08Now, unfortunately, CSS does not support variables.
00:13Maybe in the future.
00:14Maybe in CSS4.
00:16Just like that, I'll be able to update it
00:17to a different number if,
00:19so I won't have to edit the video.
00:20So in the future, there might be a way to create variables.
00:23Currently, there isn't.
00:25And how do we solve that problem?
00:27We don't.
00:28There's no way for us to create
00:29dynamic colors in CSS currently,
00:31although there is one variable
00:33and only one variable.
00:35It's called a currentColor.
00:36It is incredibly cool, incredibly useful.
00:39Not as useful as it could have been
00:41if we had complete control, complete variables
00:44but hopefully there will be in the future.
00:46For now, there's only currentColor.
00:47Let's see it in action.
00:48It is so cool.
00:50Very hard to figure out exactly how to work with it,
00:53so I'm throwing it back at you.
00:55Come up with a few good solutions.
00:57How would you use the currentColor?
01:00I would love to hear about that.
01:01So let's get started.
01:02Let's see what it is,
01:03and then we'll figure it out.
01:06What we're bound to learn right now
01:07is just incredibly powerful.
01:09We're talking about working with the currentColor.
01:11Now, I'm gonna go ahead and show it to you,
01:13but it is so powerful and so impressive.
01:16The currentColor enables us to basically
01:18take whatever the current color is
01:20and then use it in other places.
01:23For example, let's find a place
01:25where we might wanna set that color to be our color.
01:28How about we go to our footer
01:29and we set our footer color
01:30to be based on the current color.
01:34Or how about we make it instead of going
01:35directly to the color,
01:36how about we go to the background of the footer.
01:39So let me find the footer's background.
01:40Here we go.
01:41So background color, I'm gonna go ahead
01:43and just type your currentColor
01:46instead of actually putting inside a color.
01:49By doing that, if I go ahead and click on refresh,
01:52we'll see that now our background
01:53is going to be based on basically
01:55the color of the current color,
01:57which obviously does not work here
01:58because our current color is actually
02:00going to make our color disappear.
02:03So this does not really work really well for us.
02:05But what we could do is instead of that, for example,
02:08instead of that, I'm just gonna revert that
02:10back to what it was.
02:11I'm gonna go to line 40 in my CSS,
02:13where I'll find here basically my section and boxes.
02:16Those boxes are basically those floating boxes
02:18that we were working for when we were creating our CSS,
02:22our color manipulations.
02:24I'm gonna go ahead and change those color manipulations,
02:27and instead of that, well, and on top of that,
02:29what I wanna do is I'm gonna add here basically a border.
02:32So I'm gonna go ahead and set a border
02:34and I'm gonna set that border color,
02:37I'm gonna set that border color to be currentColor.
02:40And I'm gonna go ahead, just to style it a little bit.
02:42I'm gonna continue and set my border style,
02:45and I'll just set the style to be dotted.
02:48And let's also do a few more things.
02:50Let's set a border width.
02:53Let's set that width to be three pixels.
02:56I'm gonna go ahead and also use the CSS3 radius,
02:59border radius and border, border-radius,
03:04and I'm gonna set it to be,
03:05how about I'm gonna set it to be 25 pixels.
03:08And last but not least, I'm gonna go ahead
03:10and add a margin to our elements
03:12so they have a little bit of a margin
03:13as we're floating them to the left.
03:15I'm gonna go ahead and add here a margin
03:17towards the left as well of maybe 15 pixels.
03:21I'm gonna go ahead and save and then refresh my item
03:23and we'll see something really cool.
03:25The color itself of my border
03:28reflects the color of our copy.
03:30Not only that, if I go ahead and change the copy,
03:32automatically I'm gonna be changing
03:34the border color as well.
03:35Wickedly cool.
03:37Very, very awesome and very, very useful
03:39in a scenario where you wanna use that color
03:41in different combinations.
03:43Maybe in different places, you want it to be the background
03:45or you want to add a few things with it,
03:47re-echoing that same color over and over.
03:50Now that really opens the door to variables
03:52in general in CSS.
03:54Unfortunately, not supported currently in CSS3,
03:57but we're hoping maybe CSS4 will add
03:59some primitive variables, enabling us
04:02to basically duplicate colors
04:04and have basically a color theme above
04:06and then have it basically work throughout our CSS
04:09without needing to manually put colors all over.
04:11Imagine that in most sites wanna have only three, four,
04:14maybe five different colors.
04:15It'd be wonderful if we could set four or five
04:17different variables and then set them throughout our site.
04:21Unfortunately, we can't do that right now.
04:23We have to hard code those values,
04:24but that is probably what's gonna come in CSS4
04:28if I would have to imagine what would be the big thing
04:31that would be added from a functionality perspective
04:33'cause that would be massive
04:35and would make pages work a lot smoother
04:38in my perspective.
04:39It would make it easier to create
04:41really dynamic things.
04:42So that's it.
04:43That's it for this lecture.
04:44That's it for currentColor.
04:46Really exciting new things in CSS3,
04:49and we're gonna continue on in the next lecture.
