Is it possible to place Angular Material components using code?

Currently, I'm in the process of creating a responsive Angular application.

Is there any way to adjust the height and position of the

<mat-sidenav-content></mat-sidenav-content>
component in Angular Material programmatically without relying on CSS?

I attempted to use CSS for this purpose, but it didn't yield the expected results.

For instance, in JavaScript:

const box = document.getElementById('box');
box.style.position = 'absolute';
box.style.top = '150px';
box.style.left = '150px';

I am looking to dynamically set the height of both the <mat-sidenav> and <mat-sidenav-content> components upon page load.

The desired layout should resemble this:

https://i.sstatic.net/Tv1kS.jpg

However, my current layout looks like this:

https://i.sstatic.net/AO1jG.jpg

Below is the code snippet:

<header>
    <mat-toolbar color="primary">
        <button (click)="snav.toggle()" mat-icon-button>
            <mat-icon>menu</mat-icon>
        </button>
        <span>My App</span>
        <span class="mat-toolbar-spacer"></span>
        <button mat-icon-button [matMenuTriggerFor]="menu">
            <mat-icon>more_vert</mat-icon>
        </button>
        <mat-menu #menu="matMenu">
            <button mat-menu-item>
                <mat-icon>dialpad</mat-icon>
                <span>Redial</span>
            </button>
            <button mat-menu-item disabled>
                <mat-icon>voicemail</mat-icon>
                <span>Check voice mail</span>
            </button>
            <button mat-menu-item>
                <mat-icon>notifications_off</mat-icon>
                <span>Disable alerts</span>
            </button>
        </mat-menu>
    </mat-toolbar>
</header>

<mat-sidenav-container class="mat-sidenav-container" [style.marginTop.px]="mobileQuery.matches ? 56 : 0">
    <mat-sidenav #snav [opened] = "!mobileQuery.matches" [mode]="mobileQuery.matches ? 'over' : 'side'" [fixedInViewport]="mobileQuery.matches"
        fixedTopGap="56">
        <mat-nav-list>
            <a mat-list-item routerLink="." *ngFor="let nav of fillerNav">{{nav}}</a>
        </mat-nav-list>
    </mat-sidenav>

    <mat-sidenav-content #matSidenavContent >
        <p *ngFor="let content of fillerContent">{{content}}</p>
    </mat-sidenav-content>
</mat-sidenav-container>


<!-- Additional styling -->
.mat-toolbar-spacer {
    flex: 1 1 auto;
}

header {
    width: 100%;
    position: fixed;
    top: 0;
    z-index: 100;
    display: flex;
}

