Skip to main content
View on YouTube to comment and like!

Syntax Video

CSS Battles! Recreate an ALIEN EYE with CSS

Chapter 1 00:00

Transcript

We've got another rousing CSS battle for you today, folks. We're going to be creating an alien eye with CSS. We're going to be seeing who can write the smallest CSS and HTML to get us to the highest score possible. If you want to compete, this is number 136 in CSS battle.dev.

Set your own clock for 15 minutes. Hit go and see if you can beat us in that amount of time. No cheating. You guys ready? Ready, go. All right, here we go. I am going to Last time I used CSS radial gradients. Worked out for me. So, I'm going to right ahead go for a background image radial gradient circle blue.

Start at zero. Go for 200 px. And then we'll do transparent. The shortest way to do transparent is with the hex code with two things on it. And that is zero. Okay. So that's a full circle. I came to play. All right. BG color. BG color equals. Yes. Okay. So I'm pulling out all the stops here, folks. I'm going to do this with one P tag. All I need is one P.

Uh first things first, let's get the background. Let's remove all these extra styles and that and that. probably just have a div with nested, but we'll set the background color to this. I'm going to set everything's background to that color and also remove all the margin from everything. I'm going to be doing this entirely with one element. So, we're going to have a Yeah, I can do this with one element.

These are all circles. So, width is going to be equal to something small like 200 pix. Um, it's not even that small. Height is going to be something like 200 pix because these are going to be the same size. Border radius is going to be 50%, not 500%, and let's go ahead and just get rid of these picss off the start here. I do like writing CSS. By the way, the reason why I'm using HTML tag here is cuz I've always wanted to vertically center them, but the body is not by default 100% height.

So, instead of having to size the body, I'm going to just attack the tag. All right. So, the first one, background image, radial gradient. Um, now we're just going to do multiples of that. And then we need the dark yellow circle. So, that's going to be P tag here.

We will have some nested elements in there, but let's style this P tag. I'm going to give it an attribute of X so that I can target it. That is going to have a background that's this dark yellow color. It'll have, let's say, a width of 200, height of 200, and a border radius of I'm going to try this Q unit. I think I mean 50% would be the same amount.

That'll give us the circle. Okay, I'm not going to use the Q unit. I I learned about that in a comment, of course. Off the off the jump, nothing is showing up. Um, did I goof up border? Oh, I did. I I did goof up border rads. rad. Okay, but that doesn't Oh, I see it. I wrote a P inside of here for my attribute selector. That's why it didn't work. There we go. Obviously, I need this to be much smaller. Let's move it.

Move it. Move it. Move it. Let's move it. Move it. Like uh Oh, those little mischievous guys from Madagascar. Okay, so the second one is this guy. 110 pixels. Oh, I got to I got to start with the smallest one. I'm wrong here. Okay, so we're going to start with the smallest one.

And that will be this first color. This blue here. No, not the blue. It's going to be the gray. Gray. I'm going to fix position it in the exact spot. So, we'll make everything fixed position. And then this will have a top of 100 and a left of 100. Let's just get it in the exact right spot.

We'll do a top of let's say 50, 60, and then a left of 110 120. Okay. So, it's just a little too small. This thing very well may be in the center. That's really tough. It doesn't look like it is. So, we can do style is equal to align content center. Margin is going to be zero. Auto that. Oh, that. Sorry, that needs to be on the P.

All right, man. I've already wasted some time here. I need to get this ever so on here. And then we're going to do a translate 10 pix to move it over. Okay. Um, now that we can see that, we'll change this blue to that. Okay. Oh, I think that needs to go way smaller.

75. Okay, we'll fuss with that. And then there's two more here. And this third one is going to be the blue. And that will go 120. And then the fourth one is going to be yellow again. Maybe it is 180. A 180 circle. It is. Okay. So, that gets it exactly. Let me zoom in and out. Make sure that it's Yeah, that should be a perfect match for the the circle in the center. So, it's a 180 by 180 circle.

