惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
L
LINUX DO - 热门话题
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
爱范儿
爱范儿
D
DataBreaches.Net
Simon Willison's Weblog
Simon Willison's Weblog
S
Secure Thoughts
S
SegmentFault 最新的问题
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
P
Proofpoint News Feed
The Hacker News
The Hacker News
T
ThreatConnect
N
News and Events Feed by Topic
T
Threatpost
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - Franky
Recorded Future
Recorded Future
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
F
Future of Privacy Forum
F
Full Disclosure
Cyberwarzone
Cyberwarzone
J
Java Code Geeks
李成银的技术随笔
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
H
Hacker News: Front Page
人人都是产品经理
人人都是产品经理
博客园_首页
Scott Helme
Scott Helme
Google DeepMind News
Google DeepMind News
美团技术团队
Malwarebytes
Malwarebytes
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
T
The Exploit Database - CXSecurity.com
G
GRAHAM CLULEY
Recent Announcements
Recent Announcements
C
CXSECURITY Database RSS Feed - CXSecurity.com

CSS-Tricks

Revealing Text With CSS letter-spacing | CSS-Tricks Technical Writing in the AI Age | CSS-Tricks Cross-Document View Transitions: Scaling Across Hundreds of Elements | CSS-Tricks Cross-Document View Transitions: Scaling Across Hundreds of Elements | CSS-Tricks The State of CSS Centering in 2026 | CSS-Tricks Stack Overflow: When We Stop Asking | CSS-Tricks Cross-Document View Transitions: The Gotchas Nobody Mentions | CSS-Tricks What’s !important #11: 3D Voxel Scenes, Flying Focus, CSS Syntaxes, and More | CSS-Tricks Computing and Displaying Discounted Prices in CSS | CSS-Tricks rotateX() | CSS-Tricks rotateY() | CSS-Tricks rotateZ() | CSS-Tricks rotate() | CSS-Tricks Soon We Can Finally Banish JavaScript to the ShadowRealm | CSS-Tricks Using CSS corner-shape For Folded Corners | CSS-Tricks A Scrollytelling Gift for Mum on Mother’s Day 2026 | CSS-Tricks Google’s Prompt API | CSS-Tricks Making Zigzag CSS Layouts With a Grid + Transform Trick | CSS-Tricks Fixed-Height Cards: More Fragile Than They Look | CSS-Tricks What’s !important #10: HTML-in-Canvas, Hex Maps, E-ink Optimization, and More | CSS-Tricks The Importance of Native Randomness in CSS | CSS-Tricks contrast() | CSS-Tricks contrast-color() | CSS-Tricks Let’s Use the Nonexistent ::nth-letter Selector Now | CSS-Tricks Quick Hit #126 Recreating Apple’s Vision Pro Animation in CSS | CSS-Tricks Quick Hit #125 Enhancing Astro With a Markdown Component | CSS-Tricks Quick Hit #124 Markdown + Astro = ❤️ | CSS-Tricks Quick Hit #123 What’s !important #9: clip-path Jigsaws, View Transitions Toolkit, Name-only Containers, and More | CSS-Tricks A Well-Designed JavaScript Module System is Your First Architecture Decision | CSS-Tricks hypot() | CSS-Tricks The Radio State Machine | CSS-Tricks 7 View Transitions Recipes to Try | CSS-Tricks Quick Hit #122 Quick Hit #121 Selecting a Date Range in CSS | CSS-Tricks saturate() | CSS-Tricks justify-self | CSS-Tricks Quick Hit #120 Alternatives to the !important Keyword | CSS-Tricks Quick Hit #119 New CSS Multi-Column Layout Features in Chrome | CSS-Tricks Quick Hit #118 Making Complex CSS Shapes Using shape() | CSS-Tricks Quick Hit #117 Front-End Fools: Top 10 April Fools’ UI Pranks of All Time | CSS-Tricks Sniffing Out the CSS Olfactive API | CSS-Tricks What’s !important #8: Light/Dark Favicons, @mixin, object-view-box, and More | CSS-Tricks Quick Hit #116 Form Automation Tips for Happier User and Clients | CSS-Tricks Quick Hit #115 Generative UI Notes | CSS-Tricks Quick Hit #114 Quick Hit #113 Experimenting With Scroll-Driven corner-shape Animations | CSS-Tricks Quick Hit #112 JavaScript for Everyone: Destructuring | CSS-Tricks Quick Hit #111 Quick Hit #110 What’s !important #7: random(), Folded Corners, Anchored Container Queries, and More | CSS-Tricks 4 Reasons That Make Tailwind Great for Building Layouts | CSS-Tricks Quick Hit #109 Quick Hit #108 Abusing Customizable Selects | CSS-Tricks Quick Hit #107 The Value of z-index | CSS-Tricks Quick Hit #106 The Different Ways to Select <html> in CSS Quick Hit #105 Popover API or Dialog API: Which to Choose? Quick Hit #104 What’s !important #6: :heading, border-shape, Truncating Text From the Middle, and More Yet Another Way to Center an (Absolute) Element An Exploit ... in CSS?! Quick Hit #103 A Complete Guide to Bookmarklets Quick Hit #102 Loading Smarter: SVG vs. Raster Loaders in Modern Web Design Potentially Coming to a Browser :near() You Quick Hit #101 Distinguishing "Components" and "Utilities" in Tailwind Quick Hit #100 Spiral Scrollytelling in CSS With sibling-index() Interop 2026 Quick Hit #99 What’s !important #5: Lazy-loading iframes, Repeating corner-shape Backgrounds, and More Quick Hit #98 Making a Responsive Pyramidal Grid With Modern CSS Approximating contrast-color() With Other CSS Features Quick Hit #97 Trying to Make the Perfect Pie Chart in CSS Quick Hit #96 Quick Hit #95 CSS Bar Charts Using Modern Functions Quick Hit #94 No Hassle Visual Code Theming: Publishing an Extension Quick Hit #93
Fun Times With CSS Pixel Art
CSS-Tricks · 2016-08-30 · via CSS-Tricks

