Use custom icons with tabs in ionic2

Enric Gimenez picture Enric Gimenez · Feb 15, 2017 · Viewed 13.1k times · Source

I need to use customs icons with tabs in ionic 2.

Moreover I need to change the title color and icon, if the tab is selected.

Example:

ionic Tabs

Answer

adjwilli picture adjwilli · Jun 15, 2017

This is the easiest way I've found, from the forums at https://forum.ionicframework.com/t/anyway-to-custom-the-tabbars-icon-with-my-own-svg-file/46131/36.

In your app.scss file, add the following code:

ion-icon {
    &[class*="custom-"] {
        // Instead of using the font-based icons
        // We're applying SVG masks
        mask-size: contain;
        mask-position: 50% 50%;
        mask-repeat: no-repeat;
        background: currentColor;
        width: 1em;
        height: 1em;
    }
    // custom icons
    &[class*="custom-icon1"] {
        mask-image: url(../assets/img/customicon1.svg);
    }
    &[class*="custom-icon2"] {
        mask-image: url(../assets/img/customicon2.svg);
    }
    &[class*="custom-icon3"] {
        mask-image: url(../assets/img/customicon3.svg);
    }
}

Then in your templates, you can use the following HTML to create the icon:

<ion-icon class="custom-icon1"></ion-icon>

In other questions, people suggest a font based method. This answer though is far less complicated to use, as long as you're not adding hundreds of icons you should be okay. The caveat is that each image is sent as a separate request, where as with the font methods all the images are contained in one file, so it would be a little more efficient.