Achieving dynamic text alignment within a Grid tile container using HTML and Angular

Here is the main section of my parent component.

<div class="o-tile-container  ">
                        <div *ngFor="let country of Countrys">
                            <app-country
                                [name]="name"
                                [count]="count"
                                [level]="'Country'"
                            ></app-country>
                        </div>
            </div>

This is the standard HTML code for my country child component:

<div [matRippleColor]="primary" class="m-tile" matRipple>
    <div class="a-tile-graph">
        <div class="titles">
            <div class="head" id="heading-name">{{name}}</div>
            <div class="sub">{{level}}</div>
        </div>
    </div>
    <div class="content-tile">
        <div class="o-tile-content">
            <div class="a-tile-title">{{name}}</div>
            <div class="a-tile-count">{{count}}</div>
        </div>
    </div>
</div>

A simple representation of how this works.

https://i.sstatic.net/7w8PB.png

In this setup, I aim to show the heading name at the same level within each tile and display the country level consistently across containers. How can I achieve this?

Answer №1

In order to dynamically align text, you can pass your style as an @Input. Below is a sample code that demonstrates how to achieve this. Feel free to check out the code and demo on StackBlitz by following this Demo LINK StackBlitz =>
** Parent HTML:**

<div class="o-tile-container  ">
                        <div *ngFor="let country of Countrys">
                            <app-country
                                [name]="country.name"
                                [count]="country.count"
                                [level]="country.level"
                                [mystyle]="country.style"
                            ></app-country>
                        </div>
</div>

Child HTML:

 <div style="border-style: solid;width:100px" [matRippleColor]="primary" class="m-tile" matRipple [ngStyle]="this.mystyle">
        <div class="a-tile-graph" >
            <div class="titles">
                <div class="head" id="heading-name"  >{{name}} </div>
                <div class="sub">{{level}}</div>
            </div>
        </div>
        <div class="content-tile">
            <div class="o-tile-content">
                <div class="a-tile-title">{{name}}</div>
                <div class="a-tile-count">{{count}}</div>
            </div>
        </div>
    </div>

TS:
Parent TS:

export class AppComponent  {
 Countrys:any=[];
  rippleColor: string = "white";
  constructor(){
    let c1=new Country();
    c1.count=1;
    c1.level='L1';
    c1.name='A';
    c1.style={'text-align':'center'}
    this.Countrys.push(c1);
    let c2=new Country();
    c2.count=2;
    c2.level='L2';
    c2.name='B';
    c2.style={'text-align':'right'};
    this.Countrys.push(c2);
    let c3=new Country();
    c3.count=3;
    c3.level='L3';
    c3.name='C';
    c3.style={'text-align':'left'};
    this.Countrys.push(c3);
  }
}
class Country{
name;string;
count:number;
level:string;
style:any;
}

Child TS:

export class CountryComponent implements OnInit {
  @Input() level:string;
  @Input() name:string;
  @Input() count:number;
  @Input() mystyle:any;
  constructor() {
   }

  ngOnInit() {
  }
}

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

Constantly receiving an undefined value when trying to access a variable in an ngrx Observable