Pixel art is one of those lost art forms that have been overshadowed by super crisp, high resolutions images. I stumbled on some pixel art while surfing around CodePen and it reminded me how awesome it is.

See the Pen Pikachu pixel css by Devi Krisdiansyah (@agilBAKA) on CodePen.

How cool is that?! There’s something about pixelated graphics that adds a layer of simplicity and friendliness that can get lost in high definition images and illustrations.

It’s also a great example of how we can create pixel art using HTML and CSS. Let’s break this concept down and create a pattern we can use in other instances.

Create a Grid

First things first. We need a canvas to paint our pixelated masterpiece. This is really a grid that we can create a couple of different ways.

One way would be a standard HTML <table> that contains a bunch of fixed-width cells in each row. For example, let’s say we draw a perfectly square table that is eight columns wide and eight rows deep. If we make each cell 10 pixels square, then we have a table that is 80px wide and tall:

See the Pen CSS Pixels – Table Grid Example by Geoff Graham (@geoffgraham) on CodePen.

Want a larger canvas? Give the cells a larger dimension. Want to double the resolution? Double the number of cells in each table row.

The other way to set up a grid is to ditch the table and replace it with two divs: one that acts as the container for our canvas and another that will can be repeated as many times as we want to represent each pixel in the canvas.

<div class="canvas">
  <div class="pixel"></div>
  <!-- Repeat as many times as needed -->
</div><!-- end .canvas -->

The trick here is knowing exactly how many pixels to create. For that, we can multiply the number of pixels wide by the number of pixels tall. For example, if we create the same 80px square grid as the table method and want a grid that is 8 pixels wide by 8 pixels tall, then we can multiply those for a grand total of 64 pixels.

.canvas {
  /* Perfectly square */
  width: 80px;
  height: 80px;
}

.pixel {
  /* We'll need 64 total pixels in our HTML */
  width: 10px;
  height: 10px;
  float: left;
}

See the Pen CSS Pixels – Div Example by Geoff Graham (@geoffgraham) on CodePen.

What I like about this method is that it is truer to the canvas dimensions we define. I also find it easier to not have to work with the extra HTML markup that comes with table.

If we want more pixels to create a more detailed pattern, then we can quadruple the number of pixels in our HTML markup and cut the size of our pixels in half. This is kind of like making an image in Photoshop that is twice the size of the dimensions you plan to use on the page to create a higher resolution.

.canvas {
  /* Perfectly square */
  width: 80px;
  height: 80px;
}

.pixel {
  /* We'll need 256 total pixels in our HTML */
  width: 5px;
  height: 5px;
  float: left;
}

Start Painting

