Ways to retrieve or obtain the value of a CSS property in an Angular TypeScript file

Here is some code snippet from different files:

<div #sampleComponent class="cdt-sample-component"
[ngStyle]="{'height': (view_height) + 'px'}" >
  <app-component></app-component>
</div>

css file:

.cdt-sample-component {
    display: flex;
    flex: 1;
    height: calc(100% / 3);
}

}

ts file:

constructor(private renderer: Renderer2) {
}

ngAfterViewInit() {
    logger.info("NativeElement height " + this.renderer.selectRootElement(this.metricsComponent['nativeElement']).getAttribute('height'));
    }

The log printed in the ts file shows that the "height" attribute returns null. How can I retrieve the value of the height attribute defined in the CSS file for the "cdt-sample-component" div element?

This issue arises while using Angular 7. Any insight on how to access the height attribute value defined in the CSS file from the ts file would be greatly appreciated.

Answer №1

If you want to retrieve the height of an element in Angular, consider using ViewChild to access the element and then retrieve its clientHeight or offsetHeight property.

Controller

import { AfterViewInit, Component, ElementRef, ViewChild } from "@angular/core";

export class SomeComponent implements AfterViewInit {
  @ViewChild("sampleComponent") sampleComponent: ElementRef<any>;

  ngAfterViewInit() {
    console.log(this.sampleComponent.nativeElement.clientHeight);
  }
}

To learn more about the differences between clientHeight and offsetHeight, check out this link:

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 set the background opacity to 0.7 without impacting the content within it?

Currently, I'm facing an issue with two divs - the outer one is identified as "BD" and the inner one as "content". Whenever I attempt to reduce the opacity of BD's background-color, the content inside also becomes less opaque including both image ...

Customizing the width of the JQuery $ui.progressbar by overriding its properties

After spending some time, I successfully overrode the function that controls the width of the progress bar element in the jQuery UI widget version 1.12.1 from September 14, 2016. Initially, I needed the progress bar to completely fill a column in a table. ...

What exactly is the role of the @altfontfamily variable in Bootstrap?

Within the variables.less file, specifically in the typography section, you will find a variable named @altfontfamily. I am interested in utilizing the Alt Font Family but I am unsure of the process. Is there a specific class that needs to be called in or ...

Different methods to incorporate Glyphicons without relying on Bootstrap CSS

For a recent client project, I utilized basic HTML and CSS. However, the client expressed a desire for Glyphicons to be included in the website menus. Attempting to incorporate Glyphicons with Bootstrap CSS posed challenges as it affected other aspects of ...

Tips to prevent modal impacts on underlying elements?

When I click on a button, a modal pops up. However, the background contents seem to spread out and sometimes scroll automatically when the modal appears. I have searched for a solution to this issue but have been unsuccessful. Can anyone please help me ide ...

In my HTML document, I have three identical Id's and I need to figure out how to target the third one using CSS. Can you help

I am working on an HTML document that contains multiple instances of ids and classes. Here is a snippet of the code: <!DOCTYPE html> <html> <body> <div id="Caption_G" class="editor-group"> editor-group<br /> ...

Modify the CSS selector for a dropdown menu element upon selection

I have customized a select box using CSS, but I am facing an issue where the custom arrow remains upward even after selecting an option. I want the arrow to point downwards once an option is chosen and revert to the default position when clicked outside. ...

How can you change the list-style-type to switch between lower-alpha and decimal on nested items within an ordered list?

I am attempting to reverse the list-style-type of nested items within an ordered list, including sub-items and sub-sub-items. I have tried using CSS counters but it doesn't seem to be working. Can anyone point out what I might be doing incorrectly? ol ...

Customizing the Slider Range with HTML DOM Style's BackgroundImage Attribute

I have a slider range that I'd like to modify using JavaScript. Specifically, I want to change its background-image property. To achieve this, I attempted the following script: document.getElementById("range").style.backgroundImage = "linear-gradient ...

Optimal solution: How can we achieve a fixed header and a scrollable body for a table in the

Is there a better way to achieve this? HTML table with fixed headers? Seeking a solution for creating a table with a scrollable body and a static header. After exploring various options, it seems like most available codes are unreliable, not compatib ...

Consistent height constraints within table cells

Trying to create an HTML Email with fixed height blocks? Let's align those blocks! The attached demo shows disproportionate heights and misalignment issues. How can we resolve this problem? DEMO: http://jsfiddle.net/smu0gyx7/ ...

Angular 8 throwing an ExpressionChangedAfterItHasBeenCheckedError when a method is called within the ngOnInit function

I encountered a common issue with an Angular template. I have a standard template for all my pages, containing a *ngIf directive with a spinner and another one with the router-outlet. The behavior and visibility of the spinner are controlled by an interce ...

Combining CSS Selectors

Can you combine these selectors into one for ul#footer_navigate: ul#social_footer li a:link, ul#social_footer li a:visited {} I would like the same selector to apply for both anchor states on the ul with ID #footer_navigate. Is there a different approac ...

Display "No Results Found" in Angular and Ionic 4 when the observable is empty

Below is the HTML code: <ion-list> <ion-radio-group> <ion-item class="ion-no-padding" *ngFor="let service of services | async"> <ion-label> <ion-grid> <ion-row> < ...

Can the <article> tag and all its content be positioned in the center of the webpage?

Can the <article> and its content remain centrally aligned on the page? Check out my website here: Typically, when you visit a website, the article is positioned in the center of the page. However, in my case, the article only remains centered when ...

Troubleshooting: Angular version 4.3 Interceptor malfunctioning

I have been trying to implement new interceptors in Angular 4.3 to set the authorization header for all requests, but it doesn't seem to be working. I placed a breakpoint inside the interceptor's 'intercept' method, but the browser didn ...

Tips for troubleshooting errors when starting a new Angular project

Journey On my Windows machine, I initially had npm 6.4.1 and node 8.12.0 installed. I decided to upgrade in the following sequence: Started with upgrading npm Then moved on to upgrading node However, now when attempting to create a new app, I encounte ...

Find the identifier that does not currently exist in the collection of objects

There is a situation where I have an array and an object that consists of arrays of ids, which are essentially permission objects. My goal now is to extract the ids that do not exist in the given object. Can someone assist me with devising the necessary l ...

Ensure that the headers of the table are properly aligned with the

Here's the code snippet I'm currently working with: function deleteRow(row) { var i = row.parentNode.parentNode.rowIndex - 2; // this -> td -> tr // -2 because the first 2 rows are used for header var tbl = docu ...

What method can I use to adjust the font style when it overlays an image?

Sorry if this is a bit unclear, but I'm trying to figure out how to change only a section of my font when it overlaps with an image while scrolling. If you visit this example website, you'll see what I'm referring to: For a visual represen ...