I'm using one of the angular 2 material's theme. I'm able to use the theme colors on material components like button, toolbar using color="primary". But I'm not able to figure out how to theme a div. I don't want to hardcode the hash in .css file since if i decide to change the theme it'll require changes at all places. Am i missing something? What's the best way to do this?
You can use class="mat-primary" and class="mat-accent" on HTML elements to get the primary and accent colours of your theme.
With Hexadecimal values, you can set a background color for a div or any other element with a total of 6 characters. Hex colors start with the hash sign (#), any number from 0 to 9, and finally any letter from A to F.
In Angular Material, a theme is a collection of color and typography options. Each theme includes three palettes that determine component colors: primary, accent and warn. Angular Material's theming system comes with a predefined set of rules for color and typography styles. The theming system is based on Google's Material Design specification.
The default background color of a div is transparent. So if you do not specify the background-color of a div, it will display that of its parent element. Changing the Background Color of a Div In this example, we will change the background colors of the following divs.
The choice of a light versus a dark theme determines the background and foreground colors used throughout the components. Angular Material offers a "theme" mixin that emits styles for both color and typography and It’s the all-component-themes mixin.
Changing the Background Color in React. There are various ways of changing the background color of a React component, two of which we’ll explore: importing a CSS file and using inline styles. Background Color from an External CSS File. Let’s begin with what I consider to be the easiest method: importing a CSS file into the component.
Indeed, you cannot use color="primary"
on every HTML element.
What I did is a class called color-primary
@import '~@angular/material/theming';
@include mat-core();
$primary: mat-palette($mat-deep-purple, 600, 500, 900);
$accent: mat-palette($mat-amber, A400, A300, A600);
.color-primary {
color: mat-color($primary);
}
.color-accent {
color: mat-color($accent);
}
Of course, I've set the color here but you can create another class, for example : background-color-primary
.
(here's the link of my sample project : Pizza-Sync).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With