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

Syntax Video

We Recreated Space Invaders in CSS! CSS Battle

Chapter 1 00:00

Transcript

All right, CSS battle. We got three web devs. We need to recreate this UI or the Space Invaders with the least amount of HTML and CSS code. And a few ground rules. You can submit as many times as you want. And there is a 10-minute time limit. So, we might not all get to 100% cuz I mean, I can't even think how I'm going to do this in 10 minutes. The highest score and which in it takes into account the least number of characters in 10 minutes, that is the winner. All right, let's go. Okay, man. This is This is going to be tricky. All right, y'all.

This one's going to be tough. I have no idea what I'm going to do. I think like what I typically do with these is let's just target the body and turn that margin off cuz that's one thing I know we need to do. I I think the right solution here is to just make a bunch of divs or even better than divs paragraph tags because paragraph tags what it's a p so that's smaller. Okay, I learned that lesson from the last uh one of these. Each each one of these little spaceships could be a div. I think I'm going to do that. I'm going to go for closer match rather than shortest characters because I know I could recreate each one of these divs or each one of these spaceships like with a div, right? Like if I just uh target let's target every div. And actually we'll use a P tag. So we'll target the P tag. Um I'm going to set the background color.

I'm going to set the background of everything as that blue color. And then I'll override the background in each one of the P tags. So, I'm going to start off with a bunch of tags. I'm going to use the B tag for those and I'm going to put eight on the page. So, oh, you know what I should do? I should use You don't have to close list items. So, I'm going to use a unordered list. 1 2 3 4 5 6.

Grab my list items. Going to make the width 25%. Display block background red. It's not showing up at all. It's going Oh, it's cuz they don't they don't have height on them. 10px. Okay. Okay. Here we go. Here we go. Here we go. We're good. Now, the P tag at the bottom. Man, you could do classes. You could do inline styles. I guess inline styles are the fastest way for this P tag cuz I need after elements is really what it comes down to. So, I I hate that I'm using the style tag here, but we need after elements. I need to work fast. Um, so you know, let's start first and foremost with the space invader itself. What is a faster smaller attribute than class? It's an ID. So if I do ID of a pound a and we do background and we do this purple color, we could say this is height of 20 pixels. Let's do height of 15 pixels.

Okay, this is tough. Okay, 30 pixels. 40 50 pixels. Okay. 50 pixels, 70 pixels. Okay, that's it's close enough for now. I think what I'm also going to do is give some margin top to the body. Yeah, I'm just going to target the body directly on this. So, target the body and then I'll give it a margin top of 10, 20, 30. Basically, just to like push down that first ship. So, let's do like 35. Let's go 40. Yeah. So, that's perfect. So those the top of the ship is in the right spot. Now we'll also give it a give the whole body a margin left of uh let's say 30. That is perfect I believe. And we have body is display grid grid template columns repeat 4 1 fr

okay width 100% gap 5%. Okay. Oh, I need box sizing border box. Oh no, I don't. It's cuz of my gap. I'm panicking. Oh man, there's so many solutions you could do for this. You could do an unordered list container with list items. Um, and then you're positioning all of these at once, which woof, I think that actually might be a good idea. Uh, so we can do a ul with an LI inside of it. You don't need closing tags for li. That's going to be smaller, faster, better, stronger. And since I need a wrapper element anyways, the li's are all this. Okay. Uh, 70 is the right width of the overall container. I guess I could have a container for the ship. P tags will be our container. Can you use a random tag like C? So create create a C tag and then can I target the C tag to have a let's say height of 20 pixels? Maybe not. If it's is it if it's

a div, can I target it? We only got 7 minutes left. This is crazy. I don't even know if I'm going to get one ship on the screen. Do I not know how CSS works? What is going on here? We're gonna go with that. I don't know. Okay, so we have a div as the container and then a div as a child. I think maybe I could set the div div to a p tag. That's going to save us some characters. Yeah.

Okay, that at least works. Cool. So that has the height. Um, and in this case, I think we want the only want the height to be 10 pixels. Yeah. So that gives us that. Oh no. No. What am I doing? Display block content nothing. Width 10px. Height 10px. Uh, this needs to be position relative.

What's going on? Why can't I Oh, there's no background on it. Background inherit. It is there. Okay. Top 100%. Left transform translate X negative 100%. Okay. Top 100% is going to be less characters than than using a translate Y. And the ul, this ul is going to get a style tag. It's going to get list style none display of flex. And we're going to get a gap of something like 20 pixels. We're also going to say margin zero, padding zero. Jeez, not even close. Only 2 minutes left. This is This is not looking good for me. This is not looking good for me at all. Okay, so

