Like latte art, UI can be a tricky thing to handle. One wrong move with the mug and the whole meaning is changed. But consistency through the process brings to light the design of the artist.
Flexbox provides a clean, consistent way to build out a responsive layout. Its popularity and cross-browser support make it a great resource to implement into your current or future projects.
The Angular Flex Layout package is a great way to apply this functionality to your templates via directives, while also providing the flexibility of responsive breakpoints. Although a great resource, the number of directives required on your template can make things a little cluttered. If you are not inclined to this approach there are great alternatives, like Twitter Bootstrap, if you prefer to just add classes to your elements.
First, the barista must learn to pour
Alrighty; lets start things off with a fresh install.
npm i @angular/flex-layout@9.0.0-beta.31 @angular/cdk
Note: At the writing of this article the latest version of Angular Flex Layout 10.0.0-beta.32 doesn’t support Ivy.
Now add a little flavoring to the app.module.ts so your application knows what's going on with the FlexLayoutModule import.
I built out a quick application to showcase Angular Flex Layout. We're going to start off with some simple divs and zero layout.
If you would like to attempt to recreate what I did above check out this Stackblitz I created here.
First, let's begin by moving all the cards into a column inside a parent div with the fxLayout directive like this: fxLayout="column". Then, add some spacing using flexLayoutGap with a value of 50px. Since this is being used on a column the directive sets vertical spacing of 50px between our rows. Under the hood, this adds margin-right: 25px to every child in the container except for the last one. Now, let's organize the cards within this top-level container into three different rows.
Take the first card and put it into its own div and apply fxLayout="row". Then stretch the number one card out full width to represent our banner. On the parent div of the first card put the directive fxFill. This will add flex: 1 1 0%; to the element stretching it out to take up the full width of the screen.
Now create a second row to place cards two, three, and four in. On the main parent div for these four cards, we'll put the fxLayoutGap directive with a value of 25px. This will maintain a space between the cards horizontally no matter the size of the screen. Then upon each of the cards parent div apply the fxFlex directive like we did on the first row; this will cause each card to take up as much width as possible.
In the last row not much will be different and we'll just use a larger gap of 50px.
And lastly, to add a finishing touch, on every parent div of the cards put fxLayoutAlign="center". Using this directive will center the number on each card using justify-content: center.
We've introduced a few different directives from Angular Flex Layout since saying hello to the fxLayout. Here's a quick break down of those we used in the example:
Great work! Though a simple exercise, we were able to introduce you to many of the basic and widely used Angular Flex Layout directives. You can find more from the docs here.
We've built some columns and rows with spacing and other flexing, now let's move into some content positioning. Positioning can be one of the toughest things to deal with in UI design, barring using margin-right: 500px; of course 😉, but if you're like me you want a smooth clean approach that won't clutter up the app.
Let's look at an example:
This is Mr. Briebug:
Because Mr. Briebug is the local expert on all things coffee he requires his own #coffeeGuru container. Let's do some flexing and get him in the meeting!
Let's initiate the flex layout and get things organized in a row.
This on the #coffeeGuru container is the same as:
There! Now he's in his spot.
Hopefully now we'll get some good coffee around here. Finally.
So we'll add:
We can also show or hide content based on a breakpoint:
Now, whatever is in this container when the screen is LESS then 599px, the content will hide.
To take a closer look and do some hacking yourself on this example, checkout the Stackblitz here.
For just about every Flex Layout directive there is the option of breakpoints. For your convenience, checkout the below table for a list of the available aliases. For more information on the Responsive API from Angular Flex Layout and more cool tables like this, checkout their wiki on Github here.
Breakpoint
Media Query
xs
'screen and (max-width: 599px)'
sm
'screen and (min-width: 600px) and (max-width: 959px)'
md
'screen and (min-width: 960px) and (max-width: 1279px)'
lg
'screen and (min-width: 600px) and (max-width: 959px)'
xl
'screen and (min-width: 960px) and (max-width: 1279px)'
It-sm
'screen and (max-width: 599px)'
It-md
'screen and (max-width: 959px)'
It-lg
'screen and (max-width: 1279px)'
It-xl
'screen and (max-width: 1919px)'
gt-xs
'screen and (min-width: 600px)'
gt-sm
'screen and (min-width: 960px)'
gt-md
'screen and (min-width: 1280px)'
gt-lg
'screen and (min-width: 1920px)'
The Angular Flex Layout package, as we have seen, has some nice features. One of the best being the Responsive Breakpoints API: The ease of adding breakpoints for different screen sizes makes for a simplistic approach and less media queries in your CSS files.
With that, I hope you have found this article resourceful and can put what you have learned to good use! If you have more questions on this package there is much, much more information on the Angular Flex Layout wiki here.
Here's to next time and another cup of coffee. ☕️