Alignment with Angular 2 Material and Flex-Layout

I've been experimenting with angular 2 material and flex-layout in an attempt to create a responsive gallery of elements. Despite spending countless hours on it, I'm still unable to center my elements as desired: https://i.sstatic.net/Hsicy.png

Here's the source code snippet:

<div fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
  <md-card fxFlex.gt-md="20" fxFlex.md="28"  fxFlex.sm="40" fxFlex.xs="100" *ngFor="let recipe of recipes" [routerLink]="['/details', recipe.id]" class="recipe-card">
    <md-card-header>
      <md-card-title>element</md-card-title>
    </md-card-header>
    <img md-card-image src="http://placehold.it/350x200">
    <md-card-content>
      <p>
        Lorem Ipsum
      </p>
    </md-card-content>
  </md-card>
</div>

I have experimented with different values for fxFlexAlign (https://github.com/angular/flex-layout/wiki/API-Documentation) but none seem to achieve the desired result of centering the elements or evenly distributing the space between them.

Is there a solution to this issue?

EDIT

Unfortunately, using justify-content: space-between; does not work well when dealing with a dynamic number of items that wrap onto new lines, resulting in uneven alignment in the last row. Here's an example of what I mean:

.container {
  display:flex;
  width:100%;
  flex-flow:row wrap;
  justify-content: space-between;

  
}
.block {
  width:100px;
  height:100px;
  border:1px solid red;
}
<div class="container">
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
  <div class="block" fxLayout='row' fxLayoutWrap fxLayoutGap="2rem">
   ... your content
  </div>
</div>

Answer №1

<div fxLayout="row" fxLayoutWrap fxLayoutGap="2rem" fxLayoutAlign="center">

After implementing fxLayoutAlign="center", I noticed the improvement in alignment as the results are now centered.

Answer №2

If you want to achieve a similar functionality, consider trying this concept. It may be necessary to adjust the CSS % values for more accurate results.

.sp{
display: flex;
justify-content: center;
}

.i{
width: 23%;
    height: 133px;
    background-color: grey;
    margin: 3px;
    color: #fff;

}

.p{
  display: flex;
    flex-wrap: wrap;
    width: 56%;
}
<div class="sp">
  <div class="p">
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
        <div class="i"> content </div>
</div>
</div>

Answer №3

Sorry for the delay in responding, but this approach works well with dynamic items, although I don't have the exact formula handy.

Here is my simplified formula:

margin-bottom = (100 - (total items in row * fxFlex on item)) / total space between items in row

For example,

margin-bottom = (100 - (5 * 19) ) / 4
margin-bottom = (100 - 95) / 4
margin-bottom = 5 / 4 = 1.25%

In Your Code HTML

<div fxLayout="row" fxLayoutWrap="wrap" fxLayoutAlign="space-between">
    <!-- margin-bottom = gt-md = 1.25%, md = 6.5% , sm  = 22% !-->
    <md-card fxFlex.gt-md="19" fxFlex.md="29"  fxFlex.sm="39" fxFlex.xs="100" *ngFor="let recipe of recipes" [routerLink]="['/details', recipe.id]" class="recipe-card">
        <md-card-header>
        <md-card-title>element</md-card-title>
        </md-card-header>
        <img md-card-image src="http://placehold.it/350x200">
        <md-card-content>
        <p>
            Lorem Ipsum
        </p>
        </md-card-content>
    </md-card>
</div>

CSS

md-card {
  margin-bottom: 6.25%; // md flex 29

  // The responsive margin calculations are commented out, implement as needed
  // margin-bottom: 1.25%; // gt-md flex 19
  // margin-bottom: 22%; // sm flex 39
  // margin-bttom : 2%; // sm flex 49, consider using this one
}

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

Serving static files in Django

Currently diving into Django and encountering a hurdle with static files. I've followed the setup in both the settings and HTML, but unfortunately, they are not loading on the website. Seeking assistance to resolve this issue! **settings.py:** STATIC ...

Using the GetElementByID method to target a textarea element

I attempted to submit form data through TinyMCE, but the script is returning the old value instead of the new one. Here's the form code: <form> <textarea id="tml" name="template_content" class="editor_large">{$template_content|escape ...

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 ...

Is it possible to replace checkboxes with dropdowns in the NG-ZORRO Tree component?

I am attempting to create a tree structure using the tree component from ng-zorro. However, instead of checkboxes for the leaf nodes, I would like to have dropdown menus. I tried using the ng-template but the checkbox is still appearing. Here is my code: ...

Is there a way to verify if an ID includes more than one word?

I am trying to target a specific div with a unique id in jQuery: <div id="picture_contents_12356_title"></div> The '12356' is autogenerated and must be included in the id. I need to create a jQuery selector that combines "picture_co ...

Adding interpolation to conditionals in Angular

I want to dynamically add the value of "artist.artistName" to the end of a URL link For example, if artistName is 'Muse', the URL should look like However, I am having trouble correctly using append and interpolation. <div *ngFor="let artis ...

Tips for customizing the clicked-button color of the navigation bar's drop down lists

Excuse me, I have a couple of queries: 1) Currently, I am working on designing a sample website. I am facing an issue with the Dropdown list button (Our Softwares). When I click on it, everything works fine except for its Color. Instead of changing to gre ...

