Layout with FlexBox in React Native
Flex
Flex will define how your items are going to “fill” over the available space along your main axis. Space will be divided according to each element's flex property.
In the following example, the red, gold, and steel-blue views are all children in the container view that has flex: 1
set. The red view uses flex: 1
, the gold view uses flex: 2
and the steel blue view uses flex: 3
. 1+2+3 = 6 which means that the red view will get 1/6
of the space, the gold 2/6
of the space and the steel blue 3/6
of the space.
Flex direction
Flex direction controls the direction in which the children of a node are laid out. This is also referred to as the main axis. The cross axis is the axis perpendicular to the main axis or the axis on which the wrapping lines are laid out.
row
Align children from left to right. If wrapping is enabled then the next line will start under the first item on the left of the container.column
(default value) Align children from top to bottom. If wrapping is enabled then the next line will start to the left first item on the top of the container.row-reverse
Align children from right to left. If wrapping is enabled then the next line will start under the first item on the right of the container.column-reverse
Align children from bottom to top. If wrapping is enabled then the next line will start to the left first item on the bottom of the container.
Layout Direction
Layout direction specifies the direction in which children and text in a hierarchy should be laid out. Layout direction also affects what edge start
and end
refer to. By default React Native lays out with LTR layout direction. In this mode start
refers to left and end
refers to right.
LTR
(default value) Text and children are laid out from left to right. Margin and padding applied to the start of an element are applied on the left side.RTL
Text and children are laid out from right to left. Margin and padding applied to the start of an element are applied on the right side.
Justify content
Justify content describes how to align children within the main axis of their container. For example, you can use this property to center a child horizontally within a container with flexDirection
set to row
or vertically within a container with flexDirection
set to column
.
flex-start
(default value) Align children of a container to the start of the container's main axis.flex-end
Align children of a container to the end of the container's main axis.center
Align children of a container in the center of the container's main axis.space-between
Evenly space of children across the container's main axis, distributing remaining space between the children.space-around
Evenly space of children across the container's main axis, distributing remaining space around the children. Compared tospace-between
usingspace-around
will result in space being distributed to the beginning of the first child and end of the last child.space-evenly
Evenly distributed within the alignment container along the main axis. The spacing between each pair of adjacent items, the main-start edge and the first item, and the main-end edge and the last item, are all exactly the same.
Align items
Align items describe how to align children along the cross axis of their container. Align items is very similar to justifyContent
but instead of applying to the main axis, alignItems
applies to the cross axis.
stretch
(default value) Stretch children of a container to match theheight
of the container's cross axis.flex-start
Align children of a container to the start of the container's cross axis.flex-end
Align children of a container to the end of the container's cross axis.center
Align children of a container in the center of the container's cross axis.baseline
Align children of a container along a common baseline. Individual children can be set to be the reference baseline for their parents.
Note: The stretch does not work if the children have a fixed dimension along the secondary axis. In the above example, the alignItems: stretch will not work until we remove the width of the gold container.
Align self
alignSelf
has the same options and effect as alignItems
but instead of affecting the children within a container, you can apply this property to a single child to change its alignment within its parent. alignSelf
overrides any option set by the parent with alignItems
.
since alignSelf
is like alignItems
for the stretch to work u will need to remove the fixed-width that the gold container has.
Q&A
If something doesn’t work as expected or needs more details, just drop a comment below :) Happy coding!
This has been developed by myself at eDonec.