Skip to content
tezvyn:
Menu

Flexbox: Responsive Layouts in React Native

Source: reactnative.devEasyHow cards are made

Flexbox arranges components along a primary axis, creating responsive UIs without manual calculations. It's used for nearly all layout needs, from centering items to building complex grids.

Why it exists

Mobile apps must look good on countless screen sizes. Hardcoding pixel values for position and size is brittle and leads to broken layouts. Flexbox was adopted to provide a consistent and flexible algorithm for arranging elements that automatically adapts to the available space.

The mental model

Imagine your UI components are blocks inside a container. Flexbox gives you two main levers: a primary axis (the flexDirection) and a cross axis. You then use properties like justifyContent to distribute the blocks along the primary axis and alignItems to align them along the cross axis. The flex property on a child determines how much it grows relative to its siblings to fill the available space.

How it works

You apply Flexbox properties to a container component's style to control its direct children. The three most common properties are flexDirection, justifyContent, and alignItems. flexDirection sets the main axis, either 'column' (top-to-bottom, the default) or 'row' (left-to-right). justifyContent aligns children along that main axis (e.g., 'center', 'space-between'). alignItems aligns children on the cross axis (e.g., 'center', 'stretch'). The flex property, applied to a child, makes it expand to fill available space. A child with flex: 2 will take twice as much space as a sibling with flex: 1.

When to use it

Use Flexbox for virtually all layout tasks in React Native. It's the standard for structuring screens, positioning buttons, creating lists, and building any UI that needs to be responsive. It is the primary tool for distributing space among components or aligning them within a container.

When not to use it

While Flexbox is the default, for positioning an element outside the normal layout flow, like a floating action button or a modal overlay, you would use position: 'absolute'. For very complex grid layouts that don't fit a single-axis model, you might reach for a third-party library, but Flexbox can handle most cases surprisingly well.

One canonical example

To perfectly center a child component inside a parent container, give the parent container these styles: flex: 1, justifyContent: 'center', and alignItems: 'center'. flex: 1 makes the container fill all available space. Since the default flexDirection is 'column', justifyContent: 'center' centers the child vertically. alignItems: 'center' then centers it horizontally along the cross axis. This three-property combination is a fundamental pattern for centering content.

Interview question

To perfectly center a child component both horizontally and vertically within its parent using Flexbox, which properties should be applied to the parent container?

  • a.position: 'absolute', top: '50%', left: '50%'
  • b.flexDirection: 'row', justifyContent: 'center', alignItems: 'center'
  • c.flex: 1, justifyContent: 'center', alignItems: 'center'Correct
  • d.flex: 1, alignSelf: 'center', margin: 'auto'
Why?

Option C is correct because 'flex: 1' makes the parent fill available space, 'justifyContent: 'center'' centers children along the primary axis (vertical by default), and 'alignItems: 'center'' centers them along the cross axis (horizontal by default). Option B is incorrect because setting 'flexDirection: 'row'' would change the primary axis, altering how 'justifyContent' and 'alignItems' center the content relative axes.

Just read this? Test yourself on what you have been reading.

Read the original → reactnative.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react native — each one lists the topics its interview covers.

See open roles