DEV Community

Cover image for Creating Pixel Art with CSS

Creating Pixel Art with CSS

Jacque Schrag on June 12, 2019

I have always enjoyed looking at and creating pixel art. Before online pixel makers were a thing, I used to spend hours making my own pixel art in ...
Collapse
 
ben profile image
Ben Halpern

Wow, I just learned so much about CSS 🀯

Collapse
 
jnschrag profile image
Jacque Schrag

Haha I hope it was useful information! I wondered if I was maybe going too much into the weeds, but Β―_(ツ)_/Β― It made me realize how much of this information I took for granted until it came time to write it down and I realized how many assumptions I was making about what people knew.

Collapse
 
john_horner1 profile image
john_horner • Edited

Nice! I'm not going to do the next-level thing but I'm sure someone is … write a script to create the SASS to create the CSS, a script which can be run on any image!

I did this a while ago, but it's the hard-grinding version of creating table cells with background colours:

johnhorner.info/apple/

I did it with Perl and ImageMagick but I bet there's a Python tool out there.

Collapse
 
ashleyjsheridan profile image
Ashley Sheridan

I actually did this myself with PHP as the generator: ashleysheridan.co.uk/blog/Single+D...

Modern browsers with enough resources handled it quite well, even at a 1Γ—1 pixel representation.

Collapse
 
johnhorner profile image
John Horner

Nice! I knew someone smart would have done this!

I’m on my phone right now so I can’t check your source.

  • Does it optimise by re-using colours in a CLUT (colour lookup table)?
  • Does it have a kind of RLE (run length encoding) where a stretch of two or more pixels the same colour are made into a single long element?

Those concepts are used to optimise the file size of GIFs. JPGs I don’t know. They’re more mysterious to me.

Thread Thread
 
ashleyjsheridan profile image
Ashley Sheridan

It doesn't have any optimization really, I might go back to it and update it, those are some good ideas.

Collapse
 
jnschrag profile image
Jacque Schrag

This is incredible! 27,000 lines of CSS for just the 4 examples on the page is nuts, although I believe it. Very cool!

Thread Thread
 
ashleyjsheridan profile image
Ashley Sheridan

Yes, I would absolutely not recommend using the technique to the degree I pushed it! My blog post was just about seeing how far I could push it. I did do a full 1Γ—1 example of the Mona Lisa shown there, but it was starting to slow the page down considerably, so I didn't make it part of the post. There is a link to the generator on github should you wish to try it out yourself though!

Collapse
 
jnschrag profile image
Jacque Schrag

Oh gosh πŸ˜‚ all I can think is the Inception meme β€œwe need to go deeper”. Although that would actually be really cool to be able to recreate any image via box-shadow. Although I wonder if you would hit a limit at how many box-shadow definitions you’re allowed to have before the browser can’t handle it anymore.

Collapse
 
john_horner1 profile image
john_horner

Only one way to find out!

Thread Thread
 
y6nh profile image
Hugh

I tried to find that out β€” though these are conventional blurry shadows, not pixel art: codepen.io/y6nH/pen/YRmVvZ (click with shift or ctrl to add larger numbers).

Thread Thread
 
jnschrag profile image
Jacque Schrag

That's amazing. I stopped after 1500ish, so I didn't find the limit, but that's truly incredible just how much the browser can handle. Although I did start to notice a slow down in how quickly it rendered starting around 800ish.

Collapse
 
laurieontech profile image
Laurie

This is so cool!

Collapse
 
jnschrag profile image
Jacque Schrag

Thank you!!

Collapse
 
powerc9000 profile image
Clay Murray

Damn this is cool as hell. Didn't even know pseudo element could have box shadows.

Collapse
 
jnschrag profile image
Jacque Schrag

Yes! Another fun fact for you: the box-shadow property can be animated. Which means you can make moving pixels like this: codepen.io/jschrag/pen/PrYrQE

But if you apply the box-shadow to a pseudo element & animate it, it actually becomes more performant in the browser. Here’s an article about that: alligator.io/css/transition-box-sh...

Collapse
 
anpos231 profile image
anpos231

From what I understand, this article is about creating an illusion of transforming box-shadow by changing it's opacity.
The problem with animating box-shadow is that it triggers repaints on every change.

Thread Thread
 
jnschrag profile image
Jacque Schrag

