CSS-based theming enables apps to customize the colors quickly by loading a CSS file or changing a few CSS property values.

Colors

Ionic has nine default colors that can be used to change the color of many components. Each color is actually a collection of multiple properties, including a shade and tint, used throughout Ionic.

A color can be applied to an Ionic component in order to change the default colors using the color attribute. Notice in the buttons below that the text and background changes based on the color set. When there is no color set on the button it uses the primary color by default.

  1. <ion-button>Default</ion-button>
  2. <ion-button color="primary">Primary</ion-button>
  3. <ion-button color="secondary">Secondary</ion-button>
  4. <ion-button color="tertiary">Tertiary</ion-button>
  5. <ion-button color="success">Success</ion-button>
  6. <ion-button color="warning">Warning</ion-button>
  7. <ion-button color="danger">Danger</ion-button>
  8. <ion-button color="light">Light</ion-button>
  9. <ion-button color="medium">Medium</ion-button>
  10. <ion-button color="dark">Dark</ion-button>

Layered Colors

Each color consists of the following properties: a base, contrast, shade, and tint. The base and contrast colors also require a rgb property which is the same color, just in rgb format. See The Alpha Problem for an explanation of why the rgb property is also needed. Select from the dropdown below to see all of the default colors Ionic provides and their variations.

Modifying Colors

To change the default values of a color, all of the listed variations for that color should be set. For example, to change the secondary color to #006600 , set the following CSS properties:

  1. :root {
  2. --ion-color-secondary: #006600;
  3. --ion-color-secondary-rgb: 0,102,0;
  4. --ion-color-secondary-contrast: #ffffff;
  5. --ion-color-secondary-contrast-rgb: 255,255,255;
  6. --ion-color-secondary-shade: #005a00;
  7. --ion-color-secondary-tint: #1a751a;
  8. }


When secondary is applied to a button, not only is the base color #006600 used, but the contrast color #ffffff is used for the text, along with shade #005a00 and tint #1a751a colors for the different states of the button.

Not sure how to get the variation colors from the base color? Try out our Color Generator that calculates all of the variations and provides code to copy/paste into an app!

See the CSS Variables documentation for more information on CSS variables.

Adding Colors

To add a new color, create a class that defines all of the variations using CSS variables for that color. The class should be written in the format .ion-color-{COLOR} where {COLOR} is the name of the color to add. For example, to add a color called favorite, the following class could be added:

  1. .ion-color-favorite {
  2. --ion-color-base: #69bb7b;
  3. --ion-color-base-rgb: 105,187,123;
  4. --ion-color-contrast: #ffffff;
  5. --ion-color-contrast-rgb: 255,255,255;
  6. --ion-color-shade: #5ca56c;
  7. --ion-color-tint: #78c288;
  8. }


Once the class is added, the color can be used on any Ionic component that supports the color property. An example of using the favorite color on an Ionic button is below.

  1. <ion-button color="favorite">Favorite</ion-button>


It's important to note that adding the class above does not automatically create the Ionic CSS variables for use in an application's stylesheets. This means that the variations beginning with —ion-color-favorite do not exist by adding the .ion-color-favorite class. These should be declared separately for use in an application:

  1. :root {
  2. --ion-color-favorite: #69bb7b;
  3. --ion-color-favorite-rgb: 105,187,123;
  4. --ion-color-favorite-contrast: #ffffff;
  5. --ion-color-favorite-contrast-rgb: 255,255,255;
  6. --ion-color-favorite-shade: #5ca56c;
  7. --ion-color-favorite-tint: #78c288;
  8. }


Now the favorite color can be used in CSS like below to set the background and color on a div.

  1. div {
  2. background: var(--ion-color-favorite);
  3. color: var(--ion-color-favorite-contrast);
  4. }


See the CSS Variables documentation for more information on CSS variables.

Themes

Ionic provides several global variables that are used throughout components to change the default theme of an entire application. The following sections separate the different theme variables by usage: Application Colors, Stepped Colors.

Application Colors

The application colors are used in multiple places in Ionic. These are useful for easily creating dark themes or themes that match a brand.

It is important to note that the background and text color variables also require a rgb variable to be set in rgb format. See The Alpha Problem for an explanation of why the rgb property is also needed.

NameDescription
—ion-background-colorBackground color of entire app
—ion-background-color-rgbBackground color of entire app, rgb format
—ion-text-colorText color of entire app
—ion-text-color-rgbText color of entire app, rgb format
—ion-backdrop-colorColor of the Backdrop component
—ion-overlay-background-colorBackground color of the overlays
—ion-border-colorBorder color
—ion-box-shadow-colorBox shadow color
—ion-tab-bar-backgroundBackground of the Tab bar
—ion-tab-bar-background-focusedBackground of the focused Tab bar
—ion-tab-bar-border-colorBorder color of the Tab bar
—ion-tab-bar-colorColor of the Tab bar
—ion-tab-bar-color-activatedColor of the activated Tab
—ion-toolbar-backgroundBackground of the Toolbar
—ion-toolbar-border-colorBorder color of the Toolbar
—ion-toolbar-colorColor of the components in the Toolbar
—ion-toolbar-color-activatedColor of the activated components in the Toolbar
—ion-toolbar-color-uncheckedColor of the unchecked components in the Toolbar
—ion-toolbar-color-checkedColor of the checked components in the Toolbar
—ion-item-backgroundBackground of the Item
—ion-item-background-activatedBackground of the activated Item
—ion-item-border-colorBorder color of the Item
—ion-item-colorColor of the components in the Item
—ion-placeholder-colorColor of the placeholder in inputs

Stepped Colors

After exploring different ways to customize the Ionic theme, we found that we couldn't use just one background or text color. In order to imply importance and depth throughout the design, we need to use different shades of the background and text colors. To accommodate this pattern, we created stepped colors.

