Attempting to fill a template using ngfor, wherein the initial 2 elements are displayed in a row

I am attempting to complete a task where, using an ngFor directive to iterate through an array, I need to display the first 2 elements in a row and the remaining elements in a descending column. It should look like this:

first  second

third

fourth

fifth

..nth

To achieve this, I tried applying ngClass to conditionally style the first two elements like so:

                    <div class="d-flex flex-row">
                      <div *ngFor="let item of array; let i = index">
                        <span [ngClass]="{ 'd-flex flex-column': i > 1 }">
                          <div>{{item}}</div>
                        </span>
                      </div>
                    </div>

However, this approach is not working as intended since all elements are still displayed in a row. Any suggestions on how to improve this? Thank you in advance.

Answer №1

A container styled with CSS flex for alignment of content in the same row must have each content wrapped in its own div or span tag.

In the snippet below, I have eliminated the d-flex class from the first line and the flex-column class from the span element to ensure that the content stays on the same line for the initial two items.

Before attempting to place the first two elements within a flex container, it is necessary to have their values predefined. An alternative approach would be to store these initial values in a list within your component code, accessing them as the first item of an array and outputting them on each row.

Give this a try:

<div class="flex-row">
    <div *ngFor="let item of myArray; let i = index">
        <span *ngIf="i==0">
            <div class="d-flex">
                <div>{{myArray[0]}}</div>
                <div>&nbsp;</div>
                <div>{{myArray[1]}}</div>
            </div>
        </span>
        <span *ngIf="i==1">
        </span>
        <span *ngIf="i>1">
            {{item}}
        </span>
</div>

By following the above code structure, you should achieve the desired outcome.

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

Adjust the placement of image when changing the size of the window

Is there a way to prevent an image with a fixed position from covering up page content when the browser window is resized? <div > <img id="img1" class="wrapperImage"></img> </div> <style type="text/css"> .wrapperImag ...

Issue: ng test encountered a StaticInjectorError related to FormBuilder

I recently started using ng test to run tests on my Angular application. I utilized the Angular-Cli tool to create modules and components, and the .spec.ts files were generated automatically. During one of the tests, I encountered the following error: ...

Exploring the process of integrating absolute paths within a global SCSS file in a VueJS project

The webpack configuration in my vue.config.js file looks like this: const path = require("path"); module.exports = { pluginOptions: { 'style-resources-loader': { preProcessor: 'scss', patterns: [ "./src/style ...

Error: Unexpected character < in node_modules/angular2/ts/package.json syntax

I am currently working on developing an app with Angular 2 and have encountered an error during setup. The error message I received is: SyntaxError: /home/mts/Desktop/sampleProject/appSails/node_modules/angular2/ts/package.json: Unexpected token < at O ...

Looking to develop a swipeable card feature similar to Gmail using React JS (excluding React Native)?

Looking to replicate the swipe card functionality of Gmail in React Js, After creating the UI and implementing swiping using UseGeature, I am facing an issue with smooth animation when hiding or showing buttons using UseEffect. There is a 20px movement be ...

Enhancing productivity with a more streamlined process for creating a responsive website design through the optimization of

I recently had the task of placing two images in a band on a webpage and ensuring they were responsive to different screen sizes. Image 'a' represented a circular logo, while image 'b' was a rectangular logo. The red band served as the ...

Refreshing a webpage to accurately display changes made in a CRUD application without the need for a hard reset

My to-do app is almost fully functional - I can create items, mark them as completed, and delete them using a delete button. However, there's one issue: when I delete an item, the page doesn't update in real-time. I have to manually refresh the p ...

Navigating through dropdown browser menus using VBA

I am looking to automate the process of looping through a dropdown list on a specific website and downloading the latest file for each entry. I have successfully opened the website and clicked the "submit" button, but I am struggling to find a solution to ...

Tips for adding images using v-for in Vue 3

Having some trouble creating a set of images using a v-for loop, as it seems to be not recognizing the variables inside the tag. The picture is broken and the alt attribute just shows me {{ item.alt }}. Here is my HTML: <section class="our-technolo ...

Fluid Grid design showcasing a gap on the right side

After working on the Skeleton Framework and making adjustments to the grid design, I am facing a small gap issue on the right side. Despite things slowly falling into place, this minor gap is causing confusion. Please refer to the image below for clarifica ...

Ways to align a button in the center using Material-UI

I'm struggling to find a way to center buttons in Material-UI. Here is the code snippet I currently have: function BigCard(props) { const { classes } = props; return ( <div> <Card className={classes.card}> <C ...

What is the best way to render multiple components from a single file in the App.js?

I am currently in the process of learning React.js and I have a question regarding displaying multiple components in App.js. Specifically, I have two pages named Home.js and About.js. When running the code, if you click on "About Us", only the About Page ...

Calculating the Vertical Size of a Component within Django Template Design

Within a div element with a fixed width, I have a p element with the following CSS properties: height:100px; overflow:hidden; I am looking to implement a functionality where a MORE button is displayed if the text within the paragraph overflows and is hid ...

Obtaining the class value using the getAttribute function

Is there a way to extract and store the value of the class (for example, "AAABC") in a variable? I've attempted various keywords with the getAttribute method, but have had no success. Using terms like "class" returned "gwt-Label", while others yielded ...

Creating a CSS key animation for resizing an image: What is the correct way to make an image expand and contract

Check out this CSS key animation example: http://jsfiddle.net/ryurage/Q2ELp/ I'm attempting to make an image grow and occupy most of the window when triggered by user interaction like hovering or click-and-hold. Subsequently, I want the image to shr ...

I'm encountering difficulty accessing the Index value within the template's Ref

I'm having trouble accessing the index value inside the templateRef. It shows as undefined in the console. <ng-container *ngFor="let notification of notifications; let i = index"> <ng-template *ngTemplateOutlet="notificationPage ...

javascript - Retrieving a JSON string

I am currently working on a stock website where I need to retrieve JSON information from either Google's API or Yahoo's API. To test this functionality, I have used a replacement function to log the data onto a text box for testing purposes. Howe ...

I need assistance finding and selecting multiple checkboxes on the HTML page. Can someone help me locate them?

[![Click here for image][1]][1] [1]: https://i.sstatic.net/7ahkUEeK.png Can someone please provide guidance on enabling the check for this HTML page? I am having trouble locating the checkbox element. ...

Insert data into a drop-down menu and organize it

When I choose a value in one Select, the other options are removed and the previous value is added to them. How can I use JQuery to add the previous value to Select and then sort it alphabetically? ...

Enhance Your Website with Bootstrap 3.0 Affix

Looking to attach a right hand column to the top of the window as you scroll using Bootstrap 3.0 and 'affix' feature. Here is the HTML for the element: <div class="col-lg-4 right-hand-bar" data-spy="affix" data-offset-top="477"> This is ...