Install npm package
npm i tiny-flex
Oryarn add tiny-flex
Import the components:
// Import all the components
import "tiny-flex";
// Import the container component only
import "tiny-flex/components/container";
The code shipped in the package is ES2015. If you are targeting unsupported browsers, make sure you transpile it in your project.
Include from CDN
<!-- Import all components -->
<script
type="module"
src="//unpkg.com/tiny-flex@insert-version-here/components/index.js"
></script>
<!-- Import the container component only -->
<script
type="module"
src="//unpkg.com/tiny-flex@insert-version-here/components/container.js"
></script>
Use
<flex-container>
<flex-item>Hello</flex-item>
<flex-item>world</flex-item>
</flex-container>
flex-container accepts the following optional attributes:
- direction
- align-items
- justify-content
- align-content
- wrap
- flow
- gap
- row-gap
- column-gap
flex-item accepts the following optional attributes:
- flex
- grow
- shrink
- basis
- order
- align-self
Demo
Defaults
View code
<flex-container>
<flex-item>tiny</flex-item>
<flex-item>flex</flex-item>
<flex-item>web</flex-item>
<flex-item>components</flex-item>
</flex-container>
Column
View code
<flex-container direction="column">
<flex-item>tiny</flex-item>
<flex-item>flex</flex-item>
<flex-item>web</flex-item>
<flex-item>components</flex-item>
</flex-container>
Reverse column
View code
<flex-container direction="column-reverse">
<flex-item>tiny</flex-item>
<flex-item>flex</flex-item>
<flex-item>web</flex-item>
<flex-item>components</flex-item>
</flex-container>
Content justified to flex-end
View code
<flex-container justify-content="flex-end">
<flex-item>tiny</flex-item>
<flex-item>flex</flex-item>
<flex-item>web</flex-item>
<flex-item>components</flex-item>
</flex-container>
Items aligned to the center
View code
<flex-container direction="column" align-items="center">
<flex-item>tiny</flex-item>
<flex-item>flex</flex-item>
<flex-item>web</flex-item>
<flex-item>components</flex-item>
</flex-container>
Item with flex 1
View code
<flex-container direction="column">
<flex-item>tiny</flex-item>
<flex-item flex="1">flex</flex-item>
<flex-item>web</flex-item>
<flex-item>components</flex-item>
</flex-container>
Item self aligned to flex-end
View code
<flex-container direction="column" align-items="center">
<flex-item>tiny</flex-item>
<flex-item align-self="flex-end">flex</flex-item>
<flex-item>web</flex-item>
<flex-item>components</flex-item>
</flex-container>
Items with different orders
View code
<flex-container>
<flex-item order="2">tiny</flex-item>
<flex-item order="1">flex</flex-item>
<flex-item order="4">web</flex-item>
<flex-item order="3">components</flex-item>
</flex-container>
Learn more
- Flexbox Froggy is a fun game to learn Flexbox.
- Try reaflex, a flexbox library for React.
See other projects