fbpx

Flexible icons using SVG, simple XML and Option Sets

Follow me:

There’s one thing that most Bubble applications have in common: it’s likely that you’ll use icons. Bubble has its built-in icons and a plugin containing the icon library from Google’s Material Design. Additionally, there are a few community-built plugins that add more icons, like the Icons (bdk) by the always excellent Gaurav and the Feather Icon library plugin by Airdev.

It’s not always that these libraries contain exactly what you want. Additionally, with thousands of apps using them, you may want to look into other icons to have a design that stands out more. This tip let’s you add icons and/or images to your site simply by storing text instead of uploading any files.

It was shared by Twitter user Youssef Elkhayami, and has some interesting uses. It takes advantage of the fact that SVG files are saved in an XML format that can be placed directly in a HTML element.

Update: thanks to commenter Carmel, we’ve now traced the original source of this trick to be Patricia from the Bubble forum first mentioning it in this thread. Thanks Carmel!

What’s an SVG file?

SVG, or Scalable Vector Graphics is an open standard developed by the World Wide Web Consortium (also known as W3C). As the name suggests, it is a vector graphic format, meaning that instead of being saved as pixels, it is saved as paths, outlines and color settings saved in an XML format.

From a graphical standpoint, it means that it can be resized into any size without any loss of quality, since we’re simply mathematically increasing the size of one or more defined shapes. This has some obvious advantages. Like I mention in my Bubble performance book, SVG files are small, especially since they can be displayed at any dimension without its download size increasing or multiple versions of the file needed.

Why use XML instead of just uploading an SVG file?

From a Bubble developer standpoint, SVG’s have other interesting uses. The fact that it’s XML based means we can store it as text (instead of uploading a file) and directly manipulate how it looks (such as changing a color) by placing dynamic content inside of the XML data.

Here’s the SVG XML content for a scholar hat icon:

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 164 164" stroke="currentColor">
  <path fill="#fff" d="M12 14l9-5-9-5-9 5 9 5z" />
  <path fill="#fff" d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222" />
</svg>

Here’s how that icon would look inside of an HTML element in Bubble:

image

If we change one of the color codes inside of the XML (marked in blue), the icon updates accordingly:

<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 164 164" stroke="currentColor">
  <path fill="#1BAAF8" d="M12 14l9-5-9-5-9 5 9 5z" />
  <path fill="#fff" d="M12 14l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 14l9-5-9-5-9 5 9 5zm0 0l6.16-3.422a12.083 12.083 0 01.665 6.479A11.952 11.952 0 0012 20.055a11.952 11.952 0 00-6.824-2.998 12.078 12.078 0 01.665-6.479L12 14zm-4 6v-7.5l4-2.222" />
</svg>
image 1

In a Bubble HTML element, you can of course replace any value in this XML with a dynamic value, or even the entire XML snippet to change out the icon. These values can be saved directly in your HTML element, or you can create an Option set to store different values and bring out when needed. There are several scenarios where this is useful:

Dark mode/light mode

You can make your pages lighter and save time designing icons by having the colors saved in an option set.

Keeping the page size small

Using an icon library such as Material Icons forces Bubble to download the entire library, adding between 100-200 kb to your total page size. This method saves a tiny string of text, downloading only the icons you need.

Reducing the number of downloads

You don’t need to upload files and can make fewer server requests, which is good for your page load speed.

Can you think of other creative uses? Let us know in the comments!

Support the site and keep it free ❤️

I love tech startups and the Bubble community, and have made it my mission to try and create content that’s valuable, easy to follow and entertaining.

Creating content next to full-time consulting work is time-consuming; if you’d like to support it and keep the site free for everyone, please consider buying me a coffee or becoming a supporting member.

Buy Me A Coffee

Follow me:
Bubble.io books

Learn Bubble the right way

Our professional Bubble books teach you how to plan, structure and build your applications right from the start.

5-star review stars

More Posts

2 Comments


Carmel
September 7, 2022 at 23:11
Reply

The person you credit with this approach is not the originator. If you search the Bubble forum for “ Font Awesome 5 Support’ you will see that it was originated by Patricia a long time before the Tweet you credit. Many of us benefited from this, perhaps it would be nice to credit the correct person.


    Petter Amlie
    September 19, 2022 at 10:30
    Reply

    Thanks for flagging that Carmel! I’ve made an update to the article to reflect that the tip originated from Patricia’s post.

Leave A Reply

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

*

Email icon

Useful articles and tips

Join the mailing list to get guides, opinions and articles on Bubble, no-code, automation and the tech industry.

We don't share your email address with anyone, and you can unsubscribe at any time.

You have successfully subscribed