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

Syntax Video

CSS Battle - Re-create an Image with CSS in the Fewest Characters | Experts Compete in CSS Code Golf

Chapter 1 00:00

Transcript

welcome to CSS battles where Scott West and I are going to all attempt to recreate an image with CSS we're using CSS battle. deev and they have a point system where the fewest number of characters wins and we're going to go for this one with a 10minute time limit so we all have 10 minutes to recreate this image in the fewest number of characters possible I'm going to do it in two is everyone ready yeah I'm ready all right I'm about to stunt on these uh I would say fools but uh my homies I'm about to stun on them here because you don't need any of this stuff we're styling the body so we can just nuke all of that out and I'm doing this in one selector which should be fairly trivial I'm also going to do this with one property I just see four rectangles basically we could have four divs on the page um but this is shortest number of characters part of what people get wrong about this kind of stuff is they see four squares here and they say oh that's that's four divs um why do we need divs

we got a background here so we could use box Shadow like basically have a single div and then all the rest of them are all box Shadows we also could do a linear gradient I just don't know what's going to be less characters a box show or linear gradient I'm going to look this up on mdn so my immediate thought was let's use a linear gradient with some hard stops there man that's hilarious I don't know what the syntax is for a linear gradient and with background you can do a linear gradient in fact I'm going to do zero de uh for 0Β° it's starting at the uh bottom going to top so the way you want to do this is color goes from 0% then to that same color at let's say since this is a big one I'm going to say like 20% we'll adjust the percentages here to match but I'm just going to eyeball it and then again you take the next color which is this one and you say this color

from 20% and we're going to comma same color then go to something like uh I'm going to have to tweak all these numbers something like 30% let's get these colors in here now and then I think the idea is we've got to match it so 20% yes if you give a linear gradient the same stop as the previous one then you get a nice hard Edge seems like a lot of characters cuz a box Shadow is just x y and then colors okay I'm going to go with box Shadow so here's my thought we target a specific element we're going to go with a P tag I want this to be shortest character so I'm not going to close it out and then also we don't have to close out the style tag and then here we're going to Target the P tag I have to remove the margin from the body yeah I'm going to try to Target the body directly so if we do body then we actually don't have to specify an element yeah okay I think I need to have an element I'm I'm wasting so much time here just trying to figure out how we're going to attack this these are probably

going to need a lot of tweaking in fact I think the majority of time it's going to take me to do this is going to just be getting these percentages correct cuz I can already see percentage wise I'm I'm just super off and then we're going to say the last one then goes from 80% to comma and then 100% so if I wrote this linear gradient correctly okay I think we need like hard start and stop of the same color what if I duplicate this so I go from zero to 20 with the same color okay it looks like I forgot a comma here that's it that's it y'all okay so looks like I was um pretty far off on my initial percentages uh another thing is I don't need a 0% here cuz that assumes it if I'm trying to save uh code space here and let's get this one correct first 35%

33% ooh that looks pretty close I think maybe if I Target everything and say margin zero yeah then that that's less less characters A P tag has 100% width by default great and then I want to be able to give it a height and then use box Shadow to create the the rectangles underneath let's try with a height of 100% um and then this one is just going to have a background that's yellow but then my thought is uh if we set a negative margin that'll push this one above what's visible so if I do something like margin -200 pixels yeah okay so this this is my thought you can see how we're that yellow Square there is getting moved up so let's go -250 pixels that looks like the perfect height okay okay we're starting to get it we'll figure out the stops and once we're done and this goes to 50 and then this one starts at 50 goes to 100 something broke

comma there we go okay um now let's let's figure out the matching I mean I think you do have to do percentages I don't think you can do like 100 pixels I just said that I did 100 pixels and it looks way better than what I had typically I would do this in percentages but the percentages in this particular case are obnoxious uh always getting into 33.33% or any of that stuff like no thanks 15 % maybe 177% 16% 16 and a half this is kind of annoying that I have to update both of them 16.75 that's exactly it okay you can see I'm removing space as I go to to make it short as possible so there needs to be a space there now we go with the Box Shadow so our first box Shadow is going to have no X offset but a y offset I

believe it's zero for x and then let's say like 20 pixel in the Y yes okay that's the way to go and then we want that box Shadow to be orange we got 5 minutes left on the timer I'm I'm probably only going to be able to solve it this way okay so how big is that 50 pixels was a good guess yeah so that looks good I'm curious do we need the space between the PX and the hash we don't that's great and then you can specify multiple box Shadows so we'll do a comma and then we want the next one to be like 200 pixels and we'll make it purple sh yep uh 200 200 was too much let's go with 100 150 yeah 150 is the way to go okay so then the next one starts at 200 pixels and goes from 200 pixels and we'll say 250 pixels oh my word y'all I think I got this so we need 250 picks and then