Good catch! Looks like I wasn't paying close enough attention. That said, if your animation has two states, one way to approach it would be to have two pseudo elements (::before for default state & ::after for 2nd state) with box-shadow applied and alternate the opacity on those.

Or just make a gif. :)

Thread Thread
 
gypsydave5 profile image
David Wickes

Or just make a gif. :)

🀣🀣🀣

Collapse
 
demaine profile image
Colin Demaine • Edited

The box-shadow technique is definitely a fun way to make pixel art with CSS. I like your approach in explaining the technique to make it simple and accessible. In comparison, my first attempt of using this technique was a bit extreme, to say the least: codepen.io/demaine/full/rRvdJZ

Collapse
 
jnschrag profile image
Jacque Schrag

I remember seeing this when you first created it! It’s so cool!! In another comment we were talking about the browser limits of using box-shadow for this kind of thing, so it’s great to see your piece and read how you addressed that issue.

Collapse
 
willsmart profile image
willsmart • Edited

Such a cool technique! Great post.

I noticed you're drawing a lot of transparent shadow pixels though, they're easily gotten rid of via something like:

Which might speed up rendering a bit (depending on the how many transparent pixels there are and if there are mysterious render optimisations in play).

Collapse
 
jnschrag profile image
Jacque Schrag

Yes, drawing the transparent box-shadows aren't necessary, so that's a great improvement! Thanks for sharing. :D

Collapse
 
rolandcsibrei profile image
Roland Csibrei • Edited

Cool approach even though it is incredibly insane to create pixel art with this technique. We need "crazy" developers like you to push the limits further and further. Thanks for sharing! Have a great day!

Collapse
 
johnhorner profile image
John Horner • Edited

I've been quietly obsessing over this topic ever since I read your article and I have written a Perl module to turn images into data structures.

This video shows an image being uploaded and rendered by two different methods, one with elements 1Γ—1 pixels in size, the other using your shadow method. They load very differently, as you can see.

The shadow method is quite a bit smaller in file size, though of course both are laughably huge compared to the original GIF itself.

I optimised a bit by using the most common colour as the background, so no "pixels" need to be rendered for that colour, and by creating classes for each colour with names as short as possible.

I'm going to do RLE (run-length encoding) soon but it's making my head hurt trying to figure out how to do it.

I used a GIF because the maximum colours is 256 as opposed to 16 million for JPG. The image in question is 128Γ—128.

Collapse
 
jnschrag profile image
Jacque Schrag

This is really cool! Until I posted and had some discussion in the comments, I'd never considered creating a tool to generate those different pixels programmatically. Optimizing by coloring the background is quite clever too!

Collapse
 
andreujuanc profile image
Juan C. Andreu

Is this pusheen but kitten? D:

Collapse
 
jnschrag profile image
Jacque Schrag

Kitten because it's small? If so, then yes!

Collapse
 
andreujuanc profile image
Juan C. Andreu

C:

Collapse
 
worksnakes profile image
Christopher Andert

I never thought to use box shadow multiple times on the same element. That's a nifty trick!

Collapse
 
jnschrag profile image
Jacque Schrag

Yeah! It's one way that you can create truly incredible CSS art like this codepen.io/ivorjetski/pen/xMJoYO

Collapse
 
jaymeedwards profile image
Jayme Edwards πŸƒπŸ’»

Super cute article (and artwork). Nice!

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks! Can’t claim credit for the original artwork though, I found it online :)

Collapse
 
jaymeedwards profile image
Jayme Edwards πŸƒπŸ’»

Oops, guess I missed that. πŸ€¦πŸ»β€β™‚οΈ

Collapse
 
mobidi profile image
Mobidi

Really nice exercice ! Thanks, I'll try to make some :D

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks! Would love to see what you come up with! :)

Collapse
 
mobidi profile image
Mobidi

Oh and just changing the shape of your orignal pixel with a border radius seems to make some really cool stuff happens.

Collapse
 
harrymckillen profile image
Harry McKillen

I've been doing this for a little while, but without that great little pixelize function. Which I can now "borrow"!

Collapse
 
jnschrag profile image
Jacque Schrag

Please do! Nobody should be writing that many box-shadow effects by hand πŸ˜‚

Collapse
 
harrymckillen profile image
Harry McKillen

In lieu of sitting down to figure out how to break the problem apart, like a luddite, I do it! :) You've saved me a lot of time.