While updating the background (—ion-background-color) and text (—ion-text-color) variables will change the look of the app for most components, there are certain Ionic components where it may look off, or broken. This will be more apparent when applying a darker theme.

In some components we use a shade darker than the background or lighter than the text. For example, an item heading text may need to be #404040 , which is a few shades lighter than our default text color. Meanwhile, the loading component background is a shade darker than white, using #f2f2f2 . We use stepped colors in order to define these slight variations. It is important to update the stepped colors when updating the background or text color of an application.

By default, the Ionic stepped colors start at the default background color value #ffffff and mix with the text color value #000000 using an increasing percentage. The full list of stepped colors is shown in the generator below.

Generate Stepped Color Variables

Create a custom background and text color theme for your app. Update the background or text color’s hex values below, then copy and paste the generated code directly into your Ionic project.

Globals

While the previously mentioned variables are useful for changing the colors of an application, often times there is a need for variables used in multiple components. The following variables are shared across components to change global padding settings and more.

Application Variables

NameDescription
—ion-font-familyFont family of the app
—ion-statusbar-paddingStatusbar padding top of the app
—ion-safe-area-topAdjust the safe area inset top of the app
—ion-safe-area-rightAdjust the safe area inset right of the app
—ion-safe-area-bottomAdjust the safe area inset bottom of the app
—ion-safe-area-leftAdjust the safe area inset left of the app
—ion-marginAdjust the margin of the Margin attributes
—ion-paddingAdjust the padding of the Padding attributes

Grid Variables

NameDescription
—ion-grid-columnsNumber of columns in the grid
—ion-grid-padding-xsPadding of the grid for xs breakpoints
—ion-grid-padding-smPadding of the grid for sm breakpoints
—ion-grid-padding-mdPadding of the grid for md breakpoints
—ion-grid-padding-lgPadding of the grid for lg breakpoints
—ion-grid-padding-xlPadding of the grid for xl breakpoints
—ion-grid-column-padding-xsPadding of the grid columns for xs breakpoints
—ion-grid-column-padding-smPadding of the grid columns for sm breakpoints
—ion-grid-column-padding-mdPadding of the grid columns for md breakpoints
—ion-grid-column-padding-lgPadding of the grid columns for lg breakpoints
—ion-grid-column-padding-xlPadding of the grid columns for xl breakpoints

Known Limitations

The Alpha Problem

There is not yet full browser support for alpha use of a hex color. The rgba()_and_rgba()) function only accepts a value in R, G, B, A (Red, Green, Blue, Alpha) format. The following code shows examples of correct and incorrect values passed to rgba().

  1. /* These examples use the same color: blueviolet. */
  2. .broken {
  3. --violet: #8a2be2;
  4. /* rgba(#8a2be2, .5) */
  5. color: rgba(var(--violet), .5) /* ERROR! Doesn't support hex. */
  6. }
  7. .working {
  8. --violet-rgb: 138, 43, 226;
  9. /* rgba(138, 43, 226, .5) */
  10. color: rgba(var(--violet-rgb), .5) /* WORKS! */
  11. }

See the CSS Variables section for more information on how to get and set CSS variables.

Ionic uses colors with an opacity (alpha) in several components. In order for this to work, those properties must be provided in RGB format. When changing any of the properties that have a variation ending in -rgb, it is important they are also provided in a comma separated format without parentheses. Below are some examples for changing text and background color.

  1. :root {
  2. /* These examples use the same color: sienna. */
  3. --ion-text-color: #a0522d;
  4. --ion-text-color-rgb: 160, 82, 45;
  5. /* These examples use the same color: lightsteelblue. */
  6. --ion-background-color: #b0c4de;
  7. --ion-background-color-rgb: 176, 196, 222;
  8. }


Note that the RGB formatted colors are the exact same color as the hex properties, but can now be used with rgba(). For example, —ion-text-color-rgb can now be used in the following way

  1. body {
  2. color: rgba(var(--ion-text-color-rgb), 0.25);
  3. }

Variables in Media Queries

CSS variables in media queries are not currently supported, but there are open drafts to add custom media queries and custom environment variables that would solve this problem! However, with the current state of support, the following will not work:

  1. :root {
  2. --breakpoint: 600px;
  3. }
  4. @media (min-width: var(--breakpoint)) {
  5. /* Doesn't work :( */
  6. }

Modifying CSS Color Variables

While it is possible to easily alter a color in Sass using its built-in functions, it is currently not as easy to modify colors set in CSS Variables. This can be accomplished in CSS by splitting the RGB or HSL channels and modifying each value, but it is complex and has missing functionality.

What exactly does this mean? Basically, using a CSS preprocessor, such as Sass, allows us to use functions to manipulate a single color. For example, we can create the following colors in Sass:

  1. // Background color, shade, and tint
  2. $background: #3880ff;
  3. $background-shade: mix(#000, $background, 12%);
  4. $background-tint: mix(#fff, $background, 10%);
  5. // Text color, darker and lighter
  6. $text: #444;
  7. $text-darker: darken($text, 15);
  8. $text-lighter: lighten($text, 15);


After running through the Sass compiler, the colors will have the following values:

VariableValue
$background#3880ff
$background-shade#3171e0
$background-tint#4c8dff
$text#444444
$text-darker#1e1e1e
$text-lighter#6a6a6a

However, because CSS variables can be set at runtime and are more dynamic, it is not currently possible to manipulate them using a simple function.

This is normally not a problem, but when an application needs to have dynamic theming it presents issues. In Ionic, this is the reason that there are variations to each color, and it is also why stepped colors are necessary for theming.

There are drafts and issues discussing color modification proposals that would make this possible.