like that is 250 pixels shorter than something like 80% which also kind of worked all I want to say though is damn I uh I crushed this um and then the next one needs to be a bit larger so is there multiple cursors there is so 32% 33% 33.75 it's a bit too much there we go 33.5 good and the next one is let's go to 70 that's too much 60 not enough 65 66 6 67 66.5 66.7 five that looks exactly it and then we have one more box Shadow that's zero and then uh let's try 200 that color 250 should be fine because it

overflows this is my answer all right so we have 127 characters is there anything I can remove to make it smaller so this is maybe as small as I could get this now I'm going to look for micro optimizations here like if you just if you omit the degrees I think this breaks oh it defaults the top down okay that changes things shoot all right shoot shoot shoot shoot shoot cuz now I don't have that much time okay but now I'm curious it has to be as small as possible so what can we remove obviously don't need this div let many characters is this does it tell me no it doesn't how much time do I have left 2 minutes goes pretty quick pretty quick the white space count is it better to oh yeah what's can I do this in line do I need to remove

this margin or I I guess I don't need it I I I was able to specify without that so I think we're as small as possible let's see what score we have right now we have 100% match that's good like we're it's a one toone match we have a 722.6 score I'm curious do I need this closing parentheses on the CSS or sorry the closing curly brace that increased it by like one point okay now it's time to optimize folks since we're going from top down which I should have known honestly let's just start from scratch here all right top down then goes to the same number at let's say 50 pixels then we have a comma and then we have the next one which is our Orange and we can say orange which then uh starts at 50 pixels and then goes for another 50 pixels or 100 pixels okay and then this one starts at 100 pixels and goes to 200 pixels perfect oh my gosh crushing this

okay and then this last one starts at 200 pixels and I think that's all I need folks look at that micro optimization I barely tweaked this but I flip-flopped it I got rid of the degrees I got rid of a couple of these definitions so I didn't have to say zero pixels what if I do body style equals is that less code I think so okay now let's get rid of the spaces in here oh those are necessary I guess the spaces after the commas are not necessary okay the space is not needed let's see I Pro I don't need that semicolon there okay that should give us like one or two more points yeah so 725 I feel good about this I just know that Scott and Wes are like really good at CSS they probably know so many more tricks that I

don't know but they are new to code golfing with CSS and as far as I know that looks pretty effing perfect I would like to see either of them try to have a better solution than this I would imagine CJ's got like four divs going um I could see Wes also using a linear gradient but is there any smaller possible way I can have this defined I'm going to say absolutely not I think I crush this but I'm just going to use these last couple minutes to kick back and nail that all right I'm going submit woohoo your stats 100% match and 69 2.17 points all right I'm done 30 seconds left I'm thinking now do I even need the pixels here on margin oh man I just heard CJ saying he's adding margin

I'm going to win this so good I don't oh that's better okay that's going to give me at least one more Point all right that's got to be it there's no way they beat this I think I won I'm going to say that right now what' you get I got 100% Ma match uh my my score was 60135 you're very confident over there how about us all right I got I got a 100% match um 692 high score what I cannot believe that I won this what I look I just I sent a screenshot I got a 100% match with a 72745 high score so uh what did you guys use I used box Shadows you guys oh box Shadow I use a linear gradient I how did you do a smaller linear gradient than me you removed white space and stuff oh wait was I supposed to remove the comments yes yes oh can I read the comments way to read yeah I'll I'll allow it yeah so this is our this is our first time doing this together so yeah

remove the comment I won't let you remove any more Whit space than that but absolutely remove the comments oh man I think my gradient is going to be smaller than Wes's I optimize the [ __ ] out of my gradient 687 after removing white space still not as good as mine I did mine on an inline style tag and I put the body without a closing body tag I got killed and I should have used a tech Shadow like CJ did is with a linear gradient hard stops you need a start and a stop so you're duplicating them all but you here here's why my gradient is better than yours West I I would have had a higher score if I would if I would have done the inline style because you don't need a start or end hard stop and I went pixels which are smaller so yeah I omitted the start and end pixs are not smaller percentage one character no you got 16.75% is five characters uh where or that's six characters where mine is what

100 picks yeah I apply your cheesy optimizations and I think I I win yeah so let me walk you through some of my optimizations I didn't close the P tag for one that saves you like four characters or whatever else I don't even have a tag well yeah I mean I bounced between like trying to Target the body or not I didn't close the final CSS rule there's no curly brace there and there's no closing to the style tag the browser just adds that automatically MH and I removed all the spaces I could between the colons and the semicolons and the last style of every rule does not have a semicolon man the Box Shadow is definitely the way to go cuz I resubmitted mine with Wes's optimizations and I got to 700 so that's as good as linear gradient can be cuz I I I was talking a lot of [ __ ] during what I was recording but winner all right good thank you so much for watching obviously I am the best at this but if you want to see us do more or uh compete in web dev trivia definitely check out this video here and uh we have tons of other cool stuff on

the channel so subscribe

Quote it with credit and a link. Quoting Syntax