Improving website by replacing images with CSS
Tags: css, beginners
Published: 2020-12-07 09:12:24

Improving the performance of the website is serious business. Most people don’t realise how much standard stock photo weight.

My [blog](https://albertwalicki.com)  was built in Gatsby to have the best performance with images. High-quality images are huge and very heavy; that’s why we should always optimise them.

# How images slow down my page?

*   **Heavy images**: a website with large images will load much slower. Each image is an additional request to the server and extra seconds to wait.
*   **Wrong dimensions**: calling down images won’t speed up your page. Each image should have a few different sizes. For example, on phones, we don’t want to load full HD images, we want to load much much smaller.
*   **Old file formats**: Naturally, different formats are larger than others. Next-gen images are images that have superior quality and compression compared to their JPEG and PNG ancestors. We should use them whenever we can.

If you do all this stuff above, you are great! But… Did you ever think about moving further? If you are using next-gen, small, RWD-friendly images what you can do more? **Replace images with CSS**! A lot of images can be replaced with pure CSS. This is the next level of performance of your website. Consider an example with 4kB image, ten similar images weight 40kB! And of course, often overlooked — 10 additional requests to resolve.

As a developer, I’m always trying to reduce the weight of the bundled app, and this is a straightforward way to do this.

# Real examples

A lot of images can be replaced with pure CSS + HTML to improve the performance of the website. All small dots, clouds, speech bubbles, shapes, stars, lines can be created without using images. I will show you two examples from real projects. Let’s start!

## Frontend unicorn book


<noscript><img alt="Image for post" class="dq hn dr ho w" src="https://miro.medium.com/max/1600/0*stcIyqXTUa0iKP5c.jpg" width="800" height="450" srcSet="https://miro.medium.com/max/552/0*stcIyqXTUa0iKP5c.jpg 276w, https://miro.medium.com/max/1104/0*stcIyqXTUa0iKP5c.jpg 552w, https://miro.medium.com/max/1280/0*stcIyqXTUa0iKP5c.jpg 640w, https://miro.medium.com/max/1400/0*stcIyqXTUa0iKP5c.jpg 700w" sizes="700px"/></noscript>

[https://frontenduniorn.com](https://frontendunicorn.com/)

On my book website — [Frontend Unicorn](https://frontendunicorn.com) all those rectangles are pure CSS. They are extremely easy — squares with background and shadow. When my friend and one of the best designers I know — [Diana](https://medium.com/@dianamalewicz) — did that layout, she wanted to cut all of these squares as one big image.

> Hey! Don’t do that! I will create them in CSS.

This image could waste 20–50kB and slow down the page a little bit, moreover doing it with CSS allows me to animate those squares without losing quality!

One square 37x37px as PNG weight 3KB and as SVG almost 1kB. To write it in CSS + HTML we have to use 150B/0.15kB (minified). Of course, images and SVG also need some HTML + CSS which I didn’t count 😉.

| PNG| Compresed PNG | SVG | CSS |
| -| - |-| -|
| 3kB | 1kB | 1kB | 0.15kB |

Using SVG, in this case, is also much better than PNG. We can use `currentColor` inside them to create more colourful squares.

All those clouds will be replaced with CSS soon.

## Counter images

One of our clients asked me a few weeks ago:

> For some reason I can not find the files for the number of images. It’s the 1,2,3,4 & 5 circular images.


<noscript><img alt="Image for post" class="dq hn dr ho w" src="https://miro.medium.com/max/800/0*BU9OqRXjJ-TSeDn4.png" width="400" height="225" srcSet="https://miro.medium.com/max/552/0*BU9OqRXjJ-TSeDn4.png 276w, https://miro.medium.com/max/800/0*BU9OqRXjJ-TSeDn4.png 400w" sizes="400px"/></noscript>

Image from one of our client's website

Ha! There isn’t any image. This image was created with a few lines of CSS and HTML. This is my favourite performance improvement example.

| PNG | Compresed PNG | SVG | CSS |
| -| - |-| -|
| 41kB | 13kB | 4kB | 0.5kB |

Naturally, like in the example above — images and SVG weight is counted without any styling and HTML.

This client has 5 similar images on his website, but with a different number on it. So let’s count again:

| 5 PNG | 5 Compresed PNG | 5 SVG | 5 CSS |
| -| - |-| -|
| 205kB | 65kB | 20kB | 0.5kB |

Do you see it? Five small **images replaced with CSS** and such an improvement!

Creating an image with 1,2,3,4 & 5 numbers creates one more problem. Accessibility. The Web Content Accessibility Guidelines (WCAG) recommended to not use images of text if you expect the text to be read by the user. You can read more here: [Images of Text](https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-text-presentation.html).

# Image-to-CSS

Most of the shapes are built with a single HTML element and one or two pseudo-elements [:before](https://developer.mozilla.org/pl/docs/Web/CSS/:before) and [:after](https://developer.mozilla.org/pl/docs/Web/CSS/:after).

This article begins the series [Image-to-CSS](https://www.albertwalicki.com/tags/image-to-css) about replacing images with CSS. I will teach you the basics of CSS shapes, and then we will go further into a little bit more complicated cases.

Thanks! 😎

You can follow me on social media or sign up for my newsletter!
🐦 [Twitter](https://twitter.com/AlbertWalicki)
👨🏻‍💻 [Blog](https://albertwalicki.com)
📰 [Medium](https://albertwalicki.medium.com/)
💌 [Newsletter](https://albertwalicki.com/newsletter)
🦄 [Frontend Unicorn book](http://frontendunicorn.com/)