How to set the default theme color for the mat-sidenav background in Angular 6 and 7?

Is there a way to make the background of a mat-sidenav match the theme color of my mat-toolbar?

In the file src\styles.scss, I have the following:

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

The template / HTML file includes:

<mat-toolbar color="primary">
    <button mat-icon-button (click)="sidenav.toggle()">
        <mat-icon>menu</mat-icon>
    </button>
    Title text
</mat-toolbar>

This displays an indigo menu bar with the menu icon and Title text. I now want a mat-sidenav below that (which will contain menu items) and I would like its background to match the color of the:

<mat-toolbar color="primary">

which is indigo according to the theme.

The code continues for rendering the mat-sidenav:

<mat-sidenav-container>
    <mat-sidenav #sidenav mode="side" opened>
        <app-vertical-menu></app-vertical-menu>  // renders the menu items
    </mat-sidenav>
    <mat-sidenav-content>
        <div class="app-main-area">
            <router-outlet></router-outlet>
        </div>
    </mat-sidenav-content>
</mat-sidenav-container>

However, the background and colors of the mat-sidenav don't reflect the applied theme, appearing blank instead.

I am aware that I can manually style the mat-sidenav background within the component by adding a class name in the template - for example:

...
<mat-sidenav #sidenav2 mode="side" opened class="vertical-menu">
...

and then define the style in the scss file:

.vertical-menu {
    background-color: navy !important;
}

But what I really want is to apply the theme color from the default theme (e.g. indigo-pink.css) so that the background color of mat-sidenav matches the color scheme used for the toolbar.

Answer №1

To implement a unique look for your component, you will need to create a custom theme. Here is an overview of the process:

  1. Define your theme in a file named style.scss
  2. Create a separate file for your component theme, for example
    my-component.component.scss-theme.scss
    . In this file, focus on theming aspects like color and typography while keeping other styling details (sizing, positioning, etc.) in the main component stylesheet.
  3. Include and reference the component theme within the style.scss file

You can explore a sample application demonstrating these concepts on Stackblitz. Take note that both mat-sidenav and mat-toolbar share the same theme. Feel free to experiment by applying different colors such as primary, accent, or warn to these components.

For more in-depth resources on Angular Material Themes, check out Tomas Trajan's blog post and the official Angular team documentation on Theming Your Own Components.

Update: December 12th, 2019

I have written a series of articles providing detailed insights into working with Angular Material Themes on Medium. Explore them here: Create, Understand, and Apply theme.

Update: October 1st, 2020

An updated series of articles delving into Angular Material Theme customization is now available on Medium. Dive into the topics of Creating, Understanding, and Applying a theme.

Answer №2

Although this may not be the preferred solution for you (or me), I went ahead and created a new class in my styles.css file that matches the primary color of the indigo-pink theme.

