Customizing the default font color in Angular Material

I am currently navigating through theming in Angular Material and feeling a bit disoriented. I have implemented the prebuilt indigo-pink theme by importing it into my styles.scss as shown below:

@import "~@angular/material/prebuilt-themes/indigo-pink.css"; 

Following the documentation, I created a new file named theme.scss and included it in the angular.json after styles.scss. Here is how my theme.scss looks:

@import '~@angular/material/theming';

@include mat-core();

$sg-app-primary: mat-palette($mat-indigo);
$sg-app-accent:  mat-palette($mat-pink, A200, A100, A400);

$sg-app-theme: mat-light-theme($sg-app-primary, $sg-app-accent);

@include angular-material-theme($sg-app-theme);

My Objective All I want to do is change the default font color to white instead of black everywhere. I do not intend to modify anything else. I simply copied the primary and accent palette from the example, so I also feel they might not need any alterations.

Answer №1

It appears that your query has been addressed in this post: . Essentially, you will need to create a customized foreground map and integrate it into your theme. By combining the code snippet provided and the information from the aforementioned post, you can achieve something similar to the following for your styles.scss:

@import '~@angular/material/theming';

@include mat-core();

$sg-app-primary: mat-palette($mat-indigo);
$sg-app-accent:  mat-palette($mat-pink, A200, A100, A400);

$sg-app-theme: mat-light-theme($sg-app-primary, $sg-app-accent);

@function my-mat-light-theme-foreground($color) {
    @return (
        base:              $color,
        divider:           $white-12-opacity,
        dividers:          $white-12-opacity,
        disabled:          rgba($color, 0.38),
        disabled-button:   rgba($color, 0.38),
        disabled-text:     rgba($color, 0.38),
        hint-text:         rgba($color, 0.38),
        secondary-text:    rgba($color, 0.54),
        icon:              rgba($color, 0.54),
        icons:             rgba($color, 0.54),
        text:              rgba($color, 0.87),
        slider-off:        rgba($color, 0.26),
        slider-off-active: rgba($color, 0.38),
        slider-min:        rgba($color, 0.38)
    );
};

$white-foreground: my-mat-light-theme-foreground(white);
$my-app-theme-custom: map-merge($sg-app-theme, (foreground: $white-foreground));

@include angular-material-theme($my-app-theme-custom);

/* For the non-Angular Material items */
body {
    color: white;
}

Answer №2

Utilize utility functions that allow you to input primary, accent, and warn palettes while automatically generating background and foreground colors.

These convenient functions are included in the scss provided by angular material file, making it easy to incorporate them into your theme configurations.

Take a look at the following code snippets for reference, showcasing how they effortlessly include foregrounds and backgrounds for light and dark themes. It's truly impressive work by the team behind this functionality.

 @function _mat-create-light-color-config($primary, $accent, $warn: null) {
  @return (
    primary: $primary,
    accent: $accent,
    warn: if($warn != null, $warn, mat-palette($mat-red)),
    is-dark: false,
    foreground: $mat-light-theme-foreground,
    background: $mat-light-theme-background,
  );
}


@function _mat-create-dark-color-config($primary, $accent, $warn: null) {
  @return (
    primary: $primary,
    accent: $accent,
    warn: if($warn != null, $warn, mat-palette($mat-red)),
    is-dark: true,
    foreground: $mat-dark-theme-foreground,
    background: $mat-dark-theme-background,
  );
}

Answer №3

After experimenting with Angular 15 and avoiding custom functions, I discovered a solution that worked well for me.

@use '@angular/material' as mat;

...

$newFontColor: green;

