Styling & Flexbox on Mobile

Styles are written with StyleSheet.create: camelCase, numbers without units, combined as [a, b] (the last wins), no cascade. The unit is dp: padding: 16…

Look for a .css file in a React Native project and you won't find one. Styles are written as JavaScript objects, and the layout is always flexbox. It works like the receipt board on the wall of Mrs. Rini's shop: receipts are pinned from top to bottom, one after another, and if you want some side by side, you have to say "line them up" first. In React Native, every View is a board like that: its children are stacked downward (column), unless you write flexDirection: 'row'.

StyleSheet: styles as objects

const styles = StyleSheet.create({
  status: { fontSize: 12, lineHeight: 16, fontWeight: '600' },
});

<Text style={[styles.status, { color: '#1d4ed8' }]}>Ironing</Text>

Other options if you already know Tailwind:

Way When to pick it
StyleSheet.create (this lesson) built into React Native, no extra package, and the base under every other way
NativeWind, <Text className="text-xs font-semibold"> you want to write Tailwind classes on React Native components. Its stable version 4.2.7 uses Tailwind CSS v3. Tailwind v4 is only supported by NativeWind 5, still a release candidate (npm, 15 November 2026)

Units: dp, not pixels

Every size in React Native is in dp (density-independent pixels), called points on iOS. 1 dp looks about the same size on any screen. An Android phone with a 720 × 1,600 pixel screen at density 2 reports itself as 360 × 800 dp. The iPhone 15 has a 1,179 × 2,556 pixel screen at scale 3, so it is 393 points wide.

You write Phone at density 2 iPhone 15 (scale 3)
padding: 16 32 physical pixels 48 physical pixels
width: 96 192 physical pixels 288 physical pixels

PixelRatio.getPixelSizeForLayoutSize(16) does the math for you: 48 on the iPhone 15. Font sizes also follow the user's text setting on the phone (fontScale), because allowFontScaling on Text defaults to true. Percentages work for sizes too: width: '50%'.

Flexbox: defaults that differ from the web

React Native computes layout with Yoga, Meta's flexbox engine. The rules are flexbox, but some starting values differ:

Property Web (CSS) React Native
display block, inline and others every View is already flex
flexDirection row column
flexShrink 1 (may shrink) 0 (doesn't shrink)
alignContent normal flex-start
position static relative
box sizing content-box border-box (padding counts inside width)

flex: 1 in React Native means: grow to fill the remaining space along the parent's main axis, from a basis of 0. That's how an element fills the available space, for example a screen { flex: 1 } under a header. height: '100%' takes the parent's full height regardless of its siblings, so next to a header it is too tall and spills over.

Now the order row on a 360 dp phone. Left and right padding 16, a gap of 12, a 96 wide badge. Say the customer's name is long: 260 dp wide when written on one line. Guess first: without flex: 1 on the name part, where does the status badge on the right end up? What Yoga computes:

Name part Name width Badge starts at Badge ends at
no flex 260 16 + 260 + 12 = 288 384, 24 dp past the screen edge
flex: 1 360 - 32 - 12 - 96 = 220 248 344, exactly 16 from the edge

Without flex: 1, the name part refuses to shrink (flexShrink 0), so it stays 260 wide and the status badge is pushed 24 dp off the screen. With flex: 1, the name part takes the remaining space, 220 dp, and the name wraps onto a second line. flexShrink: 1 fixes it too.

OrderRow, number by number

// src/app/(tabs)/index.tsx
import { router } from 'expo-router';
import { FlatList, Pressable, StyleSheet, Text, View, useColorScheme, useWindowDimensions } from 'react-native';
import { PRICE_PER_KG, ORDERS, formatPrice, type Order } from '@/data/orders';

const COLORS = {
  light: { bg: '#ffffff', text: '#111827', muted: '#4b5563', border: '#e5e7eb', badge: '#dbeafe', badgeText: '#1d4ed8' },
  dark: { bg: '#111827', text: '#f9fafb', muted: '#9ca3af', border: '#374151', badge: '#1e3a8a', badgeText: '#bfdbfe' },
};

function OrderRow({ order }: { order: Order }) {
  const w = COLORS[useColorScheme() === 'dark' ? 'dark' : 'light'];
  return (
    <Pressable
      style={[styles.row, { backgroundColor: w.bg, borderBottomColor: w.border }]}
      onPress={() => router.push(`/order/${order.id}`)}>
      <View style={styles.info}>
        <Text style={[styles.receipt, { color: w.text }]}>#{order.id} {order.name}</Text>
        <Text style={[styles.detail, { color: w.muted }]}>
          {order.kg} kg · {formatPrice(order.kg * PRICE_PER_KG)}
        </Text>
      </View>
      <View style={[styles.badge, { backgroundColor: w.badge }]}>
        <Text style={[styles.status, { color: w.badgeText }]}>{order.status}</Text>
      </View>
    </Pressable>
  );
}

export default function Orders() {
  const { width } = useWindowDimensions();
  const columns = width >= 600 ? 2 : 1;
  return (
    <FlatList
      key={columns}
      numColumns={columns}
      data={ORDERS}
      keyExtractor={(order) => order.id}
      renderItem={({ item }) => <OrderRow order={item} />}
    />
  );
}

const styles = StyleSheet.create({
  row: { flex: 1, flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 16, paddingVertical: 12, borderBottomWidth: 1 },
  info: { flex: 1 },
  receipt: { fontSize: 16, lineHeight: 22, fontWeight: '600' },
  detail: { fontSize: 14, lineHeight: 20, marginTop: 2 },
  badge: { width: 96, paddingVertical: 4, borderRadius: 12, alignItems: 'center' },
  status: { fontSize: 12, lineHeight: 16, fontWeight: '600' },
});

Screen width: useWindowDimensions

useWindowDimensions() gives { width, height, scale, fontScale } and changes when the phone rotates or the window is resized. On a 768 wide tablet, columns becomes 2: each row is 384 dp, and its name part 244. Dimensions.get('window') called once outside a component doesn't follow those changes.

numColumns must not change on the same FlatList. Without key={columns}, rotating the phone throws Changing numColumns on the fly is not supported. Change the key prop on FlatList when changing the number of columns to force a fresh render of the component. With the key, the FlatList is created fresh.

Safe area: the notch, the Dynamic Island and the home bar

The safe area is the part of the screen not covered by the status bar, a notch or the Dynamic Island, and the home bar. On Android, an Expo SDK 57 app is always drawn to the screen edges (edge-to-edge), so the status bar sits over your app there too. The headers from Stack and Tabs already handle it. The trouble starts when a header is hidden:

// src/app/(tabs)/account.tsx (this tab uses headerShown: false)
import { Text, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

export default function Account() {
  const insets = useSafeAreaInsets();
  return (
    <View style={{ flex: 1, paddingTop: insets.top + 16, paddingHorizontal: 16 }}>
      <Text style={{ fontSize: 24, fontWeight: '700' }}>Hello, Wulan</Text>
    </View>
  );
}

On an iPhone with a Dynamic Island, insets.top is 59 and insets.bottom 34, so the greeting starts at 75. react-native-safe-area-context 5.7.0 ships with Expo SDK 57, and Expo Router already mounts its SafeAreaProvider. SafeAreaView from react-native is deprecated and logs a warning.

Dark mode

⚠️ Why does the badge leave the screen, or the text vanish?

TL;DR: Styles are written with StyleSheet.create: camelCase, numbers without units, combined as [a, b] (the last wins), no cascade. The unit is dp: padding: 16 becomes 32 pixels on a density 2 phone and 48 on an iPhone 15. React Native's flexbox differs from the web: flexDirection defaults to column, flexShrink to 0, alignContent to flex-start, position to relative. flex: 1 makes an element fill the available space. In a 360 wide OrderRow, flex: 1 on the name part gives it 220 dp; without it a long name pushes the badge 24 dp off the screen. Use useWindowDimensions() for widths that can change (two columns from 600, with a key), useSafeAreaInsets() when a header is hidden (59 at the top of an iPhone with a Dynamic Island), and useColorScheme() plus userInterfaceStyle: "automatic" for dark mode.

⚡ Styles as objects

Write:

const styles = StyleSheet.create({
  status: { fontSize: 12, lineHeight: 16, fontWeight: '600' },
});

<Text style={[styles.status, { color: '#1d4ed8' }]}>Ironing</Text>

Meaning: Property names are camelCase, numbers have no unit, and the unit is dp: padding: 16 becomes 48 pixels on an iPhone 15. style={[a, b]} combines styles, and the last one wins. There are no classes and no cascade.

⚡ The default is column

Guess first: if flexDirection isn't written, which way are a View's children laid out?

Downward, column. On the web the default is row. Want them side by side? Write flexDirection: 'row'.

Meaning: Every View is already flex. Other defaults differ from the web too: flexShrink defaults to 0, so content refuses to shrink.

⚡ flex: 1 saves the badge

Write:

info: { flex: 1 },
Guess first: on a 360 dp wide phone, a long name 260 dp wide has no flex: 1. Where does the 96 dp status badge to its right end?

At 384, so 24 dp off the screen. The name part refuses to shrink because flexShrink is 0.

Meaning: With flex: 1, the name part takes the remaining space, 220 dp, and the name wraps onto a second line. The badge ends at 344, exactly 16 from the edge.

What you will learn