Then, inside the circle, I want uh that dark bluish green circle. So, I'm going to have another P tag inside of here. We'll give this an attribute of Y. I'm going to style it the same way as the other, but then we could just override the background. Holy guacamole. That put it in the right spot. Okay, now it's time to do background or uh let's do box shadow here. I'm going to do box shadow for literally everything simply because I can and I feel like that is the right strategy.

So, we're going to say 10 pixels. It's going to move 0 pixels vertically. It is going to be a spread. We're going to do zero. And we're going to say something like 10 pix for now. And it's going to be this color. And it's going to be 20 30 pixels. Okay. So, the spread is too or the I don't know if that's called the spread or not.

What? What? But what? What but what but what but what but what but what but what but what but what but what but what but what but what but what but what but what but what but what but what but what but what a tremendous guess that was. All right, we're in good shape. Um let's start sizing things down now. Let's start from the outside in. So I'm going to throw on diff 80 95. No six. Oh, these got to go way down. Okay, so we're going to do 35 45 here. Okay, good. First one. This one's got to be way bigger. 120. Man, this is this whole video is gonna be me just pushing pixels around.

Almost there. All I have to do is position this one into the right spot. It's top needs to go down a bit more. So, like 70 80. And then left needs to be a bit more. So, we'll do 140. And then it's definitely too big. So, let's bring it down to like 130. And then let's work on the diff. Let's just get it centered exactly on the top like we did with the last one. Bring it over a little bit more. Yeah. So, let's 146 145. So, that gets the top exactly where it needs to be. Then, let's make it 140 by 140. Much closer. And then bring it over a little bit from the left. Yes.

This next one is going to be let's say 40 pics like this. Okay. 40 pics might have been too much, but like maybe zero. I found the formula. I have found the formula. All right. So we have them all. Now we got to push them over. So my move here is going to be background position. You can use multiple background positions as long as you keep them in the order as you specified your background images. So the first one is the inside one. So X is going to be 10px0.

The second one is going to be the yellow. Oh, that actually looks bang on. Okay, so I'll keep that. Third one is the blue. Oh, it's not actually even moving. Bang on. It wasn't even rendering properly. And then the third one is zero.

Oh man, I'm almost there. Oh, is it because it's 50% border radius? Like if I were to do like 100%. Whoa, that sucks. How is this not creating a circle? How could that possibly be? That doesn't make any stinking sense because this, for all intents and purposes, is just about there in the border radius is now causing extreme discomfort. We're going to have to do this with another P tag, which sucks cuz this is adds so much more code to this. Uh maybe I can come back to this if I get this at least cooking a little bit here. Man, that sucks. Now, we need another yellow circle. We'll just do PZ and it's going to have all the same styling of the others. We'll just bring the width and the height down. So, this needs to be even smaller than 140. So, let's just do 100 by 100.

Top needs to be more than the blue circle. So, let's go 100 there. And then left. Try 100 there. 20 130 140 seems close. That's exact. I guessed it exactly. Okay. Um, and then let's get that last circle inside of there.

17. Okay. It's starting at the right spot, but it has to be much bigger. How much smaller? 70. Yes. Okay. Now, we're going to move it over to the left. 15. A little less. 12. That's looking pretty close. So, this will be another P. We'll give it an attribute of W. We'll style all of those in the same way. And then W has a width even smaller. So let's try like 50 and a height of 50. And it's going to have a background of this color. And then top will need to be even more. So we'll try 140, 130. And then left will be even more. So like 160, 170, 180. And I think the trick I learned here is like get the top of the circle aligned exactly and then you can find the width. So let's just get it

aligned exactly. Oh man, I hate this. I hate this because I was really cruising on something that was working and now I'm adding a whole another element here. Translate. We could say 50% 50% and then left 50% top 50%. And right now it's sitting I know I I'll tweak this in a second. Right now it's sitting um above everything, but if we move this here it will sit below. 99.8% match.

