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

Syntax Video

Coding Snake in CSS?! Kevin Powell Takes the Challenge

Chapter 1 00:00

Transcript

Welcome to CSS Battles, where we all try to solve this problem in the fewest number of characters possible. We're going to have a 15-minute timer. And today we have a special guest, the king of CSS himself, Kevin Powell. Welcome on, Kevin. Yeah, thank you so much for having me. I'm looking forward to it, but I'm also really nervous. Of course, we all are. Me, too. All right. I'm not doing three, two, one. I'm going to start right now. Okay. So, what I've learned last time is I'm just going to I'm not going to worry about length too much. I honestly think I might do just a single yellow square and then maybe use box shadow to repeat it a bunch. This is in in the website itself where you could do this trick. Look at that. I don't even I don't think I need the closing tag. There we go. This weird obscure BG color property on the body is faster than putting a body block here. So, I I think what I'm going to do for this is just try and get it to work as close as

possible and then worry about making it short. This one's kind of tricky. Let's just do div.g grid. We're going to do a grid. We don't have emit uh div with a class of g for grid. And I guess that should be an id cuz we're focused on making this short, right? What's the short? I see. Here I go. Immediately I'm like, oh, I got to make use the shortest element. I'm just going to use the span.

I can switch the elements later. All right. So, 1 2 3 4 5 6 7 8. This This could work as a CSS grid as well, but I feel like it's going to be a lot of code to place these with all of the grid. I'm just going to go with like a single square that just gets repeated a bunch with with box shadow. I think that's the way to go. All right, let's get this going. So, let's style the background.

Remove the margin. So, background is that red color. And then let's remove the margin on everything. And let's just have a P tag. And we'll make our little white circle. and then get that placed in the ex exact spot and then work on the yellow squares. I'm going to use a custom uh HTML element here named C.

Reason being C I'm just going to say C for circle and C going to have a background of white or FFF and we're going to do a display grid and a place place items of center. Uh, let's just say that my ID of G has a background of green for now so I can see it. We'll give it a width of 150 pixels and a height of 100 pixels so we can see it. And I basically want it to be the size of what we're doing there. So this is going to be 150, 130, 120. Okay, so that's one row. So one, two, three, four, five. Okay, five rows.

This is so much HTML, but that's fine. Uh, so this little attribute selector makes it so that you don't have to add a class name. It's way less characters. Um, this looks like it's going to be to be need to be a little bit bigger. Let's do like 20. And then we'll give it a border radius 100%. There is our circle.

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 children. I've made a mistake of using inline elements in the past with this, but I want it to be short. And I think because they're grid items, it will be okay. 3 4 5 6 7 8 9 10 11 12 13 14 15. I forget how many I needed. 1 2 3. Why can't I use C here? I'm going to use C. I'm going to try this again even though it failed the first time.

Somebody who is good at this stuff needs to explain to me why this is not working for this one. I guess I could put all these things in line instead of styling on the P. If this is a div, then let's just have this be a div for now. Div. Okay, whatever. Now, I'm going to take my grid, grab every single item and say background, uh, what is the yellow? And that's this width 10px, height 10px, and then the body is going to be this color here.

Background. Okay. So, we're just going to make uh everything in this challenge uh position fixed. Uh so, top let's do 100 pixels. A little too far. 90 pixels. And then we'll do uh the left 40 3050 90 as well. See how we're doing. I can't believe I got that exactly right.

So, we've got ourselves uh a nice little circle. And if we submit, I I bet we're we're doing pretty good on percentage wise. Uh width of Oh, I don't need pixels, right? I can take that off. Perfect. Width of 10 and height. I was going to see if aspect ratio, but I think that would be more characters. A height of 10. Oh, I need a display grid on here. Display grid.

There they are. They're definitely not big enough. Let's try 20. That looks pretty good. They definitely have a border radius. I wonder if somebody could do this with a gradient. People do crazy things. Why is that not working? Div with the class of grid. Oh, cuz we have to display grid on it, obviously. Okay. Um, we'll do a gap of 10px. Okay. Uh, the border radius for this is not going to be 50%, it's going to be something more like 5 pixels. Man, that was a good guess. On the body itself, I'm also going to say style align content center justify content center. I don't know if you can Can you do that without having the quotes here? I guess that Oh, you can't just justify content. What was I thinking? So, let's get that little yellow box on there. And this will be another P tag. We'll give it a Y attribute. We also don't need the