footer {
    height: auto;
    background-color: #F5F5F5;
    text-align: center;
    padding: 20px 0px 20px 0px;
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

mat-sidenav-container {
    background-color: #FFFFFF;
    height: auto;
    overflow: auto;
}

mat-sidenav{
    margin-top: 65px;
    position: fixed;
    margin-bottom: 60px;
}

mat-sidenav-content{
    padding: 20px;
    margin-top: 60px;
    overflow: auto;
    /*height:520px;*/ /* i don't want to set it here */
    height:100%;
}

mat-nav-list{
    width: 250px;
    padding: 5px;
}

mat-nav-list mat-list-item{
    width: 100%;
    padding-right: 100px;
}

@media (max-width: 767.98px) {
    mat-nav-list{
        margin-top: 0px;
        width: auto;
    }

    mat-sidenav{
        margin-top: 0px;
        margin-bottom: 0px;
    }

    mat-sidenav-content{
        padding: 20px;
        margin-top: 0px;
        height:auto;
    }
    
    footer {
        position:initial;
        left:initial;
        bottom: initial;
    }
}

Answer №1

Check out this StackBlitz demo showcasing how to utilize CSS's flex box for component arrangement.

Ensure your styles.css includes the following:

html,
body {
  height: 100%;
  width: 100%;
}

Your app.component.html structure should resemble this:

<mat-toolbar color="primary">
  <button mat-icon-button class="example-icon">
    <mat-icon>menu</mat-icon>
  </button>
  <span>My App</span>
</mat-toolbar>

<mat-drawer-container class="container">
  <mat-drawer mode="side" opened>
     Side Nav content here!
  </mat-drawer>
  <mat-drawer-content>
    <div class="content">
      Main content here!
    </div>
    <div class="footer">
        Footer content here!
    </div>
  </mat-drawer-content>
</mat-drawer-container>

To style and arrange the components, add this extra CSS:

:host {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

.spacer {
  flex: 1 1 auto;
}

mat-drawer-container {
  height: 100%;
  display: flex;
}

mat-drawer {
  width: 20%;
  overflow-y: auto;
}

mat-drawer-content {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.content {
  height: 100%;
  overflow-y: auto;
  padding: 10px;
}

.footer {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 90px; //fixed height for footer

  border: 1px solid blue;
}

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

What is the best way to access the req.user variable from Passport in client-side JavaScript?

I have successfully implemented Passport authentication in my Express application and everything is working perfectly. On my index page, I am displaying req.user as follows: <% if (!isAuthenticated) { %> <a id="signIn" href="/login">Sign I ...

Converting function arguments into key/value pairs: A simple guide

I am looking for a way to achieve the following in NodeJS: a = 10 b = 20 c = 30 d = 40 ...... ...... function createObject(a, b, c, d, ....) => { // This function is expected to return an object. // return { a : 10, b : 20 ...

Incorporating Angular: A guide to removing a specific element from an HTML array using line breaks

For instance, here is a screenshot of an example: https://i.stack.imgur.com/UlP23.png In the section where I labeled "I want to go down a line", I have an array containing errors related to usernames and passwords. This is how the component.ts file looks ...

Unraveling JavaScript using regular expressions in Python

I need to parse some JavaScript text using Python. Within the JS code, there is a variable like this: this.products = ko.observableArray([#here is some json, #here is some json]) The observableArray can hold a single JSON object like: observableArray({&a ...

Protractor's count() function fails to execute properly when called outside of a promise loop

var alerts = element.all(by.xpath("//div[@class='notification-content']")); alerts.count().then(function (val) { console.log(val); }); let compareValue = val; Is there a way to access the 'value' outside of the promise l ...

Is there a way to access the result variable outside of the lambda function?

My goal is to retrieve data from an external API using Typescript. Below is the function I have written: export class BarChartcomponent implements OnInit{ public chart: any; data: any = []; constructor(private service:PostService) { } ngOnInit( ...

How to Manually Update the Angular Release Version in your environment.ts File using the CLI

Within the environment.ts file, we store the release version. I am looking for a command to increment the minor version from the Command Line Interface (CLI) in order to use it within a Jenkins build job. export const environment = { … version: ' ...

ng-class not functioning properly when invoked

In my controller, I have the following function: $scope.menus = {}; $http.get('web/core/components/home/nav.json').success(function (data) { $scope.menus = data; $scope.validaMenu(); }).error(function () { console.log('ERRO') }); ...

Tracking the frequency of text values that have been clicked on

Feeling uncertain about the best direction to take. In a table of unknown length, the first column consists of clickable links, while the second column contains corresponding text. The number of rows in this table is variable and depends on search resul ...

How to eliminate cell borders in a Vaadin table

I recently set up a table in Eclipse using Vaadin for assistance. After some trial and error, I successfully removed the borders of the table with the following line of code: tblResetButton.addStyleName(Reindeer.TABLE_BORDERLESS) ; However, there is sti ...

Looking for assistance in creating a stunning portfolio showcase?

As I work on building my portfolio webpage, I've realized that with my limited knowledge of HTML/CSS, creating a great gallery might be challenging. In search of an efficient solution, I ventured across the web but couldn't find anything that ful ...

TypeAhead.js and Bloodhound displaying an uneven quantity of search outcomes

I have a frontend setup with TypeAhead and Bloodhound integration, fetching JSON data from a Play/Scala server. The version of Typeahead being used is 0.11.1. Here is how the implementation looks: HTML: <div id="typeahead" class="col-md-8"> < ...

How can I relocate an object to a different position within THREE.JS?

I'm trying to figure out how to move one object to the position of another object. I found some suggestions online to use the TWEEN library, but I'm having trouble integrating it into my code. Any help would be greatly appreciated :) <scrip ...

Issue with array push not working within nested Promise

I recently encountered an issue with my Express API route that retrieves an artist's details along with an array of releases for that artist. My current goal is to iterate over this array, extract each URL, and then make additional API calls to retri ...

Utilizing a switch statement in Jquery to target specific input elements for focus

My Objective As a user presses enter, I want to target specific input elements with a data-index attribute value between 0-2 and of type text. Then, I plan to check their attribute values using a switch statement to perform certain actions. Presented bel ...

Is there a way for me to retrieve the pageProbs that I have shared with a Component in _app.js?

Upon the loading of my website, I am fetching a large amount of data using getInitialProps. MyApp.getInitialProps = async (appContext) => { // Calls page's `getInitialProps` and fills `appProps.pageProps` const appProps = await App.getInitialProps( ...

Is there a way to adjust a 5-minute countdown interval timer by 1 minute in a react JS application?

I am in need of creating a 5-minute interval timer using react JS, with a 1-minute offset. The current timer I have functions like this: 1:00 => 1:05 => 1:10 => 1:15 => 1:20. However, I require it to be adjusted to display: 1:01 => 1:0 ...

PHP and MySQL with jQuery and AJAX combine to create a real-time news feed update system similar to Twitter

Is there a way to automate the news feed on my website so that it periodically checks for new status updates? I would like it to display a button labeled "(?) New Messages" when there are new updates, and then load only the new ones when the button is clic ...

Exploring the Benefits of Utilizing External APIs in Next JS 13 Server-side Operations

Can someone provide more detail to explain data revalidation in Next JS 13? Please refer to this question on Stack Overflow. I am currently utilizing the new directory features for data fetching in Next JS 13. According to the documentation, it is recomme ...

Incorporating CodeMirror into Angular2 using TypeScript

I've been working on integrating a CodeMirror editor into an Angular2 project, but I'm encountering some issues with the instantiation of the editor. Here is my code snippet: editor.component.ts import {Component} from 'angular2/core' ...