This is where the rubber meets the road in the sense that we add color to our pixels. We can use the nth-child attribute to select specific pixels in the grid.

/* The third cell in our grid */
.pixel:nth-child(3) {
  background: orange;
}

As you can imagine, this list will get very long very quickly depending on the number of cells in the grid and the how much detail the design requires. The opening example in this post uses 1,920 total pixels in the grid and more than 300 different child selectors. Phew!

A Simple Example

I decided to make a pixelated self portrait. It’s pretty simple since I went with very few pixels and only four total colors.

See the Pen CSS Pixels – Self Portrait by Geoff Graham (@geoffgraham) on CodePen.

CSS Pixel Art as an Icon

Now that we have something to work with, we can use the transform property to scale our artwork down and use it like an icon:

See the Pen CSS Pixels – Self Portrait – Icon by Geoff Graham (@geoffgraham) on CodePen.

Other Pixel Drawing Techniques

box-shadow

You can draw pixel art within a single element by using a big complicated box-shadow! If you declare a vertical and horizontal offset for the box-shadow, but not a blur or spread radius, you’ll get a clean colorized copy of the shape of the element to move around.

Here’s the concept. The black “pixel” is the original, and I’ve created an orange pixel to the bottom left and a red pixel to the bottom right.

See the Pen Basics of Pixel Art by Chris Coyier (@chriscoyier) on CodePen.

You could go hog-wild with that and do entire drawings.

See the Pen Pixel Hellboy by servin (@servinlp) on CodePen.

Preprocessing

Variables could help make it easier to adjust colors and sizing and such. Here’s an example in Less:

See the Pen Pixel-art hipster pacwoman by Mario Sanz (@msanz) on CodePen.

Here’s an example by Una Kravets that takes it a step further in creating the box shadow with a Sass map, which is pretty darn clever:

// Setting the colors we're syncing up with
$pixel-color-map: (
  'r' : #f00,
  'w': #fff,
  'k': #000,
  'o': transparent,
  't': #83401f,
  'p': #ffbc77,
  'b': #06f,
  'y': #ff0,
  'n': #ff8000,
  'g': #5ac528
);

// Mario pixel art matrices!
$pixel-art:(
  mushroom: (
    (o o o o o k k k k k k o o o o o)
    (o o o k k r r r r w w k k o o o)
    (o o k w w r r r r w w w w k o o)
    (o k w w r r r r r r w w w w k o)
    (o k w r r w w w w r r w w w k o)
    (k r r r w w w w w w r r r r r k)
    (k r r r w w w w w w r r w w r k)
    (k w r r w w w w w w r w w w w k)
    (k w w r r w w w w r r w w w w k)
    (k w w r r r r r r r r r w w r k)
    (k w r r k k k k k k k k r r r k)
    (o k k k w w k w w k w w k k k o)
    (o o k w w w k w w k w w w k o o)
    (o o k w w w w w w w w w w k o o)
    (o o o k w w w w w w w w k o o o)
    (o o o o k k k k k k k k o o o o)
  )
);

There are a few more functions in there to convert that into the box-shadow and apply it. Here’s the final result:

See the Pen Sass-Generated Box Shadow Pixel Art! by Una Kravets (@una) on CodePen.

Remember box-shadow can be animated too!

See the Pen Ash and Pikachu box-shadow Pixel Art by Andrew (@AstroDroid) on CodePen.

Canvas

The <canvas> APIs can certainly draw rectangles!

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
  
ctx.fillStyle = "rgb(53, 41, 15)";
ctx.fillRect(48, 0, 8, 8);
ctx.fillStyle = "rgb(238, 187, 68)";
ctx.fillRect(56, 0, 8, 8);

See the Pen Canvas Ark from Terranigma by Max (@MyXoToD) on CodePen.

SVG

An of course <svg> has <rect>. But you could even cheat a little and make <polygon>s that combine multiple pixels.

See the Pen Pixel me by Aloïs De Schepper (@Alo62) on CodePen.

3D!

Ok I think we’ve done enough here.

See the Pen 3D Pixel Art by cx20 (@cx20) on CodePen.

It’s Your Turn!

We’re always a fan of you doing things your own way, but know there are some tools already out there for drawing with pixels:

Have you created CSS pixel art that you’d be willing to share? I created a collection in CodePen, but please post your examples in the comments below so we can bask in the awesomeness.