Here is the code snippet I am working with: language.component.ts form: FormGroup; first: AbstractControl; second: AbstractControl; constructor(private _fb: FormBuilder) { this.first = new FormControl('', [Validators.required, Va ...

The data in Angular2 service is not being saved consistently

I'm diving into Angular for the first time and following along with this tutorial. One of the key features of my Angular app is the CartService, which handles my shopping cart, while the CartComponent displays it in the navbar, and the CartReviewComp ...

Entering numbers using <input type="number"> does not restrict invalid inputs, while accessing "element.value" does not give me the ability to make corrections

According to the MDN documentation on the topic of <input type="number">: It is said that they have built-in validation to reject entries that are not numerical. But does this mean it will only reject non-numerical inputs when trying to ...

What are the steps for adding information to a MySQL database with GWT, HTML, and either JDBC or Hibernate?

I have been working with GWT (Google Web Toolkit) version 2.5.1 and successfully developed a sample application to display the username and password. However, I am now facing challenges in inserting these values into a MySQL database table using JDBC or Hi ...

Find out if a dynamically imported component has finished loading in Nextjs

Here is a simplified version of my current situation import React, { useState } from 'react'; import dynamic from 'next/dynamic'; const DynamicImportedComponent = dynamic(() => import('Foo/baz'), { ssr: false, loading ...

Exploring the use of ASP:Label, <span>, and accessing elements by their ID

Attempting to access a control within a specific class has been challenging for me. HTML Code: <span class="GeoPanelHeaderLeft"> <asp:Literal ID="LiteralHeaderText" runat="server" Text="New Survey Ticket"></asp:Literal> & ...

Scrolling and adjusting window size while perfectly displaying images pixel by pixel using php and javascript

I'm currently working on a forum concept that will feature images. I'm looking to have the image displayed at default width and height, but also want users to be able to resize the window to view more of the image as needed. Additionally, I would ...

The parameter necessary for [Route: admin.request.update] is missing. The required URI is admin/request/{request}, and the missing parameter is 'request'

When attempting to access detail.blade.php, I encountered an error stating "Missing required parameter for [Route: admin.request.update] [URI: admin/request/{request}] [Missing parameter: request]." Despite following the steps and codes exactly as in my pr ...

Adjust the color of the input range slider using javascript

Is there a way to modify the color of my slider using <input type="range" id="input"> I attempted changing it with color, background-color, and bg-color but none seem to work... UPDATE: I am looking to alter it with javascript. Maybe something al ...

What is the reason for not being able to align breadcrumb text to the right

This section contains HTML code and CSS for a breadcrumb container. The issue is that the breadcrumb is currently displayed on the left side. How can the breadcrumb be right-aligned? .breadcrumb-container { font-family: "Work Sans", sans-serif; ...

Collecting information from form submissions, accessing data from the database, and displaying the results

I am currently working on a project using nodejs, express, and mongodb within the cloud9 IDE. My goal is to create a page with a form that allows users to input data, search for corresponding information in the database, and display that data on the same ...

jQuery SlideUp and SlideDown causing Margin Bug in Internet Explorer 7

When clicking the "more info" or "less info" buttons to slide content up or down, a spacing glitch is created in IE7. Using show/hide instead of slideUp/slideDown seems to solve the issue. Is there a way to make sliding work in IE7 without causing this pro ...

"Converting to Typescript resulted in the absence of a default export in the module

I converted the JavaScript code to TypeScript and encountered an issue: The module has no default export I tried importing using curly braces and exporting with module.exports, but neither solution worked. contactController.ts const contacts: String[ ...

Ways to position a single item in the middle of a multi-column layout

I need help with aligning dynamic images in a 2-column layout. What's the best way to center a single image or collapse the columns when there's only one image? Currently, the single image is always placed in the left column. Is there a CSS-only ...

What is the best way to display a div in Chrome without allowing any user interactions?

I currently have a <div> placed on top of my webpage that follows the mouse cursor. Occasionally, users are able to move the mouse quickly enough to enter the tracking <div>. Additionally, this <div> sometimes prevents users from clicking ...

What is the method for obtaining the width of a g element?

I am encountering an issue with retrieving the width of the <g></g> element using jQuery. Both .innerWidth() and width() methods are returning null for me. Is there a way to obtain the width of the <g></g> element using jQuery? I a ...

Initial binding of Angular2 ControlGroup valueChanges event

My form contains <input type="text"> elements and I've noticed that ControlGroup.valueChanges is triggered during initial data binding when using [ngFormModel] and ngControl. As a result, it gives the impression to the user that the form has al ...

Is it possible to load a webpage in a WebBrowser control without displaying certain HTML elements by their IDs

Is there a way to load a specific page using the WebBrowser control without displaying unwanted advertisement banners in the 'tb' DIV element? I've searched online and came across an example that uses the mshtml reference, but I can't ...

Is there a way to modify the default lite-server port in the Angular quickstart setup?

I recently obtained the Angular 4 quickstart app and decided to modify the default lite-server port by including "port": 8000 in bs-config.json like this: { "server": { "port": 8000, "baseDir": "src", "routes": { "/node_modules": "node ...

How can I make two lines equal in length by stretching them?

Looking to replicate the design shown in the image below using CSS: https://i.stack.imgur.com/cZiFT.png It's crucial for me that both lines are of equal length: https://i.stack.imgur.com/8phWR.png I attempted to recreate it with the following code ...