best practices for creating space between flexbox items

My task involves presenting a continuous scroll list of scheduled jobs using Angular Material. Each item in the list needs to be divided into three columns utilizing flexbox. Despite my efforts, I am struggling with adding space between the columns within the list.

Below is the HTML code snippet:

<app-nav>

  <div class="grid-container">
    <div class="row1">

      <mat-grid-list cols="6" rowHeight="100px">
        <mat-grid-tile [colspan]="1" [rowspan]="1" [style.background]="lightgreen">
          <h2>All Scheduled Jobs</h2>
        </mat-grid-tile>
        <mat-grid-tile [colspan]="4" [rowspan]="1" [style.background]="lightgreen">

        </mat-grid-tile>
        <mat-grid-tile [colspan]="1" [rowspan]="1" [style.background]="lightgreen">
          <button (click)="openCreateScheduledJobDialog()" mat-mini-fab color="primary"
            aria-label="Example icon button with a plus one icon">
            <mat-icon>plus_one</mat-icon>
          </button>
        </mat-grid-tile>
      </mat-grid-list>


    </div>
    <div class="row2"> // issue lying here:

      <cdk-virtual-scroll-viewport itemSize="70" class="example-viewport">
        <mat-list>
          <mat-list-item *cdkVirtualFor="let scheduledjob of scheduledJobs" class="example-item">
            <div class="flex-container-list-item"> // problematic flex display
              <div class="details">
        
                <h3 matLine>{{getJob(scheduledjob.jobID)}} Job</h3>
                <h4 matLine> Status: {{getJobStatus(scheduledjob.isActive)}}</h4>
                <p matLine>
                  <span> Start time: {{scheduledjob.startTime | date}} </span>
                </p>
                <p matLine>
                  <span> End time: {{scheduledjob.endTime | date}}</span>
                </p>
                <p matLine>
                  <span> Runs Every: {{scheduledjob.daysFrequency}} days
                  </span>

                </p>

                <p matLine>

                  <button mat-stroked-button color="primary"
                    (click)="openMachineDetailsDialog(scheduledjob.machineSerialNumber)">Machine Details</button>
                </p>
              </div>
              <div class="edit"> <button mat-flat-button color="primary" (click)="openUpdateScheduledJobDialog(scheduledjob)">Edit</button></div>
              <div class="delete"> <button mat-raised-button (click)="delete(scheduledjob)" color="warn">Delete</button>
              </div>
            </div>
            <mat-divider></mat-divider>
          </mat-list-item>

        </mat-list>

      </cdk-virtual-scroll-viewport>

    </div>
    <div class="row3">
    </div>
  </div>
</app-nav>

  • Note that the angular material cdk-virtual-scroll-viewport is being utilized.

Here is the accompanying CSS code:

.row1 {
  grid-area: row1;
}

.row2 {
  grid-area: row2;

}

.row3 {
  grid-area: row3;
}


.grid-container {
  display: grid;
  grid-template-areas:
    'row1 row1 row1'
    'row2 row2 row2'
    'row3 row3 row3 '
  ;
  grid-gap: 2px;
  padding: 2px;
}

.grid-container>div {
  text-align: center;
  padding: 10px 0;
  font-size: 15px;
}

.example-viewport {
  height: 600px;
  border: 2px solid gray;
}

.example-item {
  height: 50px;
}

/* ******************* list item flex ***************** */

.details {
background-color:lightcoral ;
}

.edit {
 background-color: lightgreen;
}

.delete {
background-color: lightskyblue;
}


.flex-container-list-item {
  display: flex;
  border: 6px solid red;
  justify-content: space-between;
  --gap: 10rem;
  flex-wrap: wrap;
}

.grid-container-list-item>div {
  text-align: center;
  padding: 10px 0;
  font-size: 15px;  
}

https://i.stack.imgur.com/QFbI8.png

Answer №1

To create space between flex items, you can utilize the gap property on the flex container or apply

