How to use external stylesheet for Angular material colors
It turns out my issue was Angular was using node sass instead of dart sass, and since @use isn't a thing in node-sass, it threw an error. I had to delete the node-sass folder in my global dependencies.
Angular 9 / Material - Reuse of material theme variables in angular components style
You can do this in a different way:
There are something called 'partials'. You can create partials for various things and include/import them in your component.scss file and use them
Example:
At your root level, create a folder called styles. Inside styles, you can create multiple partials, one for variables, one for mixins, one for fonts etc.,
Partials start with an underscore for their file name such as _variables.scss, _mixins.scss, etc. The underscore will prevent the angular compiler from compiling everytime.
In '_variables.scss' file, you can create multiple global variables for colors, such as
$primary-color: #fff;
$secondary-color: #333; etc
In '_mixins.scss' you can create mixins such as
@mixin mixin-name() {
mixins functionality
}
In your component.scss file, simply import them and use it.
component.scss
@import '../styles/mixins';
@import '../styles/variables';
.component-class-name {
background-color: $primary-color;
color: $secondary-color;
}
Create angular material theme with CSS variables
I created a little library to make this a little easier.
You can use it like so:
Install:
npm i angular-material-css-vars -S
Then remove any existing
@import '~@angular/material/theming';
from your main stylesheet file.Add this to your main stylesheet instead:
@import '~angular-material-css-vars/main';
@include initMaterialCssVars();Change the main theme color like so:
import {MaterialCssVarsService} from 'angular-material-css-vars';
export class SomeComponentOrService {
constructor(public materialCssVarsService: MaterialCssVarsService) {
const hex = '#3f51b5';
this.materialCssVarsService.changePrimaryColor(hex);
}
}
Using Material variables from main styles.scss
Use the mat-color
SASS mixin from Angular Material.
Here's some example usage:
@import '~@angular/material/theming';
@import 'app-theme.scss';
.extend-toolbar {
background-color: mat-color($candy-app-primary);
color: white;
}
For more info, check out the docs.
Related Topics
Can Text Be Hidden and Shown Using Just CSS (No JavaScript Code)
Remove All Stylings (Border, Glow) from Textarea
Jstl If-Statement Inside HTML-Attribute
Scale and Reposition Iframe Like Background-Size: Cover
Why Use * Selector in Combination with *::Before and *::After
How to Specify a CSS Shorthand for "All Elements Except the First/Last"
Bootstrap 4 - How to Make 100% Width Search Input in Navbar
Vertically Align Floating Divs
How to Restrict Width of Bootstrap 3 Dropdown-Menu in Navbar
How to Use CSS to Vertically Center the Text in an Anchor, Within a Li
Flexbox Space-Between and Align Right
Angular Material - Global Color Variables
How to Show Images/Links on Div Hover
Pseudo-Element After Not Showing
Browser Specific CSS Padding for Firefox Field