Aligning Children using Flexbox in React Native

September 22, 2015

This tutorial is part of a series, in which we are learning all about the layout system in React Native. I recommend that you read the previous tutorial about how flexDirection works as we will continue using the same project that we created in the first tutorial.

Now that we have a better understanding of flex direction, let’s review the alignment options that we have available. We will create a container that displays a message with a title, we will learn how to align this component.

First we need to create the required views and texts, open the index.ios.js tab in your favorite text editor and add the following code to the render method, as a child of the content view.

'use strict';
var React = require('react-native');
var {
} = React;
class ReactLayouts extends Component{
    render() {
        return (
            <View style={styles.mainContainer}>
               <View style={styles.toolbar}>
                    <Text style={styles.toolbarButton}>Add</Text>
                    <Text style={styles.toolbarTitle}>This is the title</Text>
                    <Text style={styles.toolbarButton}>Like</Text>
                <View style={styles.content}>
                    {/* START NEW CODE */}
                    <View style={styles.messageBox}>
                            <Text style={styles.messageBoxTitleText}>A simple mesage</Text>
                            <Text style={styles.messageBoxBodyText}>This is just a dummy sample it will help us to see the alignment in action.</Text>

              {/* END NEW CODE */}

In the previous code we’ve defined a message box container. It’s important to notice that we are adding just the code between the comments, the toolbar code was defined before in the flexDirection tutorial. We only have the title, and the content is completely static.


Once we have the component rendered, we need to add some styles to the stylesheet object.

var styles = StyleSheet.create({

   // …


These is a pretty common styles, just some colors, paddings, fonts and so on. As a result we should have something as in the following image.


Now that we have a component, we can start playing around with the alignment. Aligning components in React is very straightforward, all we need to do is define the alignItems property in the container’s styles.

var styles = StyleSheet.create({
        alignItems:'center'     //<-----

This will automatically center the component on the screen. Because we didn’t define a flex direction, the column direction is used. Therefore the component is horizontally centered.

On the other hand, if we set the flex direction to row, the component will be vertically centered. This is a very important concept to keep in mind.


We have three more options to align our component.

1.flex-start which will align the component at the top/start of the parent component.

2.flex-end which will align the component to the bottom/end of the parent container.

3.stretch will set the height or width to 100% of the container, based on the flex direction.

We can also justify our components.  For example if we want to center our components horizontally and vertically, we will need to apply the following changes to our styles.

var styles = StyleSheet.create({

First we set the flex direction to row, this will arrange the children horizontally. In order to center the component horizontally we use the alignItems property, then we use justifyContent to vertically center the component.


We have a few more options to justify our component to the left, right, as well as to add space between or around the children.


The layout system in React Native is very powerful and flexible, we can create any layout with all the available options that we have. Understanding how flexbox works is important in order to build our very custom layouts and components.

You can download the code from Github. I recommend you to take a look at the documentation and try all the possible values in the available properties.

Crysfel Villa is a Sr. Engineer at Modus Create. He's a passionate JavaScript coder and an accomplished software developer with over 8 years of experience on technical training, consulting and systems analysis. When he's away from the keyboard, he enjoys playing the guitar, piano and violin. Crysfel currently lives in NY and can be found attending tech meetups throughout the city. He has a React Native book due for release in December 2016.

  • joshschumacher

    The images on your article aren’t loading. When I look at one of the images independently, it looks like they’re being hosted by google with domains and there’s an error, “ACL Denied That’s all we know.”

    Sample URL:

    • Erika G

      Thanks for the heads up, Josh! The post has been updated accordingly.

  • Draško Kokić

    I wonder if the paragraph “First we set the flex direction to *row*, this will arrange the children horizontally. In order to center the component horizontally we use the *alignItems* property, then we use *justifyContent* to vertically center the component.” is correct. Either it is applying for flex direction *column*, or you would need to exchange *alignItems* and *justifyContent* in your paragraph. Or most probably, I am wrong as it is after 1am here 😉

What We Do

We’ll work closely with your team to instill Lean practices for ideation, strategy, design and delivery — practices that are adaptable to every part of your business.

See what Modus can do for you.


We're Hiring!

Join our awesome team of dedicated engineers.