[margin](http://developer.mozilla.org/en-US/docs/Web/CSS/margin)
to individual flex items.

Your current approach may not be effective because while you define a CSS custom property --gap: 10rem for the flex container, it seems like this property is not being actually utilized in your provided code snippet.

The use of gap will establish a fixed amount of spacing between the flex items, with good browser support overall except for Safari versions before 14.1.

Alternatively, an option could be to set margin-right on all flex items other than the last item to achieve a similar spacing effect.

Below is a simplified working example:

.flex-container-list-item {
  display: flex;
  border: 6px solid red;
  gap: 2rem;
  flex-wrap: wrap;
}

.details {
  background-color: lightcoral;
}

.edit {
  background-color: lightgreen;
}

.delete {
  background-color: lightskyblue;
}
<div class="flex-container-list-item">
  <div class="details">
    <h3>Job</h3>
    <h4>Status: active</h4>
    <p>
      <span> Start time: 2 PM</span>
    </p>
    <p>
      <span> End time: 4 PM</span>
    </p>
    <p>
      <span> Runs Every: 3 days </span>
    </p>

    <p>
      <button>Machine Details</button>
    </p>
  </div>
  <div class="edit">
    <button
      mat-flat-button
      color="primary"
      (click)="openUpdateScheduledJobDialog(scheduledjob)"
    >
      Edit
    </button>
  </div>
  <div class="delete">
    <button mat-raised-button (click)="delete(scheduledjob)" color="warn">
      Delete
    </button>
  </div>
</div>

Answer №2

To achieve the desired outcome, make sure to follow these steps:

.container{
   
    display : flex;
    flex-flow: row wrap;
    justify-content: space-between;
}

.item{
    margin: 10px;
    height: 100px;
    width: 100px;
}

.item1{
   background-color: red;  
}

.item2{
   background-color: yellow;  
}
<div class="container">
   <div class="item item1"></div>
   <div class="item item2"></div>
</div>

Although your question may not be entirely clear, you can update your HTML and css files as follows:

.flex-container-list-item{
  
  display:  flex;
  flex-flow: row wrap;
  justify-content: space-between;

}
<div class="flex-container-list-item">
  <div class="details">details contents</div>
  <div class="action">
    <button
      mat-flat-button
      color="primary"
      (click)="openUpdateScheduledJobDialog(scheduledjob)"
    >
      Edit
    </button>

    <button mat-raised-button (click)="delete(scheduledjob)" color="warn">
      Delete
    </button>
  </div>
</div>

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

I am working on two Angular projects and I am looking to integrate one of them into the other project

In my development work, I am juggling two Angular projects on Github: AngularApp1 and AngularApp2. AngularApp1 is a robust project with many components, while AngularApp2 serves a more specific purpose. My goal is to have a button in AngularApp1 that open ...

Can you have two navigation bars and a picture all in one Bootstrap layout?

I have a requirement to use a single image on two different Bootstrap navbars. However, the issue is that the image appears split between the two navbars.https://i.stack.imgur.com/jUnIL.png My goal is to display the full image without it being divided bet ...

What's the best way to arrange my containers for optimal placement?

I'm attempting to create a Thymeleaf template for rendering a printable PDF. Here is what I currently have: <html xmlns:th="http://www.thymeleaf.org" > <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" ...

Attempting to create a single function that can be utilized with numerous divs that share the same class in jQuery

Currently, I am working on creating a basic jquery gallery viewer. In my code, I have the following structure: <div class="photoset"> <img /> <img /> </div> <div class="photoset"> <img /> <img /> <i ...

Adjusting the appearance of internal components with material-ui icons

Currently working on a project using react and material-ui where I am looking to customize the styles of specific elements within components. An example is the ListItem component which includes primaryText, leftIcon among others, and my goal is to modify t ...

Bizarre CSS Transitions and Scaling Quirks in Webkit

Working on a website, you can check it out here In Chrome, some strange behavior occurs when the window reaches a certain resolution. Specifically, at 943 pixels width, white bars appear next to images when hovering over them, giving the impression that t ...

When an additional data stream is introduced, Resolver is unable to effectively resolve the issue

I have been working on implementing a resolver to fetch data based on the parameters provided by the route. However, I encountered an issue where the resolver does not resolve when there is an additional data stream that my data depends on. When I direct ...

Freeze the headers of multiple tabulators on a single page without restricting the height

My website contains more than 3 tabs and several charts interspersed between tables. Based on the requirement, we have not set a fixed size for the tables. Therefore, the height of the table varies depending on the data. I am looking to fix the headers o ...

Trouble locating DOM element in Angular's ngAfterViewInit()

Currently, I am attempting to target a specific menu item element within my navigation that has an active class applied to it. This is in order to implement some customized animations. export class NavComponent implements AfterViewInit { @ViewChild(&a ...

Transferring image data from Angular to ExpressJS using blobs

While attempting to send a blob image, I encountered an Error: Unexpected end of form when using multer with Serverless Framework. Upon checking console.log My understanding is that I need to append it to FormData before sending it in the body, but I hav ...

Angular 4/5 | Custom Dropdown Component

I have been working on a custom dropdown directive in Angular that I can attach to any DOM element. Below is the code for my directive: import { Directive, HostListener } from '@angular/core'; @Directive({ selector: '[appDropdown]' ...

Creating a Fixed Footer with CSS

Another question on the same topic, with a twist. Despite trying various solutions found in countless articles, none seem to work for me. My familiarity with HTML/CSS is limited due to just a few months of off-and-on practice. Hence, I'm seeking help ...

React's Material-UI AppBar is failing to register click events

I'm experimenting with React, incorporating the AppBar and Drawer components from v0 Material-UI as functional components. I've defined the handleDrawerClick function within the class and passed it as a prop to be used as a click event in the fun ...

Utilizing Google Sheets as a secure, read-only database for Angular applications without the need to make the sheet accessible to the

Seeking a way to utilize Google Sheets document as a read-only database for my Angular application, I have attempted various methods. However, the challenge with all these approaches is that they necessitate public sharing of the Sheet (accessible to anyon ...

Can the individual headers of an ag-grid column be accessed in order to apply an on-mouseover event with a specific method?

In my current project, I am utilizing ag-grid to create a dynamic web-application that combines tools from various Excel files into one cohesive platform. One of the Excel features I am currently working on involves displaying a description when hovering ...

Importing Angular Material modules

I've integrated the Angular Material module into my project by updating the material.module.ts file with the following imports: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MatT ...

Issue with NGXS Selector Observable not reflecting updated state

My issue is that when I update the state, my selector does not pull the new values. I have defined the selector in my state and I can see the state values getting updated. However, the selector in my component is not fetching the latest values. Even though ...

Tips for determining the minimum value within an array of objects across multiple keys using a single function

I am currently tasked with the challenge of determining the minimum value from an array of objects that contain multiple keys. My ultimate goal is to identify the minimum value among all keys or specific keys within the objects. For instance var users = ...

Adding a scroll bar to a fixed container: tips and tricks

I am looking to implement a scrollbar in a fixed container for the cart products. The goal is to enable easy navigation through a large number of products added to the cart by scrolling within the container. Below is the HTML code snippet representing the ...

What is the best way to prevent the child elements from inheriting the hover effect of the main container?

Looking to spice up the appearance of the div element that holds the product's image, SKU, and price. Adding a hover effect to the main container causes the child divs to inherit it, resulting in separate hover effects on each child div. Sample HTML ...