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 Angular Observable continues to show an array instead of a single string value

The project I am working on is a bit disorganized, so I will try to explain it as simply as possible. For context, the technologies being used include Angular, Spring, and Maven. However, I believe the only relevant part is Angular. My goal is to make a c ...

Unusual behavior observed in Safari regarding transitions and animations

I have encountered a strange bug in Safari 8 and 9 on desktop, as well as iOS 8.4 and newer on mobile devices. I am perplexed by why it is happening and how to resolve it. This issue can be replicated with 100% accuracy. Here are the steps to reproduce: ...

What is the best way to customize the endIcon of a styled-component button?

Presented here is a neatly styled button I've created import styled from 'styled-components'; import Button from '@material-ui/core/Button'; import ArrowForwardIosIcon from '@material-ui/icons/ArrowForwardIos'; const MyB ...

What is the best way to incorporate CSS into JavaScript code?

Here is the code I am working with: <script type = "text/javascript"> function pic1() { document.getElementById("img").src = "../images/images/1.png"; } function pic2() { document.getEl ...

Is there a way to create animated CSS box-shadow depth using jQuery or CSS3 transitions?

This code snippet applies delays but doesn't seem to update the style changes until the loop completes: for (i=20;i>=0;i--) { var boxShadow = i+"px "+i+"px "+i+"px #888"; $('article').css("box-shadow", boxShadow); ...

Preventing Context Menu from Appearing on Long Click in HTML5 Games

I'm attempting to utilize a similar technique as demonstrated in this stackoverflow post: How to disable the 'save image as' popup for smartphones for <input> However, I am encountering an issue where my button is not displaying at ...

Can someone please explain to me why the Transition effect is not functioning properly for input:checked::before?

Can someone please help me troubleshoot why the transition effect on the input's before element is not working when checked? If I can't solve this issue, I might have to consider alternative solutions. input[type="checkbox"]{ width: 160px ...

Utilize Jasmine to spy on an inner function and return a mock value from the last function call

I'm currently working on a Jasmine test where I have an object structure like this: class User { public getData(): void { return { getPersonalInfo: () => { ... } } } } Typically, I would access it as ...

The functionality of the delete button in Datatables is only operational on the initial page, failing to

My datatable is giving me trouble. The delete button in the action column only works on the first page, but not on the other pages. Here is the code for my delete button: <table id="example" class="table table-striped table-bordered" cellspacing="0" wi ...

What are the steps to achieve form styling using Bootstrap?

I am trying to achieve a specific style using the Bootstrap framework. I have been able to achieve something similar to what I want, but the issue is that the save button is not aligned to the right and is not taking up the available width. There is a gap ...

What is the best way to extract a variable from a URL and assign it to an Ionic variable

I am currently developing a project in Ionic 3 that utilizes the admob plugin. I have set up two variables (Mybanner and Myinterstital) to store the admob code, and I would like to retrieve the content of these variables from an external URL like this: ht ...

Adjust the height to match the shortest sibling child div height

In my layout, I have multiple rows with each row containing multiple columns. Within each column, there is a div and a paragraph - the div contains an image. My goal is to set the div with the class cover-bg to the lowest height of cover-bg in the same row ...

Creating artwork using a "graphite drawing tool" on a blank canvas

I created a script that draws a series of lines on a canvas, giving it a sketch-like appearance. However, I have encountered two issues with the script. Firstly, why is the y value appearing to be twice as much as it should be? Secondly, why is the line w ...

Troubleshooting Tips: Investigating a Service Call in AngularJS 2 using ES6 Syntax

MY DILEMMA: I have recently started learning Angular2 and found myself wanting to debug a service call. The service appears to have been properly called, as evidenced by the view display. However, when trying to log the content of the variable holding t ...

Is the Owl carousel Auto Play feature malfunctioning when hovered over?

I seem to be encountering an issue with the auto play functionality of the owl carousel. I have uploaded and included all the necessary files, and it is functioning properly when initially loaded. The auto play feature works perfectly, but when I hover ove ...

What causes certain properties of html and body elements to behave in this way?

I'm on a quest for understanding some mysteries. First riddle: Why does the body have a mysterious margin-top? html { height: 100%; background-color: red; } body { height: 100%; margin: 0; background-color: yellow; } <h1>Hello P ...

Using Angular to pass a class as a parameter in an HTTP GET request

I am currently working with a class that looks like this: export class CodeTable { public tableId: number; public connectionTable: number; public connectionCode: number; public code: number; ...

Unable to clear all checkboxes after deleting

In my application, there are 3 checkboxes along with a master checkbox that allows users to select or deselect all of them at once. Everything works fine with the master checkbox until I delete some rows from the table. After deleting data, I can check th ...

How to access elements by their class name in Angular

Recently, I encountered a situation with this specific span element: <span *ngFor="let list of lists[0].question; let i = index" id="word{{ i }}" (click)="changestyle($event)" class="highlight"> {{ list}} < ...

The Zurb Foundation grid system is causing issues when trying to implement vertical tabs

My vertical tabs are giving me trouble when I try to nest grid or block elements, causing everything to overflow outside the element. For example, in the image below, the numbers 1,2,3 should be in the right section. <div class="row"> <div class ...