Collapse
 
tailcall profile image
Maria Zaitseva

There must be a webpack loader for that!

Collapse
 
jnschrag profile image
Jacque Schrag

Haha I mean...there’s a webpack loader for everything else. πŸ˜‚

Collapse
 
anpos231 profile image
anpos231

WOW

Collapse
 
jsardev profile image
Jakub Sarnowski

This is amazing! :D Awesome work!

Collapse
 
daveskull81 profile image
dAVE Inden

This is awesome. A great example of the power of CSS and a preprocessor like SASS. Plus, pixel art is super cute and fun and makes for a great article topic in my opinion. Thanks for this article!

Collapse
 
jnschrag profile image
Jacque Schrag

Thank you!! Glad you enjoyed it. :)

Collapse
 
k_penguin_sato profile image
K-Sato

sick!!

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks!

Collapse
 
jaeheonjee profile image
Meeooow

Funny πŸ˜„! really good article!

Collapse
 
jnschrag profile image
Jacque Schrag

Haha thank you!!

Collapse
 
webdeasy profile image
webdeasy.de

Very nice! Good work πŸ˜‡

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks so much!

Collapse
 
jsvcycling profile image
Josh Vega

My mind literally just exploded with how awesome this is! Thanks for sharing!!

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks so much!

Collapse
 
aeiche profile image
Aaron Eiche

What a brilliant and creative use for box-shadow. This is absolutely delightful!

Collapse
 
alebiagini profile image
aleBiagini

Very Nice tut! Congratss

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks!!

Collapse
 
rixcy profile image
Rick Booth

So so good, I'll definitely be playing around with this when I get chance!

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks! Would love to see any pixels you end up creating!

Collapse
 
allison profile image
Allison Walker

Very impressive!

BTW, the link to your github page doesn't seem to be working.

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks!

Do you mean on my profile? If so, it's working fine for me. But if you're interested, it's github.com/jnschrag

Collapse
 
allison profile image
Allison Walker

You're welcome.

Yes it goes to jacqueschrag.com/. I get a 404. Just FYI.

Thread Thread
 
jnschrag profile image
Jacque Schrag

Oh, you mean my personal site. Yeah, that's currently offline. :) Thanks for letting me know!

Collapse
 
asfo profile image
Asfo

I did this when I was bored just to create something like this but in a simpler way :P

codepen.io/asfo/pen/QxWzVd

Collapse
 
wuz profile image
Conlin Durbin

This post is super awesome! Thanks for sharing!!!!

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks so much!

Collapse
 
oramirezperera profile image
orlando ramirez

Hey, excellent post! really like it!.

Collapse
 
celsobessa_90 profile image
Celso Bessa

That is clever and neat!

Collapse
 
jnschrag profile image
Jacque Schrag

Thank you!

Collapse
 
thebuffed profile image
Eric Davidson

Awesome! It's cool to get to a point technically that you can start recreating tools for your own use.

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks so much! I low-key wrote the article because I wanted to make the color switching tool at the end haha

Collapse
 
ananyaneogi profile image
Ananya Neogi

This is so cool! πŸ˜€
I've always loved playing with pixel art and have spent way too much time at Make 8-bit Art

Collapse
 
jnschrag profile image
Jacque Schrag

Thanks! There’s so many online tools for this it’s great. Although for whatever reason, I find using them more tedious, even though writing out the colors for all those cells wasn’t exactly quick lol

Collapse
 
washingtonsteven profile image
Steven Washington

This is amazing and adorable. Love it!

Collapse
 
jnschrag profile image
Jacque Schrag

Thank you!!

Collapse
 
unlimiter profile image
Unlimiter

Pretty smart. I like the idea!

Collapse
 
macskeptic profile image
β™₯ β˜•

that escalated quickly... here's a pixel, 2 pixels, 3 pixels, cat!

good stuff though. makes me want to go play with it : )

Collapse
 
jnschrag profile image
Jacque Schrag

That’s great! Hopefully you’ll be able to use this knowledge in a project. πŸ˜ƒ

Collapse
 
jessepinho profile image
Jesse Pinho

This is amazing πŸ˜‚πŸ‘

Collapse
 
sinayeganeh profile image
Sina yeganeh

nice! do you know how can i pixelize with css? (Is css have a filter for this?)