that gives us our top piece. And then maybe we also want our ship bottom. So we'll create another div. Let's call it ship bottom. And then put only Oh, man. Oh man. Only two minutes left. All right. Put this all inside of a div. We'll call this the ship. And then this will be T and then B.

So top. Okay. So we have that and then the bottom we'll have like bottom left and then bottom right. BBR only a minute and a half. There's no way I have to at least get one ship on the screen. Uh now we'll change the background of these to inherit. All right. So the width of these. There we go. Okay. Okay. Now we're now we're working.

Padding 20 px. All right. What did I do? Okay. So, my general thought processes here is that I'm going to I mean, this is possibly dumb, but I'm going to have uh these these be essentially list items. We're going to have two different unordered lists. The other side parts, the other side bits are going to be the other side bits are are going to be um shoot, I can't even speak right now.

The other side bits are going to be uh before and afters. I need to align the items vertically, which is align content top align items start. No, get up higher. So, we'll say background color red for debugging right now. Okay, that's where they are. If I say left zero, they're here. Okay, so that's the afters. If I want befores as well, I can do befores as well. And this is going to be right 9gative -9. And then we're going to do on both of them, top nine pics. All right. So top nine picss on the rights. We'll do top nine picss on the lefts. And that gives us our general shape. Uh bottom left. Uh no margin left. Width 10 pixels. Beautiful. Okay. So we've got the bottom left and then the bottom right. These two need to be inside of a

div themselves. One minute left. I think I can get one ship on the screen. And then we'll give this a class of just B for bottom. Just say display flex and then justify content space between. Oh frick. It's the whole width. Let's use a span. I don't want to have to take it off. I can afford the characters. Going to win this anyway.

Okay. That even just putting that in there. Oh, look how close that is. And then these are going to be varsc. These are going to be varc. And then the easiest way to do this is to say hyphen c is now going to be this background color. And this should make all these yellow. 60 pixels 70 pixels. Okay, I have one ship. 25 seconds left. Yeah, only one ship. Let me remove these comments just so that doesn't count for our style. I want to create multiple, but it's Yeah, it's going to put them on top of each other. So I could create a container and then flexbox them. That's pretty close. I think I should increase my gap here. 15%. I think we need more time. Oh, woof. Okay. Display flex. Oh my gosh. I should have abstracted this out into a class. 5 seconds. I have to delete this code and just go with what I have. I have one ship. One ship in I think I'm out of time. Oh, it's up. Hey guys, we need a lot more time. Sorry. I only got a certain part of it done. I

didn't get any of it done. Oh, do we want to do another five? Well, but let's let's go with what is your what is what is your score right? If you submit right now, what is your score? I I'm at 516. Oh my gosh. I can't believe I'm beating you, Wes. I thought you guys would be so much better. I'm at three. I have three points, but I didn't wrap it up. I was in the middle. Yeah, I was hoping that we'd get more time. I didn't wrap it up.

I didn't Let's do five more minutes. We should wrap it up, but I'll say I have created one purple spaceship, and that got me 519 points. See? Yeah. I don't have Yeah. Yeah. I'm far along. I just don't have it in the uh Well, you have that already, Scott. Yeah, I'm telling you. And I got three points for this. So, I mean, like I said, I'm I'm almost there. We need an audience. My one ship is in the exact right spot it needs to be in. Oh, come on. Mine Mine is by far the the best. What'd you do wrong? Oh, maybe it's cuz I've used a lot of CSS.

Let's give us five more minutes. We do 10 more. 10. Let's do 10 more. Okay. 10 more. Okay. Oh, this is fun. I like this. All right. They they gave us 10 more minutes cuz CJ and and Scott absolutely suck at this. CJ said he he has like one little spaceship and he's beating me. So I think it's because he has way less code. Do we need display block on this? No. Okay, that's good. Okay. Um do we need position relative on this? Yes, we do. We were in a good spot, right?

Cuz we have at least one spaceship. So, what I was saying is if we create a container div uh ships, we can then throw a bunch of ships inside of there and then uh just space between them, right? So, we'll target ships and make it a flex box. Um and then I guess we could do justify content space between or space evenly and then just get it get get it the right width. And so, we have one ship, two ship, three ship, four ship. Now, this bottom one does not need a translate of 31 pixels and shouldn't have that. So, let's get rid of that.

Um, just to clear that up. And I had a margin top of 100. Let's clear that up to get it up to like um something way more reasonable. Let's do I'm getting there. 72 73 74. Okay, beautiful. All right, 7 minutes left. I think we can recreate the yellow part now cuz it's going to be exactly the same. So, I'm going to just copy paste this.