Is using transform scale in CSS to scale SVG sprites a valid method of scaling?

I have been using an SVG sprite as a CSS background image in this manner: .icon-arrow-down-dims { background: url("../sprite/css-svg-sprite.svg") no-repeat; background-position: 16.666666666666668% 0; width: 32px; height: 32px; display: inline-b ...

Different and Basic Designs for mat-table

Is there a way to customize the appearance of the material data table (mat-table) beyond the pre-built themes? I'm specifically interested in styles like table-striped, table-sm, or table-bordered from bootstrap 4. Is it possible to apply these style ...

Utilizing Semantic UI Grid to distribute columns and fill in blank spaces

I'm a beginner in semantic UI and I'm struggling to understand how to adjust the columns to fill in the blank space correctly. Can someone please explain how to do this? By the way, I am using the `<div class="ui grid"> <div class="fou ...

Unexpected arrows are being added to the dropdown menus in a responsive Twitter Bootstrap menu

I am puzzled by the behavior of my responsive twitter bootstrap nav. Take a look at this screenshot: The issue is with the strange up-pointing arrows that appear. The carets are fine, but these extra arrows are not desired. They disappear if I remove all ...

Is there a way to extend this section to fill the entire screen height without any white space at the bottom?

We've been working on extending the content to the full height of the screen, but no matter what we try, there's always some white space lingering at the bottom. Any suggestions on how to fix this issue? section { height: 100vh; background ...

How come this particular design is being passed down from a predecessor (and not a direct parent) div?

Web development can be frustrating at times. Hopefully, someone out there can shed some light on this issue and offer a solution. You can view the HTML/CSS code below or check out this example on JSFiddle The problem arises when I have a header div and a ...

The menu will expand to full width, with each list item evenly sharing the width to achieve full coverage

I am currently facing an issue with my horizontal menu on my website. I want the menu to stretch across the full width of the site, but I can't seem to get it right. Here is the code for my menu: <nav> <ul id="menu" class="menu"> &l ...

Struggling with adjusting the image dimensions on my HTML and CSS page

<div class="profile"> <img src="image/JaeHeadShot.jpg" alt="Head Shot"/> <h1> Jae Hong </h1> </div> .profile{ border: 2px solid rgba(0,0,0,0.3); text-align:center; padding: 30px; } img.profile{ width:423; height ...

Hyperledger Indy - TheOrgBook Module Experiencing Build Failure Caused by Node and NPM Hiccup

After successfully deploying the VON Network and Permitify Modules of Hyperledger Indy SSI VC Demo on MacOS, I encountered issues with TheOrgBook setup. Despite attempting the suggested hot fix from GitHubIssue #26, TheOrgBook Demo continues to fail. ER ...

An error has occurred in Angular5 and Three.js: 'Scene1' property is undefined

Running into an issue with my angular5 website integrated with Three.js. I keep getting an error message that says: Cannot read property 'Scene1' of undefined when trying to add an object to the Scene. Any suggestions on how to properly define th ...

Extracting arrays from JSON in Angular: A step-by-step guide

I am currently working with Angular2 (version 5) and facing an issue. I am able to make an HTTP request and receive JSON data. While I know how to access and use individual values, I am struggling with accessing and extracting the two arrays inside an elem ...

word-wrap: break-word; repair or substitute

Is there a way to prevent line breaks before words that are going to break anyway when using word-wrap: break-word;? It seems unnecessary and unattractive. Any suggestions? For Example: Consider a div with word-wrap: break-word;, set at a width equal to ...

If the div is devoid of content, then remove the class

<div class="slider">content goes here</div> <div id="slider-2" class="active inactive">also some content here</div> <script type="text/javascript"> function checkEmpty( element ){ return !$.trim(element.html()) ...