Jump to content
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Sign in to follow this  
Rss Bot

Get started with CSS art

Recommended Posts

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:

View the full article

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×