// Accessing color parameter from our theme
$palette-color : map-get($mat-indigo, color);
// Extracting background parameter from color parameter
$foreground: map-get($palette-color, foreground);
$foreground: map_merge($foreground, (text: $newFontColor);
// Adjusting foreground parameter for palette
$palette-color: map_merge($palette-color, (foreground: $foreground));
// Updating palette for theme
$light-theme: map_merge($light-theme, (color: $palette-color));

@include mat.all-component-themes($light-theme);

I found guidance in this article that helped me connect the dots.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 8: Setting the Default Dropdown Option to the Newest Value in the Updated ArrayList

I am currently working with Angular 8 and TypeScript. After calling a service and updating the array collection, I want to automatically select the last aggregated value. However, I always want the placeholder to be shown. How can I achieve this? <nb- ...

The dimensions of the background for a span element

I have a span element with a background-color applied to it. Is there a way to adjust the size of this specific background? I attempted using background-size: auto 2px;, but it did not produce the desired effect. I am looking to make the background color ...

Issue with host-context scss rules not appearing in final production version

I am facing an issue in my Angular project where the scss rules that define how components should look when within the context of another component are not being applied when I build for production and put it live. Here is an example: :host-context(my-tabl ...

"Troubleshoot: Why is the Position Absolute Not Functioning in

Trying to achieve a layout similar to the last element at the bottom of the footer] 1, but getting something like this instead: https://i.sstatic.net/lXazk.png <footer> <div class="container-fluid"> <div class="row"> ...

Troubleshooting puppeteer PDF: Unable to set table height using CSS for tr:last-child { height: 100%; }

My Node.js code snippet is as follows: await page.setContent(html, {waitUntil: 'networkidle2'}); const pdf = await page.pdf({ format: 'A4', printBackground: true, margin: { top: '14mm', // default is 0, units: mm, c ...

Maximizing space efficiency in Bootstrap 5

Hello there I have a query; I am currently utilizing Bootstrap 5 for my website, and it's working well. However, I would like Bootstrap to fill the entire page instead of just 80%; (Refer to image) https://i.sstatic.net/Iswmu.jpg <head> ...

Ensure that the buttons are arranged in a single row and have horizontal overflow in the mobile version

After using Bootstrap, I managed to create a row with multiple buttons. I encountered an issue where the buttons do not align properly when viewed on mobile devices, as depicted in the image provided. Is there a solution to ensure that these buttons alwa ...

What is the method for inserting a vertical line between two div elements?

Can you show me how to add a vertical line between two div elements? .flex-container { display: -webkit-flex; margin: 0 auto; text-align: center; } .flex-container .column { width: 320px; text-align: center; } .vr { border-left: 2px dott ...

Aligning Checkbox Labels for Web Forms

Can an image convey more than a thousand words? I'm looking to align the second (and following) lines with the first one. Any suggestions? Html stands for <input><span>text</span> ...

How can I add a dropdown filter to column values in ag-grid?

I'm attempting to implement a dropdown filter for a column in an ag-grid using Angular 8. I've successfully displayed the column with the following code: columnDefs = [ { headerName: 'Id' , field: 'id' ...

Tips on transforming button-controlled tab pages in Bootstrap 2 to Bootstrap 3

I am currently in the process of upgrading my website from Bootstrap 2 to Bootstrap 3, and I have encountered an issue with a specific control that consists of multiple sets of tab pages controlled by toggle buttons. Here is an example of the functional c ...

Displaying a dynamic progress bar across all elements in fullscreen mode

I am looking to implement a full-screen indeterminate progress bar that overlays all screen elements. Here is my specific use case: When a user fills out a form, including an email field, the email id is checked against a database via ajax. While waiting ...

Add a touch of shadow to a stationary top banner as you scroll through the page

Is there a way to gradually fade in a shadow while scrolling, so that the shadow only reaches an opacity of 0.5 after scrolling a certain number of pixels? I've come across solutions to add a shadow or class to the top banner based on content position ...

The dropdown in the Material UI GridList appears excessively wide

Currently, I'm attempting to design a grid-shaped drop-down menu in Material UI. Most of the dropdowns available in the documentation are either a single column or a single row. I tried utilizing a menu component that includes a GridList, which almost ...

What is the best way to refine the dataSource for a table (mat-table) using ngx-daterangepicker-material?

I am facing a new challenge and feeling unsure about how to approach it. The issue at hand is filtering a table based on the date range obtained through the ngx-daterangepicker-material library. This library triggers events that provide a start date and a ...

Looking for an iframe that can adapt to varying content sizes and scale seamlessly with different screen dimensions?

I am new to advanced coding and currently working on my first responsive Wordpress site. I have a "Product Search" database/site that I'm trying to integrate into my website using an iFrame. I want the integration to look seamless without scroll bars ...

What is the best way to resolve the CSS border styling issue surrounding the anchor element?

I created a simple sign-up form for my school project where users can choose their roles. I'm facing an issue with the border under the anchor tag and I need help fixing it. How can I ensure that the space at the top is equal to the bottom as well? T ...

Tips for Resizing and Reviving Divs with jQuery

I have recently ventured into the world of web development to assist a family member with their website. My knowledge and experience are limited, but I am facing an interesting challenge. I am trying to manipulate certain divs as users scroll down the page ...

Saving data outside of the Angular 5 subscribe method involves storing the information in a

I need to store data received from an HTTP GET request and save it into another array within the component. getAlerts(){ this.ChatAlertsService.getAlerts() .subscribe((data) => { console.log('ngOnInit', data); ...

in Vue.js, extract the style of an element and apply it to a different element

Currently, I am using VUE.js 2 in my project. Here is my website's DOM structure: <aside :style="{height : height()}" class="col-sm-4 col-md-3 col-lg-3">...</aside> <main class="col-sm-8 col-md-9 col-lg-9" ref="userPanelMainContents" ...