Making CSS art can be intimidating, but it's easy once you break graphics down into simple parts. If you peruse websites like CodePen, DevArt and Dribbble, it’s very easy to feel overwhelmed by the incredible things that developers and designers make, especially when you’re just starting out. I made my first piece of CSS art after lurking on these sites for years and eventually mustered up the courage to try and do it. Now, a few years and over 100 experiments later, it’s one of my favourite hobbies. 

Making CSS art is interesting and fun. It teaches you nuances of CSS that you might not normally work with, helps you see graphics (and problems in general) modularly and improves your speed when writing CSS. It’s also a great way for designers and developers to get on the same page when working on a user interface together. As you practise and refine your skills making CSS art, you’ll see an improvement in your day-to-day work (and you might even enjoy it more, too).

In this article, we’re going to be making a cute ice cream bar graphic in CSS. I’ll talk through and give you the code to follow, so that you can customise it and make it your own. Let’s get started!

01. Set up your document

Before we write any CSS, we have to actually have something to style. You can use a preprocessor like HAML or Pug, or just straight HTML. I’ll stick with pure HTML and CSS in our examples, but you’re free to use what you’re most comfortable with. Pull up a blank Pen on CodePen or an HTML document, and include the following for the ice cream bar in the <body>.

02. Settle on colours and sizes

Normally when you have a graphic in front of you to make, you will need the dimensions and colours of that graphic. In this particular case, I’ll be providing you with those dimensions and colours.

When you don’t have me around though, I recommend getting yourself a colour picker tool (I personally use Digital Color Meter on Mac and ColorPic on Windows), and a screenshot tool to figure out your dimensions (the native ones for your operating systems work just fine).

Let’s add those dimensions and colours now to our document.

03. Add positioning and transforms

Right now, our image is all blocky and in the corner. Before we shape everything better, let’s add some positioning and transforms. Personally, my favourite positioning techniques (generally) are absolute positioning containers, and using flexbox for everything else. If you prefer using absolute and relative positioning for everything, or even CSS Grid, go for it!

Now, we have to position the container in the middle of the page (I’ll be using absolute positioning and a translate transform), and make sure the contents of the main container are centred and rotated as needed.

You might notice that I also added a couple of dimensions for the '.face' and '.eyes' classes – that’s for handling the spacing within that container. Go ahead and add these changes in yourself to each class:

04. Choose your pseudo-elements

Our little ice cream bar is looking pretty good, if I do say so myself! One last concept that is incredibly helpful for making CSS art is pseudo-elements.

Every single element on a page can actually be three elements: the main element, ::before, and ::after. You can style ::before and ::after as if they’re separate <div>s within any element.

For example, I could have changed the .eyes container to have no .eye <div>s at all, and just style a ::before and ::after for each eye. That would have been cool, but that would make styling the glare on each eye impossible.

That being said, for the final missing glare on those eyes, let’s add a ::before pseudo-element to make that possible. The key things to add to a pseudo-element are a display value and content: ‘’.

Check it out, and with that, we’re done!

You're an artist!

We made something really fun together today. If you’d like to check out my final result, you can find it on my CodePen profile (above). Making CSS art is really just a matter of applying the concepts we talked about in interesting and fun ways. You can take these techniques and run with them, and even add your own animations with keyframes and transitions. All this being said, I hope you learned something new, and this motivated you to try your hand at making something beautiful.

This article was originally published in creative web design magazine Web Designer. Buy issue 282 or subscribe.

Related articles:

from Creative Bloq http://www.creativebloq.com/features/get-started-with-css-art

Get started with CSS art
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *