I often find myself thinking about the best way to share something like a code example. Gists seem to work well, especially for something more complex than a function that makes up more than a few lines. But, when you do have something that is only a few lines of code that can seem like overkill.
My next thought goes to taking screenshots of my text editor. This has drawbacks as well since it can take multiple attempts to get something right that will embed well wherever I am putting the example.
Today I came across this tweet from @emmawedekind
I was reminded that I have seen code examples that look like this before on Twitter. I really like how it looks. The code is clear, has highlighting, and the whole result just looks really polished and professional. This is the kind of resource that communicates clearly and helps to elevate the content by looking nice.
I asked Emma how they created the screenshot and they pointed out the website Carbon to me. It is a really neat site built by Dawn Labs that is free to use. You can choose from various themes to adjust the colors. There is a setting for the language you are using so that it gets the highlighting right. You can export the image out to PNG or SVG for use online or just tweet the picture directly from there if you want.
The results look really nice.
I really like how it takes the guess work out of creating code example images and leaves you able to focus on the code that you are trying to share. I definitely suggest checking it out and seeing if it can help you.
Have you used Carbon before? I'd love to see examples of how folks have used this in their own work. Let me know in the comments!
UPDATE (Mar 2nd, 2019)
Multiple folks in the comments have pointed out the accessibility issues with using only images to share code as it hinders someone using a screen reader to be able to consume the content fully. Screen readers won't read out the content of the image and instead use the Alt Text set for the image.
It's also been noted that when the code is in an image it can't be selected for copying and pasting for review in a text editor.
These are both very valid points. I still think Carbon is an awesome app and is very useful, but this goes to show that it isn't a complete solution and should be combined with other methods of sharing code to ensure everyone can get what they need.
I'd still say to use it for sharing code snippets on Twitter as there really isn't a better way to do it at this time within a tweet. You'd have to link out to a gist or another place otherwise.
As for using code images in content like articles I'd consider using the images as only headers or presentational pieces and then embedding a gist or something else that allows for a screen reader to function and for the code to be more easily shared as some have suggested. Or using the image to show the code and linking to a more consumable version within your text explaining the code.
If you are going to use the image for the main way to communicate the code make sure to set good Alt Text for it to help anyone using a screen reader.
A lot of this can be hard to get right sometimes. I found some good resources on Twitter from Marcy Sutton who replied back on this tweet.
An alt decision tree - helps in determining how much info to give in the
- How A Screen Reader User Accesses The Web - a webinar from Smashing Magazine showing the experience of a blind individual using a screen reader online.