closing tag there. Yeah. So now I have I have P of X. Let's also have P of Y. This will have a background of that yellow color. Uh let's go to my grid. 1 2 3 4 5 6 7 8 9. So that's grid template columns. This feels long. Repeat 9 1 fr.

Why are they gapping already? I did this once before and I don't remember what caused Oh, because they have a size on them. I think I'm going to take the size off. And then we're just going to say that we have a gap like five pixels. And we don't need that. Um, we just need to say five, right? Oh, I need pixels on gap. Look at that. Screw nesting display block. Okay.

Um, no. I'm going to get rid of these rows entirely. That's looking a bit better. Um, border radius is 5 px. Man, I wish I could do this with a dotted border or something. I bet you could in some sort of way. Shoot, we're running out of time. I'm going to do something kind of funny here. I'm going to do a box shadow where I repeat this same shape. 30 pix 30 pics. Yes, at this point it makes sense to make this color a variable. So, C var C. I believe that's less characters.

1 2 3 4 5 6 7. Ah, is it less characters? It looks like the width and height are exactly the same. So I think we can actually move that selector up. But if we get it in the right spot, then we'll know that the width and height is good. So we know that the top is going to be 90. And then left is going to be more than that. So like 200 190 190 is exact. Okay. 1 2 3 4 5 repeat 5 1. That are they the right size? Let's just put my diff on. Oh, they're bang on.

Perfect. Okay, so now though, how can I do this in a good way? I'm also I This would probably be less characters. So, this one ID is equal to T for two. I should probably just do a um cuz I'm going to have several of them. So, this Yeah, that would be a lot less if I just did an A on there. And then we said grid column of 1 2 4 five six no five looks good. Okay. Now it's just a matter of adding all of these uh border rad border box all these box shadows to repeat it multiple times.

First box shadow we'll have a x offset of let's say uh total width is uh 20 plus probably like 5 pixels. So, let's try 25. Uh, zero in the Y. Yes. And then this needs to be the same red color. And do I need pixels on there?

Yeah, I need pixels on the first one. Um, and was that enough? I can't believe I'm just spot on guessing these. So, it's it's an offset of five pixels between each one. So, let's do another one. And then that one's going to be 50 pixels. This is too too big of a gap. So, 25. There we go.

And what I'm going to do is I'm going to just repeat this. We're going to say 45. Uh, no, it's going to be 50. Okay. So, that's the math here. So, every other one is going to be 75. I need to get this stuff lined up correctly. Oh, I can do the diff like this.

100 90. Okay, good. Oh, that's my problem. I don't even need to set the width or the height to 10. No. 215. Okay. I can't imagine this is the right way to do this. ID is equal to I almost feel what if we're going to try something else. Let's do a div. Can be anything. Let's do a B. 1 2 3. That's my middle ones that are going to run down the side.

B is going to be a display grid gap inherit grid template rows. I don't like this. But uh no, wait. What if I just did that and then I say grid row of 2 over 4 5. Somebody suggested this to us in one of the comments and I've never used this property which is webkit box reflect. And this is going to be above. And that does work with the box shadow. Yes. So, we've got those. Now, we need one more, which is going to be 75 + 25, which this one should be 100.

Yes. Rock and roll. Starting to look pretty pretty close. If I do above like 100 pixels. Oh my dear god. Yes. These guys are toast. I'm going to tell you right now, they are burnt toast. I won't. Okay, I'm not going to do Ludicrous here. How many Ludicrous fans out here? How many Lud Ludicrous fans in the house? Display grid grid template columns.

