Arrange items in a single parent Div in flex-column format, aligning them to the left and

In my Angular application, I am experimenting with stacking Bootstrap toast elements in a customized vertical layout. Specifically, I want certain toast elements to be positioned on the left side of the page (using the align-items-start style) and others on the right side (with the align-items-end style). However, all of these elements are currently nested under one parent Div.

Here's the code snippet:

  1. Parent component template
<div class="toast-container d-flex flex-column align-items-start">

     <app-share-comment [comment]='comment' *ngFor="let comment of comments" >
   </app-share-comment>

 </div>
  1. Child app-share-comment template along with its CSS and TypeScript
<div class="toast" [ngStyle]="style" data-autohide="false">
    <div class="toast-header">
      <strong class="mr-auto text-primary">{{comment.username}}</strong>
      <time class="timeago" [dateTime]="commentTime"></time>    
    </div>
    <div class="toast-body">
      {{comment.comment}}
    </div>
  </div>
.toast{
    min-width: 12vw;
    width: fit-content;
    margin: 0.6rem;
    position: relative
}
  ngOnInit(): void {
    (<any>$('.toast')).toast('show');

    this.style = this.getStyle()
  }

  getStyle(): Object {
    if (this.comment.username == 'wayne'){
      return {
        left : 12 +'rem'
      };
    }

With the current implementation, I am achieving the following effect: https://i.sstatic.net/aNHD3.png

However, what I expect is that when items are selected to align to the right, they should be aligned with the right border instead of the left border, while other items remain aligned to the left border.

I have tried various flex properties but so far haven't been successful. Can anyone provide some guidance or assistance?

I have also created a StackBlitz demo for reference: https://stackblitz.com/edit/angular-ivy-jbp7ua

Answer №1

Here are the modifications I made to your Stackblitz:

Improved Stackblitz Link :- https://stackblitz.com/edit/angular-ivy-tkgnym?file=src%2Fapp%2Fapp.component.css

Changes in app.component.css :-

p {
  font-family: Lato;
}
.toast-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.justify-content-end {
  justify-content: flex-end;
}
.my-toast {
  display: flex;
  width: 100%;
}

Modifications in app.component.html

<div class="toast-container">
  <div class="my-toast" [ngClass]="{'justify-content-end': comment?.username === 'right'}" *ngFor="let comment of comments">
    <hello [comment]='comment'>
    </hello>
  </div>
</div>

Updates in hello.component.ts (template)

<div class="toast">
      <div class="toast-header">
        <strong class="mr-auto text-primary">{{ comment.username }}</strong>
      </div>
      <div class="toast-body">
        {{ comment.comment }}
      </div>
    </div>

Adjusted hello.component.ts (style)

.toast {
        border: solid;
        min-width: 12vw;
        margin: 0.6rem;
        display: flex;
        flex-direction: column;
        width: max-content;
      }
      :host {
        display: flex;
      }

Answer №2

try using this

fetchStyle(): Object {
    if (this.note.author == "correct") {
      return {
        padding: '8px',
        border: '1px solid #ccc'
      };
    }
  }

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 could be causing the incorrect styling of the <h1> tag and certain Bootstrap elements in my Vuejs project when I import Buefy?

In my webpack-simple Vue.js project, I am utilizing Bootstrap 4 and Buefy. However, upon importing Buefy as per the documentation, I noticed that my <tag does not resize the text correctly and the nav-bar in Bootstrap 4 is displaying the wrong width. T ...

Learning to retrieve the value from a dynamically generated input tag in a TypeScript file

<div *ngFor="let task of arrayList"> <input id="task.fieldName" *ngIf="task.key === 'Others'" type="text" class="form-control"> </div> When dealing with dynamically created input fields based on a condition, the challenge is ac ...

Learn how to use Angular2 or TypeScript to display 'unsubscribe' and 'subscribe' text on a toggle button

I'm working on a toggle button that initially displays the word subscribe on the thumb. When the toggle is disabled, I want it to show unsubscribe instead. Can someone please help me achieve this functionality? Here's the code snippet: <md-s ...

Angular 4: Implementing toggle switch functionality in Angular 4 by binding boolean values retrieved from the database

Within my application, I am facing an issue with binding a toggle switch to data stored in the database. The data in the database is represented by a field called Status, which can have values of True or False. My goal is to incorporate toggle switch butto ...

Webstorm encounters difficulties compiling Sass

While attempting to utilize Sass in the Webstorm IDE, I noticed that it is defaulting to Ruby 1.8 (OS Default) instead of my preferred RVM Ruby Version (1.9.x). To address this issue, I tried setting the path for Sass in the Watcher-Configuration: PATH=$ ...

ReactJs CSS - This file type requires a specific loader for processing. There are currently no loaders configured to handle this file

I've noticed that this issue has been raised multiple times before. Despite going through all the questions, I still can't seem to resolve it. The transition from Typescript to Javascript went smoothly until I reached the implementation of CSS. U ...

Tips for ensuring that the lightbox scrolling feature only affects the lightbox and not the entire page

Currently, I am utilizing the lightbox_me jQuery plugin to trigger a lightbox when a user clicks on a product. The issue I am facing is that the lightbox content often extends below the visible area, causing the entire page to scroll when using the right s ...

Do not display large numbers within an HTML card

I have https://i.sstatic.net/DkowD.png this card here and displaying dynamic data inside it. The number is quite large, so I would like it to appear as 0.600000+. If a user hovers over the number, a tooltip should display the full number. How can I achieve ...

The art of positioning images and creatively cropping

Seeking advice on allowing users to dynamically position and clip an image on a webpage. I've tried using CSS and JavaScript for clipping and resizing, but it's not working as expected. If PHP could provide a solution, that would be ideal for my ...

A complete guide to incorporating Angular to hide a Bootstrap 4 Modal using jQuery

After adding jQuery and Bootstrap to the package, I encountered an issue where the modal function was not working properly. Typescript & HTML import $ from 'jquery'; export class AppComponent { name = 'Angular ...

Alignment issue with React JSX background position; image not centered on the right in Material UI Grid item

I've recently completed a single-page website portfolio, but I'm encountering an issue with setting the background position of the home screen to center right. You can view my site here: Here is the link to my repository: https://github.com/Bolm ...

Angular Notification not visible

I have been attempting to display a notification after clicking a button using the angular-notifier library (version: 4.1.1). To accomplish this, I found guidance on a website called this. Despite following the instructions, the notification fails to app ...

Set the jQuery cookie to change the CSS property to hide the element

Currently, I am utilizing the jquery cookie plugin to make the browser remember the state of a CSS after refreshing the page. Below is an excerpt of my code where a button is clicked: $('#cartHolder').attr('style','display: no ...

What could be causing my application to not locate the jquery module? ERROR: ENOENT: The specified file or directory cannot be found

I've decided to bring over an Angular project that already has a bootstrap template integrated into my own project. (Still unsure if this was the right move) One of the steps I took was adding these lines to the angular.json file: "styles": [ ...

The linear gradient feature in asp.net does not function properly on Internet Explorer versions 6 through 8

After running the following code below, it appears that the linear gradient is not functioning properly in IE6 to IE8. background-image: linear-gradient(45deg, white 0%, gray 50%, black 100%); I attempted to resolve this issue by adding the code snippet ...

Adjust the initial slider according to the values displayed in the following four sliders

Having an issue updating the slider value in the first one based on selected values from four other sliders. The selected value should not exceed 50, which is the maximum value in the first slider. Link to Working Fiddle : Below is the HTML code : & ...

Angular2 - Model not being refreshed by Directive

I have implemented a directive on an HTML input box to handle numeric values. The directive is meant to "clean" the number (removing commas, dollar signs, etc) when a user pastes a number into the textbox. Although the cleaning code works properly, the iss ...

Confused by navigating with php

I've been attempting to convert this link into a PHP foreach loop without any success, and I'm new to Stack Overflow. I've used an array to generate the code, but I am uncertain of how to transform this code into a foreach loop. <ul class ...

The control options on the AGM map are experiencing functionality issues

Upon setting the options for agm-maps, I noticed that my options briefly appear and then disappear. This behavior puzzled me, so I decided to create a simple project in Stackblitz to showcase the issue. Below, you'll find the code snippets: code samp ...

Encountering challenges while integrating Angular with a Laravel forum creation project

Currently, I am working on building a forum application that involves users, posts, and comments using Laravel. However, the next step in my project requires integrating Angular, which is new territory for me and I'm not sure where to start. I have a ...