What's goofed up? Let's work on the yellow one. The yellow one is the third one. It needs to be slightly larger. Oh no, that's not the right one. The yellow is the one. Oh, the second one. 51 pixels. Okay. And then maybe -1 px. Oh, that made it worse. What does the What does that mean? Is if it's showing that means mine is too large.

And then bring it over a few more or a few less. Left 180. Okay. So, this looks exact. Let's submit and hope that it's not 99.999% match. 100%. Yes. Beautiful. Okay. So, we've got 100% match. Now, let's work on getting it as small as possible. The diff stuff is always tough because you can get so close with these diffs and especially if you're zoomed in. Okay, that's 100% match. So, I I very most definitely want to get this to be at a certain state where I can say this is the smallest best code. And we have about 6 minutes left. So, maybe I can find some units that'll bring the sizing down. Like I know I don't need this last curly brace. Actually, the only spot I'm using a unit is on the radius here. I don't know if I can get anything smaller. Let me just double check that I'm not missing any spaces anywhere.

99.8% match. 99.9% match. The biggest one is too big. 89. No, it went down. It was fine. If these three elements were children, then the position absolute potentially the top and the left values would be smaller cuz instead of making them fully fixed position, then their position just relative to their parent. I don't know if I have enough time to do that. So, there's only 5 minutes left. So, I'm actually just going to going to copy my current solution, but then try to get it working where all of these are nested elements with position absolute. I mean, that's a pretty high score. I'm going to copy and paste this right now just to say, hey, I can at least get one more character off of here. I can get rid of this semicolon there. Is there any other minifications I can do? I can get rid of this semicolon. I think I can get rid of this. That better be 100% or I'm quitting.

100% match. Good. Okay. All right. We're good. We're good. We're good. All right. So, now we got to figure out how to make this as small as possible. So, we can take the style tag off. Can I combine these two things? Why did all the colors just go weird?

Oh, I'm dipping it. Oh, there we go. That's That's why I can't do that. We need to turn P of X into a container. And then I think Well, that's going to be even more cuz I think I have to close all of these so that they become children. Everything else is going to have position absolute. Yeah, I don't I don't like this path cuz there's going to be so many more characters to have the closing tags and that's not going to be enough to save us. Let me just stare at this a little bit more. We have two and a half minutes left. I'm just going to stare at it. See if I can find any last minute optimizations. But otherwise, pretty happy with this. We have two box shadows that are the same color. Would it be shorter to say color is equal to this?

And then you could eliminate this. Could you? Let's undo. Undo. Undo. Undo. Undo. Can I leave off this curly bracket? Yes, I can. Beautiful. Okay. Okay. Looks like I can eliminate that space. And that's good. Can get rid of those. Get rid of those. This is like extreme optimization time here. I wish there was a better way to put transparent. You know, the word transparent. A CSS variable is technically one character less, but instantiating that variable is going to add, so I don't need that. Oh, there's a space right here.

Yeah, I couldn't find anything. I thought of potentially replacing 50% with another unit or like 180 with the percentage, but it's all three characters. So, this this is as short as I got. Let's see how the other boys did. Did I just find a way to remove six more characters? I don't know how I could have done this any better at this point. I'm annoyed.

Oh, I think time's up. Yeah. Yeah. How'd you guys do? I killed it with radial gradients again. Rad is my new move. Oh, that I think you may. Yeah, I think radial gradient might have been the move here. And I'm uh sad to hear that. All right, I got 624 and that's great.

We've always You remember during the live show we were trying to vertically center something in the body and we couldn't figure it out. It's a background and it spans the entire height, but the body element itself is not 100% height unless you explicitly add 100% to the HTML element and the body. So, I was like, screw the body.

We're we're we're styling the HTML tag. So, I used a bunch of radial gradients and then I use background position to just scooch each of them left and right. Yeah, that's awesome. Amazing. Yeah, I I do want to push back. You can't The reason why the align content didn't work in the live show was because of Firefox because I used align content on the body. No issues here. Filled 100%. No problem.

