By Katie Kang
To be most efficient in mobile design, you must familiarize yourself with Flex. It will save you time, and more importantly headaches. Flex is useful if you want to create a responsive UI for all devices. I highly recommend using Flex instead of setting specific values between elements such as paddingRight: 50 to give a space, as it won’t create a responsive UI, since there are so many devices out there with all different screen sizes. I will be going through a few key examples of using Flex and explaining how these are beneficial in mobile design. These are all basic designs that will set you up for success within Flex!
Getting Started
We will be using
Example #1: Using SafeArea for 100% Screen Coverage
How to create two
Example #2: Creating Horizontal Sections
Flex will automatically break down the entire space in
Diving Deeper
Also, by default, the direction of flex in React Native flex is ‘column’, not ‘row’. It is opposite to what you see in React CSS. What do I mean by that? Here is an example.
Example #3: Understanding ‘column’ and ‘row’.
We’ve applied flex: 1 and instead of using the default ‘column’ direction, we are using ‘row’ direction for
Adding Adjustments
Okay, so you understand that the above example shows the entire screen as one row, and since it’s ‘row’ direction, each component within
So, where are the rest of the texts? It overflows to the outside of the screen to the right. Think of each
Let’s Kick it Up a Notch
If you understand flex and flexDirection, the rest is easy. See more example code below and try to see if you can do similar or even more complex things. I highly recommend using backgroundColor everywhere, because you can visualize the area that’s being affected.
Example #4: Adding User Profile Information
Let’s look at a more real-world example. What if you want to create a header and content with a user’s avatar on the left and their information on the right? Here is one way of doing that.
The pink and sky-blue areas below are nothing different than the example above with lots of text. The entire pink and sky blue area is one giant row, and we are simply stacking up elements from left to right. And the email and address area is just taking up the remaining space by using flex: 1.
Okay…. So what do you do if you want to vertically center ‘Avatar’ text? You can use justifyContent: ‘center’ to do that. If you want to replace Avatar text with an image, the same principle applies; you can still use the same style.
To align items horizontally centered, use alignItems: ‘center’. Think of justifyContent as a y-axis and you move items top, center, or bottom aligned using that. The same goes for alignItems; it’s like the x-axis, moving items left, center, or right. Value for the left, or right alignment is not ‘left’ or ‘right’, it’s ‘flex-start’, and ‘flex-end’. So you would do justifyContent: ‘flex-start’ or alignItems: ‘flex-start’. I know, it’s confusing at first, but just play around with it for a bit.
So far, we’ve covered the most frequently used features of flex. If you got all of these, you won’t have much trouble when building a UI. Just think carefully about how you are going to write the code when you have a complex design. Start from putting everything in the imaginary boxes. I use backgroundColor very often so that I can visualize items and it helps me to correct any mistakes in the early development stage. It gets pretty messy when the design has so many items in multiple layers of
I hope this overview helps you on your Flex journey! Remember to start small and remember the basics.
