Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the background-color of a div as primary theme color in Angular 2 Material

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?

like image 800
Savyasachi Avatar asked May 06 '17 07:05

Savyasachi


People also ask

How do you find the primary color in a theme?

You can use class="mat-primary" and class="mat-accent" on HTML elements to get the primary and accent colours of your theme.

Can div have color?

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.

What are themes in Angular Material?

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.

What is the default background color of a Div in HTML?

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.

What is the difference between light and dark theme in angular?

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.

How do I change the background color of a React component?

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.


1 Answers

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).

like image 66
maxime1992 Avatar answered Sep 20 '22 17:09

maxime1992