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

Is it possible to set up Injector within TestBed?

Currently, I am in the process of writing test cases for a custom ErrorHandler within Angular. In my implementation, I have included Injector as a dependency in the constructor due to the understanding that providers are initialized after ErrorHandler. The ...

Implementing CSS to achieve striped row styling in a paragraph (not within a table)

Is there a way to achieve alternating row shading within a wrapped paragraph element without using the :nth-child technique? For instance, if we have: <p style="width:100px;"> row 0 -- hello row 1 -- world row 2 -- !!! </p> Some brow ...

Angular form requests can utilize the Spring Security Jwt Token to allow all options methods

Despite checking numerous sources online, I am facing a persistent issue with my Angular application. The problem arises when using HttpClient along with an Angular interceptor to set headers for authentication in my Java Rest API using JWT tokens. The int ...

Issue with Angular 5 - Deselect all checkboxes not reflecting in the UI

I am currently working on integrating a reset button into a Reactive form in Angular 5. The reset functionality works flawlessly for all form fields, except for the dynamically created multiple checkboxes. Although it seems like the reset operation is hap ...

How can I delete the black background from the ion-tab-bar in Ionic 7?

In my current project using Ionic 7, I have a navigation guide set up with 4 tabs. I am trying to customize the styling of these ion tabs by adding some custom CSS. The issue I'm facing is that despite my attempts to make the background transparent, ...

The CSS Bootstrap 4 class 'input-group-append' is not functioning properly. Just recently, the styles were displaying correctly

My web application using AngularJS 1.7.8, jQuery 3.3.1, Bootstrap 4.3.1, and various other CSS and JS libraries worked seamlessly last week. However, today I noticed an issue with the button visualization. To Replicate: Visit openskymap.org to see my d ...

Issue with migrating from Angular version 2.4.10 to 4.0.0

After attempting to update my application from Angular 2.4.10 to 4.0.0, I used the following command: "npm install @angular/common@next @angular/compiler@next @angular/compiler-cli@next @angular/core@next @angular/forms@next @angular/http@next @angular/pl ...

Safari IOS experiencing issue with element disappearing unexpectedly when input is focused

I am facing a situation similar to the one discussed in the question (iOS 8.3 fixed HTML element disappears on input focus), but my problem differs slightly. My chatbox iframe is embedded within a scrollable parent, and when the iframe is activated, it exp ...

Troubleshooting Issue with Angular Property Binding for Image Dimensions

Currently, I am exploring property binding in Angular through an example. The idea is to use an image and bind its width, height, and src attributes using property binding. Surprisingly, even though there are no errors and the image renders successfully vi ...

Troubleshooting a unique Flex layout problem in Angular 2 with Angular Material

Can anyone help me figure out how to recreate this design using Flex within my Angular2 materialDesign stack? https://i.stack.imgur.com/le1ld.png Currently, I'm facing an issue where blocks 2 and 3 keep ending up below block 4... https://i.stack.im ...

Is it deemed as an anti-pattern to establish directives for styling?

Currently, I am in the midst of developing a specialized component UI library for a specific project I'm involved in. This library will consist of unique stylized components with elements that are reused throughout. As I work on this project, I find ...

Leveraging Angular to incorporate HTML templates from one component into another component

I am currently working with two components. The first one is a table component, identified by the selector 'table-component', which has standard filtering capabilities. The second component is designed for displaying the table on a page. This me ...

A step-by-step guide on how to implement a window scroll-controlled color transition

I've implemented jQuery code to change the opacity of my navbar's background as the user scrolls, transitioning from transparent to blue. Here's the snippet: $(window).scroll(function(){ var range = $(this).scrollTop(); var limit = 45 ...

How to Adjust the Padding of Tree Row in GWT?

Have you ever seen a GWT tree before? It sort of resembles the following structure: <div class="gwt-Tree"> <div style="padding-top: 3px; padding-right: 3px; padding-bottom: 3px; margin-left: 0px; padding-left: ...

Is it possible to set spacing between the rows of an HTML table without also setting spacing for the columns?

Is there a way to add spacing between the rows of a table without affecting the columns? ...

Choose the content in the second column

Can anyone help me with CSS selectors? I've been trying to target the second .col_content element in my code to apply a specific background image, but I haven't had any luck so far. Adding a second class would be an easy fix, but I'm curious ...

Guide on toggling the button by clicking on the icon to turn it on or off

Within my angular application, I have implemented font icons alongside a toggle switch. Initially, the switch is in the ON state. The specific functionality desired is for clicking on an icon to change its color from white to red (this has been achieved) ...

Cannot utilize Map within each loop

Below is the less code snippet: @threshold:{ a:50; b:200; }; @themes:{ a:red; b:blue; }; .mymixin(@name,@color,@thrshld){ //do-something } each(@themes,{ .mymixin(@key,@value,@threshold[@key]); }); Upon executing the code, an error message ...

Encountering difficulty in presenting detailed information using a .json file

My photo gallery app functions perfectly when fetching data from jsonplaceholder.typicode.com. However, when I use my own local data, it breaks whenever I try to click on a photo to view its details. How is it possible for the photos to display correctly b ...

Angular 8 encountering issues with incomplete/impartial JSON parsing

Upon receiving a JSON object through a Socketio emit, the data appears as follows: { "single":[ { "name":"Xavi555", "data":[ { "_id":"5e2ea609f8c83e5435ebb6e5", "id":"test1", "author":"someone", ...