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:

However, my current layout looks like this:

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

Changing a single variable into an array that holds the variable in JavaScript

Is there a way to change 5 into [5] using JavaScript? I need this functionality for a method that utilizes jQuery's $.inArray. It should be able to handle both scalar variables and arrays, converting scalars into arrays with a single element. ...

Is there a way to prevent links from opening in an iframe and have them open in the main window instead?

Imagine you have an iframe on your webpage: <iframe style="border-radius:15px;" width="190" height="450" frameborder="0" scrolling="no" marginheight="5" marginwidth="10" src="../img/interactive_map/interactive_sverige.htm"> </iframe> ...

Leveraging AJAX to assist in PHP for data parsing

I am currently exploring the world of AJAX and grappling with a unique situation. I am in the process of developing an HTML app that will be integrated into a mobile application using PhoneGap. The main objective of my project is for the HTML page to con ...

Navigating with NextJS to a personalized URL and managing the feedback from an external application

I'm currently in the process of developing an application that involves redirecting users to a specific URL, prompting them to open an app (with a url starting with zel:), and then sending a request back to my server. Here's the envisioned user j ...

Angular always ensures that the Ionic4 type homepage is consistently loaded

My Ionic4 App seems to consistently load the homepage despite my efforts to set different initializations such as loadChildren, component, and redirectTo. The issue persists with always loading the homepage. .app-routing.module.ts const routes: Routes = ...

When I type text into the form field, JavaScript causes a disruption in the form

I'm attempting to implement a dual-stage user onboarding procedure. Initially, users will input the industry they belong to and then provide a description. Upon clicking submit, my intention is to conceal that portion of the form and reveal the "Creat ...

Is it possible to locate and eliminate the apostrophe along with the preceding letter?

My objective is to tidy up a character string by removing elements that are not essential for the user and SEO, specifically the (letter before the apostrophes) in this case. I am looking for a regex solution or explanation of how to achieve this in PHP, a ...

Comparison between Mobile Phone's innerWidth and innerHeight Versus Resolution

My test phone, the Galaxy S3 Mini, has a resolution of 800x480 according to its specs. Strangely, when I run the following code in my HTML game: window.innerWidth window.innerHeight The width appears as 533 and the height as 295. I recently discovered ...

Perform the same actions on every element within the ul li

I'm facing an issue with my unordered list, where each list item contains a span element with an image inside. My goal is to set the background-image of each span to be the same as the image it contains, while also setting the opacity of the image to ...

Pass a delay to an Angular 2 component animation by defining it as an input parameter

Is it possible to set the delay of a component's animation directly from the HTML code? For example: HTML: <circles[delay]="'10000ms'"></circles> Typescript (ts): @Component({ selector: 'circles', templateUrl: ...

Converting ed25519 private key into OpenSSH Format using JavaScript

After generating an ed25519 key pair using the javascript crypto library, I am now faced with the challenge of saving the private key in openssh format. Despite attempting to use the sshpk library for this task, I encountered an issue where the exported k ...

Setting the dispatch type in Redux using Typescript

I'm new to typescript and I'm trying to figure out what type should be assigned to the dispatch function. Currently, I am using 'any', but is there a way to map all actions to it? Here's how my code looks like: interface PropType ...

Executing Node.js Function from an External File Dynamically

Is it possible to run a Node function from an external file that may be subject to change? main.js function read_external(){ var external = require('./external.js'); var result = external.result(); console.log(result); } setInterva ...

The content displayed in the PrimeNG p-table is limited to only the table name with no additional information

After upgrading Angular to version 9, I switched from p-dataTable to p-table in PrimeNG. With a table named users, I intended to display them on the screen upon rendering the view using the following HTML: users = ['one','two','thr ...

Can halting an ajax function mid-process prevent it from finishing?

My objective is to convert a video using ffmpeg, which tends to take a considerable amount of time to complete. I'm considering sending an ajax request to the server for this task, but I don't want the user to have to wait until the video convers ...

Adding external JSON data to a plain HTML document can be achieved through the process of

I have been experimenting with extracting data from an API in JSON format, but I am struggling to figure out how to convert the JSON tags into HTML elements. You can view a sample of the JSON data here. Does anyone know how to transform this JSON into DI ...

As I scroll, I hope the texts will stay fixed against the backdrop image

Is it possible to make the #home section fixed to the background while scrolling, including all the texts and buttons? I envision it like having texts embedded in an image. The text is located in the middle left of the page (let's keep this as default ...

How does the onclick event trigger even without physically clicking the button?

I am struggling with creating a simple button using mui. My intention is to activate a function only when the button is clicked, but for some reason, as soon as I enter the webpage, it triggers an alert automatically. This behavior is puzzling to me and ...

Tips on how to stop a webpage from scrolling and instead allow only the inner content to scroll

Check out my plunker for reference. The goal I am aiming for is as follows: The webpage should stay fixed and not scroll in any direction If the content overflows its container, a scroll bar should appear strictly within that container I have a main co ...

Using jQuery to swap an image with a div element and then using the image as the

I have a collection of images in a div, like so: <div class="image-container"> <img width="174" height="174" src="http://mysite.com/images/portfolio/p1.jpg" class="image-style" typeof="foaf:Image"> <img width="174" height="174" src= ...