Uh, repeat. We did nine, right? One fr. Whoops. Oh, man. 6 minutes left. Uh, grid row five. Grid column 1 over1. Gap inherit. I did need that extra one. Oh man, we're getting there, though. This worked better than I thought it would. So, we've we've got the top line. 6 minutes left. I am so surprised at how well I'm doing here. And our our solution is really short. I think in 6 minutes we can repeat the rest of these. Okay, so this next one's going to have the same X offset, but then it needs a Y offset of 25. And then we'll need another one with an X offset or sorry, a Y offset of 50.

Yep. So, I got to get this whole thing centered, which means I should probably um you know, this is crazy. I'm just going to do a translate on here. 5 minutes. Ah, okay. So, now let's start hiding and showing some of these. So, and first child, it's going to be background white or no, it's fff border radius 50%. Okay, now I just need all of the ones on the bottom. All of these are going to have a y offset of 100 pixels, but now we start reducing the x offset.

So, this is going to be 75. It's funny how you need pixels on some things here, but not everything. Okay. And then I can take that green background off. Where's my green [Music] background? Where's my Oh, there it is. Okay. Green. Submit. And I think I'm bang on. So, it should be 100%. But I'm sure 100% match score of 600. What I'm doing here is I am basically just going to create this box shadow that Oh my god, I have four minutes. Oh no. Uh, shoot. Okay, now we need to figure out uh what we can use instead of pixels. 25. Yeah. So, 25 VW, that's four characters versus 1 2 3 4 5 characters.

So, 25 VW, I'm just going to go with that cuz we have three and a half minutes left. So, everywhere I have 100 pixels, I'm going to use 25 VW. Yeah, they have uh command D multicursor select. So, we'll use 25 VW. That gets us down to 465 characters. I need to start hiding a whole bunch of these things. So I want to grab let's try to do some nth child. Um nth child 2n plus tilda star background black all of the items that are nine forward none. This is not a good idea. And li might be the wrong choice.

What about what's something that's two letters that has no styling by default? Uh M close M and then this would be my M. Ah, there we go. Okay. Uh I'm surpris actually. And then I don't even need that selector. I'm so confused why I don't need that selector. But I'm going to That's good. Now I'm doing this more where I'm going to do it up. So - 255 pixels and then one more which is going to be -50 pixels I believe. Boom. 200 pix. This is crazy solution here. Oh yes. I'm you know it. Uh uh. Sorry, beat that out. This is so dumb. I'm just translating this thing all over the place. Can we reduce anything else? And do I have any extra spaces or anything crazy? So, I don't need semicolons on the end. I don't need this last curly brace that's going to save us a character. There must be some other optimizations. I'm still just getting 600 for my score. That didn't do anything for me. Okay, I don't know how to optimize this. So, 25 /

4 is

four. I don't know. Um, maybe it was this thing that was screwing it up. Ah, that's what it was. I had the same selector and the the cascade mucked up the order of them. It's looking pretty good. Okay, let's let's do quickly now. Um, get rid of this. The span should be an an I. And then all of these spans be eye tags. Okay, it's not bad.

Ah, it's not perfect. Oh, I got to hide these first bunches as well. Okay, good. And let's move the P after so that way the P is on top. And I guess that changes the whole dang thing that I was trying to do. My dear lord, sweet sassy molassie here. Okay, let's fix these values. Let's fix fix.

Yeah, a minute 40 seconds left. Let me just stare at this and see if there's anything else I can try to reduce. Let's just try and minify my code as much as possible. 2N red until Oh, man. What's the How do you stop it? It's the second one until You know what? I'm just going to brute force it. That doesn't work.

Shoot. Uh 50 seconds. Three. No, I think this is it. I'm sure the other dudes I honestly I think the other dudes might have uh over complicated it like or tried to do like a CSS grid. Okay, here's uh here's a fix. Box shadow. I don't know. I'm pretty happy with this. I'm sticking with it. That's looking pretty good, I think. Okay, how do we make this much faster? Get rid of the spaces. 27 seconds. Minifying sucks. This is what I don't like about these challenges. The box shadow is going to be 0 0 uh 0 10 pix. 25 seconds. Ah, what about taking all this out? Oh my goodness.

