This is a lightweight SCSS flexbox library. It is inspired by Angular's Flex-Layout and can replace the most popular functions of the (by now deprecated) Angular library.
This library gives you the option between using CSS classes or data-attributes. Both options give you the same features but the CSS classes can be more verbose when you want a more complex layout. Check out the Wiki for a detailed documentation.
Using the CSS classes is more verbose but the recommended way to use this library:
.fx-layout-
and.fx-align-
: CSS classes to control the flexbox container.fx-gap--
: CSS classes to add gaps between the elements in a flex container.show-
and.hide-
: CSS classes which can be used to show/hide elements depending on the screen size.fx-flex
and.fx-glex-grow
: Classes to control flex behaviour of an element
Using the data attributes is the easiest way to use this library and easier to migrate to from Angular Flex-Layout:
data-layout
anddata-layout-align
: HTML attributes to be used to control the flexbox containerdata-layout-gap
: attribute to add gaps between the elements in a flex containerdata-hide-
anddata-show-
: attributes which can be used to show/hide elements depending on the screen sizedata-fx-flex
anddata-fx-flex-grow
: Control flex behaviour of an element
Add the library to your project's package.json
:
npm i -s css-fx-layout
Then use the provided mixins in your main stylesheet to include the css-fx-layout selectors. You can choose between using CSS classes or HTML data attributes (or, while not recommended, use both). If you want to use the layout-gap functionality you can customise which selectors are going to be generated by providing the start, end and unit as shown in the example below.
CSS Classes | HTML Data Attributes |
---|---|
@use "css-fx-layout" as fx;
// If you choose to use CSS class selectors:
@include fx.class-selectors;
@include fx.gap-class-selectors(1, 16, px em); |
@use "css-fx-layout" as fx;
// If you choose to use attribute selectors:
@include fx.attribute-selectors;
@include fx.gap-attribute-selectors(1, 16, px em); |
This is the simplest example. It will make the div a flex container and align the three spans in a row:
CSS Classes | Data Attributes |
---|---|
<div class="fx-layout-row">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div> |
<div data-layout="row">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div> |
The resulting layout:
An advanced example that aligns the items in reverse order with a gap of four pixels and vertically centered:
CSS Classes | Data Attributes |
---|---|
<div class="fx-layout-row
fx-layout-reverse
fx-align--start-x
fx-align--x-center
fx-gap--4px">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div> |
<div data-layout="row reverse"
data-layout-align="start center"
data-layout-gap="4px">
<span>One</span>
<span>Two</span>
<span>Three</span>
</div> |
The resulting layout:
Vertically and horizontally center an element in its parent.
CSS Classes | Data Attributes |
---|---|
<div class="fx-layout-row
fx-align--center-x
fx-align--x-center">
<span>One</span>
</div> |
<div data-layout="row"
data-layout-align="center center">
<span>One</span>
</div> |
The resulting layout:
This will hide the first span on phones and show the second span only on desktop sizes. The third span is always visible.
CSS Classes | Data Attributes |
---|---|
<div>
<span class="hide-phone">One</span>
<span class="show-desktop">Two</span>
<span>Three</span>
</div> |
<div>
<span data-hide-phone>One</span>
<span data-show-desktop>Two</span>
<span>Three</span>
</div> |
There are no pre-compiled CSS files available for css-fx-layout 2 and above. Version 1 releases provided minified pre-compiled CSS files. If you are interested in them you can find and download them from the releases page.
Initially I created this library because I liked the convenient syntax of Angular Flex-Layout and wanted to use it in non-Angular projects and without JavaScript.
By now Angular Flex-Layout has been deprecated and this library can be a replacement for most of the popular parts.