Using Angular with CSS Grid: Avoid distributing grid items evenly in terms of width

I am working on an app that utilizes a grid with rows and columns. One of the features allows me to dynamically remove rows, causing the remaining items to evenly distribute themselves across the width of the grid.

However, I now require a layout similar to flex but using grid instead. Specifically, I need each grid item to have a margin next to the adjacent item, as shown in this example, without distributing themselves across the entire width.

CSS

.column {
      padding: 10px;
      margin: 10px 0;
      display: grid;
      grid-auto-flow: column;
      .row-item {
        text-align: center;
        display: grid;
        grid-auto-rows: 25px;
        grid-row-gap: 10px;
        width: 9vw;
      }
}

HTML

<div class="column">
  <ng-container *ngFor="let jS of journeyStepDisplay">
    <div *ngIf="jS.display" class="row-item">
      <div class="column-item header">
        <p>{{ jS.name }}</p>
      </div>
    </div>
  </ng-container>
</div>

Answer №1

When you have specified minimum and/or maximum widths for grid items, you can utilize a combination of different grid properties to achieve the desired layout, such as

grid-template-columns: repeat(auto-fit, minmax(100px, 100px));

In this scenario, we are creating a grid where the items will be evenly distributed with a minimum and maximum width of 100px. If they exceed the row's width, a new row will automatically be added.

.container {
  height: 200px;
  width: 600px;
  gap: 5px;
  border: 2px solid red;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 100px));
  grid-template-rows: auto;
  padding: 10px;
}

.box {
  border: 2px solid blue;
}
<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

Answer №2

It is essential to specify the width for each individual item.

<div class="column">
  
    <div class="row-item">
      <div class="column-item header">
        <p>ciao</p>
      </div>
    </div>
  
  <div class="row-item">
      <div class="column-item header">
        <p>ciao2</p>
      </div>
    </div>
  
  <div class="row-item">
      <div class="column-item header">
        <p>ciao3</p>
      </div>
    </div>
  
</div>
.column {
  width: 100%;
  display: grid;
  grid-template-columns: 150px 150px 150px 150px;
  grid-template-rows: auto;
}

.row-item {
        text-align: center;
}

Here's a helpful guide. (I did not utilize the directive from angular, but you can incorporate it as needed)

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

Exploring the capabilities of Angular 8 Services using 'TestHttpInterceptor'!

My Angular front-end is relatively small, but it relies heavily on data from an external server. I'm encountering issues with my tests throwing a gyp error. While I can mock the service to test a simple component, this approach only allows me to rep ...

Unable to eliminate the beforeunload event

The code below is used to display a default pop-up when the page is refreshed or closed: var myEvent = window.attachEvent || window.addEventListener; var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, ...

Struggling to modify the page width using CSS and HTML on Wordpress, facing unexpected limitations

I've exhausted all my options. I've attempted to add a certain % page width globally using CSS in the editor, page template, and customizer but it's just not working. It's frustrating because I'm using the Twenty Twenty Two theme, ...

Unable to center div container with margin set to auto

I'm struggling to center my container on the website. My goal is to have it centered and take up about 80% of the page so I can incorporate an image slider and text inside. Using margin: 0 auto doesn't seem to be achieving what I want. The backgr ...

Retrieving the latest iteration of array object attributes and filling in form fields

My goal is to iterate through a data object using ng-repeat: <tbody data-ng-repeat="(contractIndex, contract) in contracts"> {{contracts}} <tr> <td class="col-md-4"> <div class="dropdown" style="width:100% ...

Turn off slider trace animation?

Check out the slider component in MUI here: https://mui.com/material-ui/react-slider/ I'm currently exploring how to disable the animation on the nub so it moves instantly to the new position. Any advice on how to achieve this? ...

What is the best way to manipulate the canvas "camera" in HTML?

I am currently developing a 3D game that involves navigating through skyboxes. My goal is to have the camera respond to user input, specifically detecting key presses (using WASD controls) to move the camera accordingly. Do you have any suggestions on how ...

Assigning the image source to match the image source from a different website using the image ID

Would it be possible to retrieve the URL of an image from a different webpage using its img ID, and then implement it as the image source on your own website? This way, if the image is updated on the original site, it will automatically update on yours as ...

Having trouble reaching a public method within an object passed to the @Input field of an Angular component

My configurator object declaration is as follows. export class Config { constructor(public index: number, public junk: string[] = []) { } public count() : number { return this.junk.length; } } After declaring it, I pass it into the input decorated fi ...

The absolute positioning of a div nested within another div is not being applied correctly

Currently, I have a few div elements here. Once I figure out the right combination of style elements, I will transfer them to a stylesheet. I'm attempting to make the last div appear with an absolute position in relation to the containing div ("one") ...

Simplest method for transforming static HTML files within SharePoint

My current challenge involves converting numerous static HTML documents to SharePoint (MOSS 2007). The documents can remain static, but they need to be integrated into the SharePoint site with its look and feel. Each page must be updated with the SharePoin ...

Angular 2 encountered an unexpected error when trying to access a URL: Response with status code 401 indicating unauthorized access

I've been working on implementing my login function and it seems to be logging in successfully. However, I've encountered an error that says: Uncaught exception: Response with status: 401 Unauthorized for URL: (I'm using Django for my backe ...

Discovering an element in an Array using Angular 2 and TypeScript

In my setup, I have a Component and a Service: Component: export class WebUserProfileViewComponent { persons: Person []; personId: number; constructor( params: RouteParams, private personService: PersonService) { ...

A helpful tip for disabling by overriding webkit-transform without removing the line

I needed to make some changes to a css file but didn't want to alter it directly for personal reasons. Instead, I created a new css file where I overrode the specific lines that needed changing. However, now I'm wondering how I can cancel out thi ...

Testing to submit a form using ReactiveForm can be done by reloading the form while it is being submitted

A tutorial I came across titled "Make Your Angular Form’s Error Messages Magically Appear" sparked the beginning of my journey. The tutorial featured a simple form with just one input field and one button. To trigger additional events on a lower level, I ...

Validating Forms using JQuery

I am currently working on a jQuery AJAX code to validate specific fields in an HTML form before submission. If any errors occur during validation, I want the error message to be displayed in a div with the ID "response." However, the code doesn't seem ...

Understanding the Code for Responsive Web Typography with line-height in ems

I am currently trying to delve into the CSS calculation process behind the styling in the code snippet provided. This code snippet is from a responsive WordPress theme, and I am struggling to decipher how the em values for the line-height of the <h> ...

Ensure that the radio button is set to its default option when the user accesses the page

I have a model with a 'PartActionType' enum which includes Transfer, Harvest, and Dispose options. public enum PartActionType { Transfer, Harvest, Dispose } On my view page, I am displaying these options using ...

Ways to implement global variables in JavaScript modules for wider accessibility

I am working on integrating input values into my budget app, but I've encountered an issue with retrieving the values from radio buttons as it shows up as undefined. I have created a global function to fetch the radio button value, while the rest of t ...

How to manually implement a scrollspy feature in Angular

I am facing an issue with implementing scrollspy inside a MatDialog. The event activate.bs.scrollspy doesn't appear to be triggering as expected. It seems like it might be firing at a window level and not highlighting the anchors on my navbar as I scr ...