Theming with PrimeNG CSS Variables

We introduced CSS variables in Version 11.3.0-RC.1. This article will explain how to use the new PrimeNG CSS variables and design logic. Let’s start!

Project Setup

Let’s create a brand new angular application using angular-cli.

ng new primengColor

Let’s add PrimeNG, PrimeFlex, and PrimeIcons.

npm install primeng primeicons primeflex

We need to import PrimeNG’s CSS dependencies to Angular.json.

node_modules/primeicons/primeicons.css node_modules/primeng/resources/themes/saga-blue/theme.css node_modules/primeng/resources/primeng.min.css

Lastly, we need to import a couple of PriemNG components. I want to create a simple form and a sidebar. We’re gonna use ButtonModule, SidebarModule, InputTextModule, InputTextareaModule, and ToastModule.

We’re ready for using PrimeNG, let’s start!

Implementation

We’ll start with surfaces and general colors.

General colors are:

General Colors

Our project gonna use text-color and font-family.

Surface colors are:

Surface Colors for Saga Blue

Surface colors can be useful when designing the surface layers and separators.

Each PrimeNG theme exports its own color palette so all colors will be adaptive to our theme.

Let’s use it in our style.css:

style.css

Now we can create our simple application.

Topbar

p-shadow-2 and p-p-2 are coming from PrimeFlex. p-shadow-* is an elevation helper. We can use it to specify the separation between surfaces and elements along the z-axis. p-p-* is a spacing helper. p-p-* gives padding to elements. In our case this padding is 0.5rem.

I’m gonna use the primary color for the topbar’s background color. I want to topbar’s background color the same as the button’s color (The pButton’s default background color is already the primary color).

Topbar

Sidebar & Simple Form

We don’t need CSS variables now because PrimeNG components using already! Let’s create properties for our components!

Message Service there is for p-toast and PrimeNGConfig there is for activation ripple.

We’re gonna add our components to app.component.html!

p-fluid there are for input width’s. p-formgrid, p-gird coming from PrimeFlex!

Now our app looks like this:

Saga-Blue

Let’s change the theme to bootstrap4-dark-purple:

node_modules/primeng/resources/themes/bootstrap4-dark-purple/theme.css

Let’s try with vela-green too!

node_modules/primeng/resources/themes/vela-green/theme.css

Voilà!

Variables always will be compatible with your theme choices!

Thanks for your reading.

Playground:

Lead Developer of PrimeNG