Styling and flexbox on mobile
You'll learn how styles work in React Native and how flexbox places boxes on a phone screen.
Watch & read
Start with the required ones. The others are there when you need them.
See it step by step
Change one style at a time and watch the boxes move on the phone.
Explanation
You want each habit to be one row: a tick box on the left, the name next to it, and the streak pushed to the right edge. You already know flexbox from CSS. On a phone it is the same idea with three changes.
1. Styles are JavaScript objects. Names are written in camelCase: background-color becomes
backgroundColor, font-size becomes fontSize. Sizes are plain numbers with no px. React Native
scales them so a box looks the same size on a small and a big phone.
const styles = StyleSheet.create({row: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", padding: 12 },name: { color: "white", fontSize: 18 },});<View style={styles.row}>...</View>
2. Every View is already a flex container. There is no display: flex to write.
3. The default direction is column. Boxes stack top to bottom unless you say
flexDirection: "row". So on a phone justifyContent moves things up and down, and alignItems moves
them left and right, until you switch to a row. Think of a class register: names go down the page. You
only get a row when you ask for one.
flex: 1 means "take all the space that's left". Put it on the screen's outer View so it fills the
phone. style can also take a list, and later items win:
style={[styles.name, habit.done && styles.doneName]}. That is how one habit turns grey when it's done.
People often write justifyContent: "center" and expect the boxes to move to the middle, left to
right. In a column it moves them to the vertical middle. Check the direction first. The other common one
is CSS habits: "16px" or background-color don't work. Write 16 and backgroundColor.
When you read a style, find flexDirection first (no value means column). Draw an arrow for it. Then
justifyContent is "along the arrow" and alignItems is "across the arrow", the same as in Phase 1.
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1export default function App() {2 return (3 <View style={{ flex: 1, justifyContent: "center" }}>4 <View style={{ width: 60, height: 60, backgroundColor: "red" }} />5 <View style={{ width: 60, height: 60, backgroundColor: "green" }} />6 <View style={{ width: 60, height: 60, backgroundColor: "blue" }} />7 </View>8 );9}
1import { StyleSheet, Text, View } from "react-native";23const habits = [4 { id: "1", name: "Drink water", streak: 5 },5 { id: "2", name: "Read 10 pages", streak: 2 },6 { id: "3", name: "Walk 20 minutes", streak: 12 },
Runs in the browser. Choose “My Device” in Snack to try it on your phone with Expo Go.
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3A
Viewhas noflexDirection. How do its children line up? - 2 / 3A screen
Viewhasflex: 1andjustifyContent: "center", and nothing else. Where do its children go? - 3 / 3Which style is written correctly for React Native?
Explain it back
How confident are you with Styling and flexbox on mobile?
Done
StyleSheet and sketch where each box lands on the phone.