Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Components and Styling

Styling and Flexbox in React Native

Style Expo apps with StyleSheet.create and flexbox: flexDirection, justifyContent, alignItems, flex, gap, percentages, shadows, useWindowDimensions and responsive patterns.

React Native has no CSS files. Styles are plain JavaScript objects with camelCase property names, passed to the style prop. Layout uses flexbox for everything, with defaults that differ from the web: the main axis is vertical (flexDirection: 'column'), and every View is already a flex container. Once you understand five properties, you can build nearly any layout.

The code is read-only (it needs a device), and a live stand-in implements a small flexbox calculator so you can see how the numbers come out.

StyleSheet.create

TSX
import { StyleSheet, View, Text } from 'react-native';

export function Card() {
  return (
    <View style={styles.card}>
      <Text style={styles.title}>Pine Ridge Loop</Text>
      <Text style={[styles.meta, styles.highlight]}>8.4 km, moderate</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  card: { padding: 16, borderRadius: 12, backgroundColor: 'white', gap: 4 },
  title: { fontSize: 18, fontWeight: '700' },
  meta: { fontSize: 14, color: '#555' },
  highlight: { color: '#0b5fff' },
});

StyleSheet.create gives type checking and a single place for styles. Pass an array to combine styles; later entries win ([styles.meta, styles.highlight], and false/undefined entries are ignored, perfect for conditions): style={[styles.btn, disabled && styles.btnDisabled]}.

Rules to remember:

  • Numbers are density-independent units (roughly points), so no px. Strings are for colors, percentages ('50%') and a few others ('45deg').
  • Names are camelCase: backgroundColor, borderTopLeftRadius, marginHorizontal.
  • Shorthands that do not exist in CSS but do in RN: paddingHorizontal, paddingVertical, marginHorizontal, marginVertical.
  • No cascade, no selectors, no em. Pass values down with props, context or a theme (Themes, Dark Mode and Fonts).
  • Recent versions support gap, boxShadow, filter, mixBlendMode, aspectRatio and display: 'contents'.

The five flexbox properties

Property Controls Values
flexDirection main axis column (default), row, column-reverse, row-reverse
justifyContent spacing along the main axis flex-start, center, flex-end, space-between, space-around, space-evenly
alignItems alignment on the cross axis stretch (default), flex-start, center, flex-end, baseline
flex how much of the leftover space a child takes number, such as 1
gap / rowGap / columnGap space between children number

Others you will reach for: flexWrap, alignSelf, flexGrow, flexShrink, flexBasis, position: 'absolute' with top/left/right/bottom, zIndex, overflow.

Column is the default

Main axis Vertical by default
Cross axis Horizontal by default
flexDirection: row Swaps the two axes

justifyContent follows the main axis, alignItems the cross axis.

1 / 2

flex: 1 means "take the rest"

A root screen almost always starts with flex: 1 so it fills the screen. Among siblings, flex values divide the leftover space proportionally: three children with flex: 1, 1, 2 get a quarter, a quarter and a half.

JS A tiny flexbox calculator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Common layouts

Row with an icon, text and a trailing action:

TSX
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 12 }}>
  <Image source={icon} style={{ width: 40, height: 40 }} />
  <Text style={{ flex: 1 }} numberOfLines={1}>A long title that should truncate</Text>
  <Text>โ€บ</Text>
</View>

flex: 1 on the text makes it take the middle and allows truncation. Centering content: justifyContent: 'center', alignItems: 'center' on the parent. Pinned footer: a flex: 1 content area above a fixed-height footer. Grid: flexDirection: 'row', flexWrap: 'wrap' with children width: '48%' (or use numColumns on a FlatList).

Sizes, spacing and percentages

Dimensions: width, height, minWidth, maxWidth, aspectRatio. Percentages are relative to the parent. Spacing: margin, padding with Top/Bottom/Left/Right/Horizontal/Vertical variants (use Start/End for right-to-left languages). borderWidth, borderColor, borderRadius. There is no box-sizing choice: sizes work like border-box.

Shadows differ by platform: iOS uses shadowColor, shadowOffset, shadowOpacity, shadowRadius; Android uses elevation. Newer versions also support the CSS-like boxShadow string on both: boxShadow: '0 2 8 rgba(0,0,0,0.15)'.

Responsive layouts

Screens come in many sizes. Use useWindowDimensions, which re-renders on rotation or window resize:

TSX
import { useWindowDimensions } from 'react-native';

function Gallery() {
  const { width } = useWindowDimensions();
  const columns = width >= 900 ? 4 : width >= 600 ? 3 : 2;
  const gap = 8;
  const itemSize = (width - gap * (columns + 1)) / columns;
  // ...
}

Prefer flex and percentages over fixed pixel widths. Never use Dimensions.get('window') at module level; it does not update.

JS Pick columns and item size

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Merging styles safely

StyleSheet.flatten turns an array into one object, which is useful when you need to read a value:

JS Style arrays resolve left to right

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Styling libraries

You are not locked into StyleSheet. Popular options include NativeWind (Tailwind classes), Unistyles (themes and breakpoints), Tamagui and Restyle. They are all plain npm packages; some need Babel/Metro setup and a development build. The ideas here (flexbox and style objects) are the foundation of every one of them.

Common mistakes

  • Expecting row layout by default. Set flexDirection: 'row' explicitly.
  • A parent without flex: 1 or a height, so flex: 1 children collapse to zero.
  • Using string numbers ('16') for sizes; use numbers.
  • Setting color on a View; set it on Text.
  • Hard-coded widths that break on small phones and tablets.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the default flexDirection in React Native?

    React Native lays children out vertically by default, unlike web CSS.
  2. 2. Which style centers a child both ways inside its parent?

    Use both flex alignment properties on the container.
  3. 3. With siblings flex: 1, flex: 1 and flex: 2 in a container, what share does the last child get?

    The free space is divided 1:1:2, so the last child gets 2 of 4 parts, one half.
  4. 4. How do you apply a style conditionally?

    Style arrays ignore falsy entries and later entries override earlier ones.

Summary

  • Styles are JS objects; combine them with arrays and organize them with StyleSheet.create.
  • Flexbox is the layout system: column by default, justifyContent on the main axis, alignItems on the cross axis, flex for shares.
  • Use useWindowDimensions, percentages and flex for responsive screens.
  • No CSS inheritance: pass values by props or a theme.

Next: Lists and FlatList