.primary-background { background-color: #3f51b5; }

Here's how I applied it to my mat-sidenav element:

<mat-sidenav class="primary-background"...

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

The Ajax request was a success, but I am unable to retrieve radio button values from the $_POST variable

My website operates fully asynchronously, generating and destroying HTML elements on button clicks that prevent navigation. In this section, I am creating a form with an array of radio boxes for rating from 1 to 10. This form is submitted using jQuery.aja ...

Executing Basic Calculations Instantly with Live Data in Qualtrics

I am encountering an issue with displaying the values on a slider in Qualtrics. I need to show the value where the respondent has placed the handle, as well as the complementary value from the other end of the scale. For example, if the user has chosen 55, ...

Creating a unique tab spacing effect using HTML and CSS

I'm currently working on my personal website and I'm looking to create a tab-like effect (similar to word processors) for showcasing projects along with their descriptions. Here's an example layout: ReallyLong NameProject Project1 Descr ...

Integrate a PHP form that includes a redirect feature and an optional opt-in box

I'm a beginner and I'm trying to enhance this code by adding some extra features. When the form is submitted, I want the page to redirect to an external URL like www.google.com The checkbox should be automatically checked and when the client re ...

Is there a way to manually trigger a re-render of all React components on a page generated using array.map?

Within my parent component (Game), I am rendering child components (Card) from an array. Additionally, there is a Menu component that triggers a callback to Game in order to change its state. When switching levels (via a button click on the Menu), I want a ...

Angular-cli is throwing an error stating it cannot locate the module '@ngtools/json-schema'

After running npm update in my project, I seem to be encountering an issue whenever I try to run ng serve. Error: Cannot find module '@ngtools/json-schema' Oddly enough, the @ngtools file is clearly present in my node_modules directory. I&apo ...

Using Angular to link Google Places API responses to a form: a guide on merging two different length objects with a shared key

I'm struggling with a key concept here regarding displaying the results of a places autocomplete query. I want to replace the types[0] name with more familiar terms such as suburb or city instead of sublocality_level_1 or administrative_area_level_1 ...

Toggle Canvas Visibility with Radio Button

As I immerse myself in learning Javascript and Canvas, the plethora of resources available has left me feeling a bit overwhelmed. Currently, I am working on a dress customization project using Canvas. // Here is a snippet of my HTML code <input type=" ...

Blazor: Passing a CSS class as a parameter to a child component

Is there a way to ensure css isolation works properly when passing a class as an argument to a child component? In the following code snippet, I anticipated the child component would display in blue, but that's not happening. Parent.razor <div cla ...

Link PayPal payments to the individual making the payment

On my website, I offer in-game features that can be purchased through a miniature store with PayPal buy now buttons. I'm looking for the best and most secure method to automatically deliver these in-game bonuses to users after they have completed thei ...

Encountering a bug with ng-transclude while working with md-button

My current use of md-button is as follows: <md-button aria-label="edit" target-mode="edit" style="z-index: 9;right: 10px; top: 10px; position: absolute;"> <i class="fa fa-check"></i> </md-button> This implementation seems to be c ...

When the mouse hovers over DIV2, the background image of DIV1 will be replaced

I have a full-screen background div with the ID #background-change. Within that full-screen background, I have 3 Divs named .fullscreen-column-1, .fullscreen-column-2, etc. My goal is to change the background image of #background-change when the mouseove ...

Change the value of the material slide toggle according to the user's response to the JavaScript 'confirm' dialogue

I am currently working on implementing an Angular Material Slide Toggle feature. I want to display a confirmation message if the user tries to switch the toggle from true to false, ensuring they really intend to do this. If the user chooses to cancel, I&ap ...

When resizing the window, divs shift positions and prevent the use of the horizontal scrollbar

I'm encountering an issue with my website layout. Specifically, I have a full-width navbar with some divs nested within it. However, when the page is resized, the position of these divs in the navbar shifts and a scrollbar appears at the bottom of the ...

Use the .html() function to alter the content of several sets of radio buttons simultaneously by changing

Here are the different options for a product. I need help with the .change() syntax to update pricing based on the following calculations: Bundle One + Marble Color = 15$ Bundle One + Black Color = 18$ Bundle Two [Most Popular] + Marble color = 25 ...

Arranging a Vertical Element Within a Rotated Holder

Who would have thought that geometry would come into play when working with CSS? I need help figuring out how to perfectly cover a rotated container with an upright background image. The goal is to hide the rotation on the sides and maintain dynamic contro ...

What strategies can I use to ensure consistent website layout across various zoom levels and browsers?

As a newcomer to web design, I am excited that my site is almost ready to go live. However, I have encountered some significant issues right before launch. When the site is zoomed in or out, the content becomes misaligned at certain zoom percentages. Add ...

Having trouble with your jQuery animation on a div?

Check out this jsFiddle example: http://jsfiddle.net/uM68j/ After clicking on one of the links in the demo, the bar is supposed to smoothly slide to the top. However, instead of animating, it immediately jumps to the top. How can I modify the code to ac ...

Are there any known browser compatibility issues with using "./" (dot slash) before HTML files?

Within the realm of shells, using ./ to indicate the current directory is quite common and generally not perceived as risky when specifying a path. On the other hand, in HTML coding, it's typical to omit ./ and instead directly specify the file name. ...

The list countdown for loop only appears in the initial iteration

Hey there, I'm currently facing an issue with duplicating my JavaScript countdowns and displaying images of cards on each loop iteration. Strangely, the countdown only appears in the first instance even though it's within the loop. I'm seeki ...