Can you uh explain radial gradients? I've never used them. What are the arguments? What's going on here? Basically, when you're sizing a radial gradient, you can tell it where to start and stop. So, you can say start at the furthest furthest corner and they will span from that. And then the trick that I'm using is that I'm using hard stops. So I'm having a circle radial gradient starts at zero goes for 50 pixels and then you immediately put a hard stop after it and then I've had to use a transparent. This is again this is the smallest possible way to represent transparent in CSS and then by putting the hard stop in there right if I didn't put that in there it would span the entire thing I guess. Actually I don't even know how that part works.

Yeah. So what am I looking at? So which circles did you draw in which order? Like what's the first one? You start by the center and go out. And I guess cuz they stack backwards. Now I I did it opposite the first way. I thought like you would you would stack the back one first, but you start with the opposite the one in the middle and go forward. Okay. Yeah. Awesome.

I did have to have two elements, which I should have only had to have one element. And so with a box shadow, you just did a box shadow and and scooch it over with the position. What I found is too that they were all multiples of uh 10 and then 20. So if I scooched over by 10 and then 20, the next one would be zero, then 40, and the next one is going to be 10 and 60. And that was like the the thing. So I figured that out really quickly. But the problem is, guys, check this out. This is insane to me. And I did not I don't know how to fix this. If I go the next step up, which is 60, and then I make it red, watch what happens to this box shadow. It's not a circle anymore. Yeah, it flattens. What's going on there?

Yeah. And so my only assumption is that it has to do with the fact that this div is 60x 60 cuz if I went to 80 by 80, it goes to being a circle. But then what I figured this out too late cuz what I would have done is I would have gone to 8080 uh scaled all the math and then used a CSS scale to to get it down to be the right size. And then I would have been at here for for sizewise.

The thing with border radius, I learned this when researching how squir the squirles work is that once it passes more than 50% of the greater of the width or height, then it just flips to it. It maxes out. So you think a box shadow is just going to make repeat the shape. This is bizarre. And I was just like I was like, I'm cruising. I'm going to finish this with 10 minutes left and get 100. And then it was like, what?

What is going on? It's just being clipped. But my high score was 618. Uh 5'9, 100% match. Uh really bummed out that I couldn't figure that out. Couldn't take really good though. It's nice when it's all 10 base, too, as well. You're not fussing with pixels here and there. But that would have been so short, Scott, if you didn't like you would have that entire last selector would been not have been on there.

Oh yeah, I know. Huge. Yeah, you would have killed me. Well, that's great for me because you got faster. We tied. We tied with a completely different approach. 322 characters. 1359. Wow. Yeah. So, exact same score as you. Yeah. Exact same score. But I just I just have position fixed with four circles. You position that yellow circle in the right spot and then the blue one and the other yellow one and then the olive one.

Wow. Yeah. 322 characters. Could you use inset instead of top right, bottom left? Dude, I will tell you what. Inset Wes does not work in this. I don't know why. I I used position fixed, inset, auto, auto on mine. Nothing. Inset 100 pixels, whatever. Nothing. Uh, every time I go to inset, it does not work in CSS battles.

So, you probably would have destroyed this if you could use inset. Oh, but then I probably have to use pixels. So, that makes it a little bit longer. Yeah. Oh, you have to use pixels with inset. You have to put at least some unit on there to get it to work. Cool. Nice. Wow, this was a this is a nailbiter. Very very close. Yeah, I didn't think you'd get anywhere close to the brevity of mine, but the the radial gradients is a lot to type.

A+. Yeah, everybody pulling a string of W's together. Just coming out from the I don't know any sports analogies, but all right. Of course, I won again. Radio gradients for the win. That's all I'm using from here on out. Uh, thanks for everyone for tuning in. If you want to watch us do any other ones, we have one right here. And I don't know if they still do it, but another one right here.

And uh thanks for tuning in. Comment below who you think did the best job, except in unless you're going to say CJ. And we'll catch you in the next one. Peace.

Quote it with credit and a link. Quoting Syntax