Give me the score. The time's almost up. 5 seconds. I'm not going to win. I'm not going to win the shortest, but ah, it's pretty close on there. Yes. Woo. How'd you guys do? I'll go first. Okay. Yeah, let's do that. Nice. So, I used a grid. I just placed everything where I got it, which is way too many. Like, I was thinking like maybe I could I could do repeating some sort of element, but I was like, you know what? I'm just going to do grid. I know that. Um, and then to turn them off, I was trying to remember what the syntax was for like ranges. Yeah. Um, you know how you can like end child, you start at one and then you like tilda.

So, I figured out the like tilda forward. Wow. But then I could not remember how to like stop it. Wow. Which is crazy cuz I have a video on it but I I totally forget. 1100. Yeah. Pretty good. Pretty good. Yeah, that's sick. All right, Kevin. Yeah, I need to I'm dying here. I actually took a super similar approach. Oh, okay. Wow. 600% match.

So, yeah, I did the same exact same grid as you. I just used 1FR though. I set like the parent to be the right size in the middle. Um, and then at first I'd set the same thing, 20 pixel size on them. But then I've made that mistake before where I couldn't get the size to be good. So, I didn't set the size. I just used the gap and the gap gave me the right spacing there. It was such a mistake. Like when you're using grid, you shouldn't be explicitly sizing your items unless you unless there's one of them that you need to, right? And then I was going to take the same approach you did of trying to get like these to line up all the way on the side. And I was just like I was going nuts trying to like I was same thing. I'm like this is I didn't even I was like trying to think of how am I going to make a selector that's going to do this and I couldn't.

So I just made this B is just an extra grid. So, it's just creating that one column there with the three items in it. Yeah. Nice. Nice. I should have just done like the color should have just been different elements. Yeah. I I treated each sort of as three different parts and then it worked in the end. I was glad that it did cuz I was a bit worried about that.

You beat me by 20. Nice. Wow. CJ. Well, I don't want to disappoint you cuz I'm pretty sure I'm going to beat you. So, Okay. Okay. Here we here we go. Oh, yeah. I didn't have time to uh to optimize mine. I optimized as I went. I did exact positioning with box shadow. So, the the little white circle was fixed position to where it needed to go. And then I have one yellow square and then I have a box shadow of that yellow square for every single other one. And damn it. And then I had no pixels. Exactly. So I I had 4 minutes left and I was like, how can I get this down lower? And so I had 100 pixels everywhere you see 25VW was 100 px. So I was like, how can I get that down? And then 25 vw was the same as 100px. And uh I learned this from you, Wes, last time. The like the attribute selector like the X and the Y.

Yeah. So that makes it so you don't have to use like a class or an ID. I am I'm very very proud of this. Yeah. I used uh box shadow like you did, but I was able to cut my box shadow in half by using WebKit box reflect. Whoa. What? That's the trick. And so and so that that like so like I have a bunch of extraneous one I didn't optimize. I left in position relative here that was unnecessary. I end up at the at the last second just uh translating a bunch of [ __ ] to get it to line up. I I was like talking too much about uh the band Crazy Town during my thing and I was like then I looked at the clock and I was like oh 4 minutes left. Oh [ __ ] Wait wait wait. What does box reflect do? Yeah tell me about that.

What is what is that? And how did you did you Google it? No, no, no, no. Some somebody left a comment on our very first uh CSS battle saying I would have used WebKit box reflect and and this was like a month ago and I was like what the is WebKit box reflect. So I looked it up. Yeah, it's just reflecting it. So what I did is I made the box which is just the L shape. So it went uh using box shadow here to here to here to here and then up. Yeah. Scott's used to the L shape. Yeah. No, I'm used to the W shape. I've won more of these than you have. And can you comment Can you comment out line 21? I just want to see what it is without the reflect. Yeah.

Oh, and I'm just reflecting it. And then what I did on this white one is I did a box shadow on the P tag here to cover it up. Yeah. Smart. Sick. I would have loved to try to optimize this. Yeah. If only you had the time, but you didn't. I know. Well, I got my ass whooped again.

That was really fun. Appreciate Kevin coming on. We're going to have another one that we put on Kevin's channel, so we'll link it down below. [Music]

Quote it with credit and a link. Quoting Syntax