search

Original address: https://css-tricks.com/fun-times-css-pixel-art/#article-header-id-4

Friendly reminder: Due to domestic network issues, CodePen may not be able to open or may be very slow, please be patient!

Pixel art is a lost art form that pales in comparison to ultra-clear, high-resolution pictures. I stumbled across some pixel art while browsing on CodePen and it reminded me how awesome this art can be!

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

Isn’t it cool? There's something simple and friendly about pixelated graphics that's missing from high-definition graphics and illustrations.

This is also a great example of how to create pixel art with HTML and CSS. Let's analyze this concept and create a pattern that can be used in other situations.

Create grid

First thing, we need a canvas to draw our pixelated artwork on. There are many ways to create a grid. One way is to use the standard HTML <table> element, which contains fixed-width cells for each row. For example, we draw a perfect square with eight rows and eight columns. If we set each cell to 10px wide, then we will get an 80X80 table. <p class="codepen" data-height="265" data-theme-id="dark" data-slug-hash="yJWXZK" data-default-tab="result" data-user="geoffgraham" data-embed-version="2">See the Pen CSS Pixels - Table Grid Example by Geoff Graham (@geoffgraham) on CodePen.</p> <p> </p> <p>Give the cell a larger size if you want a larger canvas. If you want to change from 8-bit resolution to 16-bit resolution, you only need to double the number of cells in each row of the table. </p> <p>Another way to create a grid is to use two divs instead of tables. One of them acts as a container for the canvas; the other represents the elements on the canvas and can be repeated as many times as we need. </p> <div class="cnblogs_code"> <pre class="brush:php;toolbar:false">&lt;span style=&quot;color: #0000ff;&quot;&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div &lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;=&quot;canvas&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&gt;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div &lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;=&quot;pixel&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&gt;&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&lt;!--&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt; Repeat as many times as needed &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;--&gt;&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt;div&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&lt;!--&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt; end .canvas &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;--&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;</pre> </div> <p>The reason I like this is that it's more realistic to the canvas size we defined. And I think this way is simpler without writing extra HTML tags from the <span class="cnblogs_code">table</span> element. </p> <p>If we want more pixels to create a clearer pattern, then we can double the number of pixels in the HTML tag and halve the size of each pixel. It's as if you created an image in Photoshop that you intended to use on a web page and doubled its size to get a higher resolution. </p> <div class="cnblogs_code"> <pre class="brush:php;toolbar:false">&lt;span style=&quot;color: #800000;&quot;&gt;.canvas &lt;/span&gt;{ &lt;span style=&quot;color: #008000;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt; Perfectly square &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;*/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt; width&lt;/span&gt;:&lt;span style=&quot;color: #0000ff;&quot;&gt; 80px&lt;/span&gt;;&lt;span style=&quot;color: #ff0000;&quot;&gt; height&lt;/span&gt;:&lt;span style=&quot;color: #0000ff;&quot;&gt; 80px&lt;/span&gt;; }&lt;span style=&quot;color: #800000;&quot;&gt; .pixel &lt;/span&gt;{ &lt;span style=&quot;color: #008000;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt; We'll need 256 total pixels in our HTML &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;*/&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt; width&lt;/span&gt;:&lt;span style=&quot;color: #0000ff;&quot;&gt; 5px&lt;/span&gt;;&lt;span style=&quot;color: #ff0000;&quot;&gt; height&lt;/span&gt;:&lt;span style=&quot;color: #0000ff;&quot;&gt; 5px&lt;/span&gt;;&lt;span style=&quot;color: #ff0000;&quot;&gt; float&lt;/span&gt;:&lt;span style=&quot;color: #0000ff;&quot;&gt; left&lt;/span&gt;; }</pre> </div> <h2> </h2> <h2 id="Start-drawing">Start drawing</h2> <p>We add color to pixels in a sense like rubber meets road. We can select elements in the grid using the <span class="cnblogs_code"><span style="color: #800000;">nth-child</span></span> attribute. </p> <div class="cnblogs_code"> <pre class="brush:php;toolbar:false">&lt;span style=&quot;color: #008000;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt; The third cell in our grid &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;*/&lt;/span&gt;&lt;span style=&quot;color: #800000;&quot;&gt; .pixel:nth-child(3) &lt;/span&gt;{&lt;span style=&quot;color: #ff0000;&quot;&gt; background&lt;/span&gt;:&lt;span style=&quot;color: #0000ff;&quot;&gt; orange&lt;/span&gt;; }</pre> </div> <p>正如你想象的那样,这个列表会很长,它取决于网格中的单元格的数量和设计的细节。文章开头的例子中使用了1920个像素并且超过300个子类选择器。天哪!</p> <p> </p> <h2 id="一个简单的例子">一个简单的例子</h2> <p>我决定做一张像素化的自画像。这个例子很简单,因为它的像素很少并且总共只有四种颜色。</p> <p class="codepen" data-height="265" data-theme-id="dark" data-slug-hash="qNGXjA" data-default-tab="result" data-user="geoffgraham" data-embed-version="2">See the Pen CSS Pixels - Self Portrait by Geoff Graham (@geoffgraham) on CodePen.</p> <p> </p> <h2 id=""> </h2> <h2 id="作为Icon的CSS像素艺术">作为Icon的CSS像素艺术</h2> <p>既然我们已经有了素材,我们可以 使用 <span class="cnblogs_code"><span style="color: #800000;">transform </span></span>属性缩小图片把它作为icon使用。</p> <p class="codepen" data-height="265" data-theme-id="dark" data-slug-hash="rLgGar" data-default-tab="result" data-user="geoffgraham" data-embed-version="2">See the Pen CSS Pixels - Self Portrait - Icon by Geoff Graham (@geoffgraham) on CodePen.</p> <p> </p> <h2 id=""> </h2> <h2 id="其他的像素绘制技术">其他的像素绘制技术</h2> <h3 id="box-shadow">1.box-shadow</h3> <p>你可以用一个元素通过复杂的 <span class="cnblogs_code"><span style="color: #800000;">box-shadow</span></span> 属性绘制像素艺术。如果你声明一个 <span class="cnblogs_code"><span style="color: #800000;">box-shadow</span></span> 的垂直和水平偏移,而没有模糊值及阴影半径,你将得到一个可以随意移动的元素形状的彩色复制体。</p> <p>以下是概念实例。黑色元素是初始形状,我已经在左下角创建了一个橙色像素以及在右下角创建了一个红色像素。</p> <p class="codepen" data-height="265" data-theme-id="dark" data-slug-hash="qaWqLV" data-default-tab="result" data-user="chriscoyier" data-embed-version="2">See the Pen Basics of Pixel Art by Chris Coyier (@chriscoyier) on CodePen.</p> <p>你可以疯狂的使用这种方式绘制整个图案。</p> <p class="codepen" data-height="432" data-theme-id="dark" data-slug-hash="yYbmrm" data-default-tab="result" data-user="servinlp" data-embed-version="2">See the Pen Pixel Hellboy by servin (@servinlp) on CodePen.</p> <script type="text/javascript" src="//assets.codepen.io/assets/embed/ei.js"></script> <p> </p> <h3 id="预处理">2.预处理</h3> <p>变量可以更容易地调整颜色和大小等。</p> <p>以下是less编写的例子。</p> <p class="codepen" data-height="265" data-theme-id="dark" data-slug-hash="jbMXwq" data-default-tab="result" data-user="msanz" data-embed-version="2">See the Pen Pixel-art hipster pacwoman by Mario Sanz (@msanz) on CodePen.</p> <script type="text/javascript" src="//assets.codepen.io/assets/embed/ei.js"></script> <p>这是Una Kravets编写的例子, 他更进一步地使用Sass map 创建box-shadow,很聪明的方法。</p> <div class="cnblogs_code"> <pre class="brush:php;toolbar:false">&lt;span style=&quot;color: #800000;&quot;&gt;// 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) ) );&lt;/span&gt;</pre> </div> <p>有很多函数可以把它转换成box-shadow并且应用它。下面是最终结果。</p> <p class="codepen" data-height="600" data-theme-id="dark" data-slug-hash="oXXRgg" data-default-tab="result" data-user="una" data-embed-version="2">See the Pen Sass-Generated Box Shadow Pixel Art! by Una Kravets (@una) on CodePen.</p> <script type="text/javascript" src="//assets.codepen.io/assets/embed/ei.js"></script> <p>记住box-shadow也可以做动画。</p> <p class="codepen" data-height="265" data-theme-id="dark" data-slug-hash="OXEEgL" data-default-tab="result" data-user="AstroDroid" data-embed-version="2">See the Pen Ash and Pikachu box-shadow Pixel Art by Andrew (@AstroDroid) on CodePen.</p> <script type="text/javascript" src="//assets.codepen.io/assets/embed/ei.js"></script> <p> </p> <h3 id="canvas">3.canvas</h3> <p> <span class="cnblogs_code"><span style="color: #0000ff;"><span style="color: #800000;">canvas</span><span style="color: #0000ff;">></span></span> 肯定可以绘制矩形。</span></p> <div class="cnblogs_code"> <pre class="brush:php;toolbar:false">&lt;span style=&quot;color: #0000ff;&quot;&gt;var&lt;/span&gt; canvas = document.getElementById(&quot;canvas&quot;&lt;span style=&quot;color: #000000;&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;var&lt;/span&gt; ctx = canvas.getContext(&quot;2d&quot;&lt;span style=&quot;color: #000000;&quot;&gt;); ctx.fillStyle &lt;/span&gt;= &quot;rgb(53, 41, 15)&quot;&lt;span style=&quot;color: #000000;&quot;&gt;; ctx.fillRect(&lt;/span&gt;48, 0, 8, 8&lt;span style=&quot;color: #000000;&quot;&gt;); ctx.fillStyle &lt;/span&gt;= &quot;rgb(238, 187, 68)&quot;&lt;span style=&quot;color: #000000;&quot;&gt;; ctx.fillRect(&lt;/span&gt;56, 0, 8, 8);</pre> </div> <p class="codepen" data-height="350" data-theme-id="dark" data-slug-hash="fvnek" data-default-tab="result" data-user="MyXoToD" data-embed-version="2">See the Pen Canvas Ark from Tranigma by Max (@MyXoToD) on CodePen.</p> <script type="text/javascript" src="//assets.codepen.io/assets/embed/ei.js"></script> <p> </p> <h3 id="svg">4.svg</h3> <p>Although <span class="cnblogs_code"><span style="color: #0000ff;"><span style="color: #800000;">svg</span><span style="color: #0000ff;">></span></span> has <span class="cnblogs_code"><span style="color: #0000ff;"><span style="color: #800000;">rect</span><span style="color: #0000ff;">></span></span> , but you can be opportunistic and use <span class="cnblogs_code"><span style="color: #0000ff;"> which contains more pixels <span style="color: #800000;">polygon</span><span style="color: #0000ff;">&gt ;</span></span> . </span></span></span></p> <p class="codepen" data-height="392" data-theme-id="dark" data-slug-hash="pJmQWj" data-default-tab="result" data-user="Alo62" data-embed-version="2">See the Pen Pixel me by Aloïs De Schepper (@Alo62) on CodePen.</p> <script type="text/javascript" src="//assets.codepen.io/assets/embed/ei.js"></script> <p> </p> <h3 id="D">5.3D! </h3> <p>Okay, I think we’ve done enough. </p> <p class="codepen" data-height="407" data-theme-id="dark" data-slug-hash="qEibr" data-default-tab="result" data-user="cx20" data-embed-version="2">See the Pen 3D Pixel Art by cx20 (@cx20) on CodePen.</p> <script type="text/javascript" src="//assets.codepen.io/assets/embed/ei.js"></script> <h2> </h2> <h2 id="It-s-your-turn">It’s your turn</h2> <p>We’re always keen for you to do things your own way, but know that there are plenty of tools out there for drawing pixel art. </p> <ul> <li>Ludvig Lindblom's Canvas box-shadow pixel art generator</li> <li>Jenn Schiffer's make 8-bit art!</li> <li>XOXCO's Make Pixel Art</li> </ul> <p></p> <div id="MySignature"></div> <div class="clear"></div> </table>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.