Syntax Video
The Impossible Avatar
Transcript
How do you make this avatar where the noggin of the head is sticking outside of the circle, right? And there's a little bit of box shadow behind it, but the bottom of the avatar is being clipped off by the the border radius of the circle that it's it's coming out of. This is something that I had to build. We're working on this thing called March Mad CSS. It's a CSS tournament with a whole bunch of other YouTubers and creators. And part of that is I was building out this this bracket here to be able to show everybody on it, right?
And I wanted the person's noggin to stick out and be like infinitely scalable and all of that good stuff. So I was thought, ah, that's pretty easy. And it turned out to be kind of tricky. I'm pretty proud of my solution. I also asked AI to do it. So we'll take a look at that. But I want to sort of just explain how it works and build it with you real quick here. So what we want to do is I'm going to start off just by making some base variables and styling.
Get that out of the way and then we'll look at the actual uh solution that we use to sort of mask off part of it. All right. All right. So, we're starting off here with a size variable. And I like to use that so that I can scale the thing up, right? Like if you want a really big one or if you want like a really small one, you can just scale that variable up and down, right? We'll leave it at 250 for now. Um, then I have a noggin variable, which is how far outside of that should it be? And I'm calculating that based on the size of 15% over the top. So, that way it can scale up and down. Um, then I'm calculating the height of the image with the noggin. So basically 100% plus or the size of it 250 plus the noggin and then finally we have a border width so it can scale up and down. The border is not always like a fixed value right. So we have our width we have our height which are fixed the border radius 50%. Um there's a border on it and some background color on it. Now that's great. Now this image here if we say this is width 100% but
also height 100%. Right? And then we go back here and then maybe we'll say like overflow hidden that we're we're kind of there, right? But we want this image to extend above it. So we have this variable here called height with noggin. And that [laughter] that that then stretches it. And if I don't want it to be stretched, you can use object fit cover on the image itself. And it will make sure that it's it's being the height that you want but not actually stretching it.
[snorts] Now, if I take this overflow hidden off the avatar itself, you'll see that the image is starting at the top and then and then going down. We want the opposite of that, right? So, what we'll do is we'll display flex and align items at the end of that. And what that does is it starts the image at the bottom of the avatar circle and then we'll size on up, which is exactly what we want. Good.
Now, the noggin are sticking out of it, right? But if we were to overflow hidden on those, you'll see obviously the noggin are getting cut off on the circle. So what we want to do here is we want to mask off the bottom of the image based on the circle, but not the actual border. Right? If we go back and look at right right here, the border goes all the way around. But the part of the image that would then overflow on the bottom here is going to be um masked off with there's kind of two ways we could do this, right? You can use a CSS mask or you can use CSS clip path. Um I use mask quite a bit for the MAD CSS website, right? You can see mask is can be done with alpha um transparency meaning that if some things are slightly um transparent they can still be masked partially and I like that a lot for like like taking [clears throat] a look at like the the sides of these cards I've masked off so I can kind of get this
like roughed up look right and then the text as well. I have this paper texture um that I'm masking off the image of the text. And then finally, I'm using a CSS mask to actually mask off um this circle here. I could also use a clip path here. That would totally work as well. Apparently, it's a little bit more performant, but um I'll show you why I'm using a mask. So, now when I'm working with mask images, one thing I like to do is instead of just straight going for the mask image, I like to use a background image to visualize where it is. And that's really helpful because background images and mask images have the exact same syntax. Um, so you can just build it out as a background image, visualize it, and then switch it over to a mask image. So, um, I will grab an after element and just wrap it around the image there. Then we're going to go ahead and build the background image.
Now, this is a little bit tricky. So, I'm going to pull this from um, the answer that I already have and I'll sort of step it through with you one by one. Right? So what I just did there is I stuck a mask size in there that is going to be half of the size because it's the radius in a radial gradient is is is based on the radius not the diameter. So I divided up by two and then I also subtracted the border width so it doesn't actually mask off the border width. Then we are working with a mask image. Now, basically, we need some way to programmatically create, if we take a look here, some circle that will go around here. And then we also need to make sure we include the top of the head. So, I essentially just need one circle to include the stuff in the circle. And then I also need like a little like a rectangle up here on the noggin to get it off. So, I have two
images that I've created here. If I change this from red to um what's another color? orange, right? You'll see that what I've done here is I created one rectangle that is 50% the height of our mask. And then I also created a secondary radial gradient, which is going to be the circle. And in these can, in my case, since I'm using an alpha mask, I'm going to be able to just use any color that I want. Um there's also luminosity masks which um depending on how far they are between black and white they will be um masked out according to that. Then we have a couple mask repeat properties and then the position of of where they're going to go. Now if I put in an opacity on that of like 0.7, you'll see that the those are the parts that we want to keep, right? We want to keep the orange square and we want to keep the blue
circle. Um, it's not perfectly lined up. I'm willing to bet that's my border. Yeah, there we go. So, now we have created this image, right? You can use you can actually use images for masks as well, but I've I've created the image with a square gradient and a um a circular gradient. And then what we want to do is instead of doing this, so we'll just go ahead and display none on that one there. We'll go down to our image and then apply all of those things. So instead of background image, it's mask image instead of background repeat, etc.
Right? And then finally, we set a mask mode of alpha. Boom. What that did there is it now it's taking anything that was within those drawn pixels there and it's going to then mask it off, which looks awesome. Um, and then one more thing we can do to to make it make it look a little bit nicer is we can go up to our avatar here. And I want like a little bit of drop shadow. And you can't just put like a like a box shadow on here, right? Because that's going to Oh, that kind of worked. No, that didn't work because the avatar is circular. And you'll notice that the noggin, the heads don't have a drop shadow. So, what we can do is instead of using a box shadow, you can use a CSS filter and a drop shadow on there. And then that also, I'm not sure if you can see that or not, but that also is including the hair and the head and stuff like that from it's actually applying a drop shadow to it,
which looks awesome. Um, and then finally, like, let me show you how how sweet this is, right? If we go to our avatar and then change the size to like like 150 or 50px or what 550 px, they always scale up and down accordingly, which is absolutely beautiful. Pretty nifty. I was pretty proud of that. I'm curious to see how other people might solve it. One idea that I had was maybe we just duplicate the images and then like like show the top, right? And then and then the second one you only show the bottom and then that one can have like a radius around it where you only um overflow hidden on the bottom. But I hate having to duplicate elements simply for styles. It it happens sometimes, but if if you can get away with not doing that, it's great. Um finally, I did ask AI to do it. Um let's take a look here.
So I gave Cloud Code this. Um, I gave it a screenshot and I gave it the image of myself and asked it to do that and it came up with um, this right here, which is quite honestly pretty good. I'm not not anti-AI. I used AI to write quite a bit of this code myself. Um, but not exactly perfect. I had to sort of step through it and describe it. And it did use the double um, image trick that we did. And then they also they also put a size variable in there. But you can see as the size is going up and down, it kind of gets goofed up a bit. It's not totally as scalable as the implementation that I have. So that was a really fun one. Hopefully you enjoyed that. Let me know what your solution would be. I'll put the code down in the comment below.
Quote it with credit and a link. Quoting Syntax