We're going to call this Y for yellow. Going to add another CSS variable Y, which is going to be that yellow color. We will give the same styling for P and Y. Um, however, Y is going to get a little bit of top margin. I think it's 30. Yeah, 30. I want to hide that. Actually, let's let's work on hiding that that one right there. So, anything Oh, can I just put an hidden on attribute on that? No. Well, then it gets rid of it in the CSS grid. I need a opacity zero on that. So, what if we say anything with X opacity zero and then we'll put an X on there. There we go. Okay, that got rid of it. I'm just curious about what everybody else is doing. I'm trying to make this smart and people could just be doing a whole bunch of divs and stuff.

We have one ship, two ship, three ship, four ship. Maybe I'm just too smart for this in a bad way. Now, we need to get rid of the second one. So, I'm just going to put an empty div right here. And hopefully that fixes it. It does. Oh. Uh, but it's not big enough. Oh, actually, I could just give it a class of S. That has the correct width. Yeah. And there's nothing inside of it. Beautiful. So, the first thing we're going to do is we're going to say we need a box that is width of, let's say, 40 pixels. I bet CJ's measuring this stuff. I bet he knows how to measure this stuff in a way that I don't. 50 80. I don't know if I'm going to be able to get this. Uh, try 150. So, it's XY.

So, 16%. No, no, no, no. Position absolute. Position absolute. It position absolute or fixed. And then I think we can do bottom zero. Yes. Beautiful. The next little bit on it, I'm going to do an amperand after colon after is going to be content. And content is going to look like that. It's going to be Man, I don't know if this is the best way to do this or not. I tend to over complicate things here. Uh either way, we're going to do a width of um 9 pixels. The color of this I I have so much wet code here. It It's bananas.

Also, where the heck is this thing display? Do I need a display block on this? I do. Where is I feel like if I get these Space Invaders overlapping perfectly, I'll have a really good chance at winning it. So, the the gap the first gap is the left and right. Is that true?

No, that's the vertical gap, and the second gap is the left and the right. Let's just create a Okay, 48 seconds left. I think we can create a queue here. Uh just like a width of five pixels and a height of five pixels. 30 seconds left. And then we'll give it that background color. 30 seconds left of blue. Just feverishly removed white space in the last couple minutes here. I don't even know if removing this white space even matters. Right. So we've got one Q. Uh five was too small. I think we need to go to 10 26 seconds left. Um we're going to make this a flex box and put it directly in the center. I couldn't get my darn spout going. Uh remove the background color. That is too high. Okay, time's up. I got 564. Um, my CSS approach was to use, um, which was kind of interesting. I used list items and uls for the ships specifically because you don't need to close the list items. So, uh, I had list items. I used IDs instead of classes, uh, because I

did need afters. Um, so I did the little the little squares on each of them with an after for all of them and a before. I just used divs on divs on divs. I I posted my solution and I and I beat you guys. I got 585 points and a 99.3% match. I got 528 was my highest. Yeah. 564. Divs, you just used a bunch of divs. You cheated it. Nice. That was fun. Wow. So, uh, CJ, we've learned that just using divs can work. I was trying to be fancy with CSS and sometimes you end up writing a whole lot of CSS. Divs on divs. I'm upset that like a lot of my like neat tricks like the attribute of X selector to hide something. Yes. All my cool tricks were were in vain and somewhere along the lines I I I added something and my score went down 30 points. I was like trying to do like I I what I what I was like I'm going to use list items in a UL cuz that's really short and I'm going to do this and do that and then next thing you know I'm like oh but now I got to remove all the default styling from list items and it's a total wash, you know. So, I would have

just been better off using uh paragraph tags or who knows what. I was like really watching the timer and also metagaming too because like the background is the majority of the image. So, as long as you have the background, you have like a high percentage, right? So, I think Wes, your giant rectangle right now, that's why your score is so low. If you I just did I just hit it and it went up to 572. So, like I was uh talking trash the whole time saying I bet CJ knows some tricks that we don't know here. I think the next one will be a little bit more even. But uh thank you so much for joining us for this CSS battle. And uh if you like this, let us know. We're going to be doing these.

These are a lot of fun. How did you solve this? Did you approach this differently than we did? Uh what was your score? And be honest, uh we we took what 20 minutes to do this. So you can set a timer for 20 minutes and see if you can beat our scores here. Thanks so much for watching and I'll see you in the next one. Peace.

Quote it with credit and a link. Quoting Syntax