Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 71 ยท Styling and Layout

Styling with StyleSheet

Learn how styling works in React Native: style objects, StyleSheet.create, style arrays, the missing cascade, density-independent units and platform differences.

React Native has no CSS files, no selectors and no stylesheet language. Instead, every component takes a style prop, and that prop is a plain JavaScript object whose keys look like CSS properties written in camelCase: backgroundColor, fontSize, borderRadius. The engine turns those objects into native view properties, so a View ends up as a real UIView on iOS or an Android ViewGroup.

This lesson covers the mechanics you will use in every screen: writing style objects, organizing them with StyleSheet.create, combining them with arrays, and understanding the places where React Native styling is deliberately different from the web. Layout (Flexbox, sizing, positioning) gets its own lessons next.

The real React Native runtime cannot run on this site, so React Native code appears in read-only blocks. The live examples are stand-ins: small functions and React components that reproduce the idea (merging styles, turning style objects into HTML) so you can experiment. They are not the real engine.

Style objects

A style is an object. You can write it inline:

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

export function Welcome() {
  return (
    <View style={{ padding: 16, backgroundColor: '#eef2ff', borderRadius: 12 }}>
      <Text style={{ fontSize: 20, fontWeight: '700', color: '#1e1b4b' }}>
        Hello, mobile
      </Text>
    </View>
  );
}

A few details already differ from CSS:

  • Numbers have no unit. padding: 16 means 16 density-independent pixels (more below). Writing '16px' is not valid.
  • Percentages are strings: width: '50%'.
  • fontWeight is a string ('400', '700', 'bold'), not a number.
  • Colors accept hex (#fff, #ffffff), rgb(), rgba(), hsl() and named colors such as 'tomato'.
  • There is no border: '1px solid red' shorthand. You write borderWidth: 1 and borderColor: 'red' separately (and borderStyle only when you want dashed or dotted).

StyleSheet.create

Inline objects work, but a screen full of them is hard to read. The convention is to collect styles in one place:

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

export function Welcome() {
  return (
    <View style={styles.card}>
      <Text style={styles.title}>Hello, mobile</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  card: { padding: 16, backgroundColor: '#eef2ff', borderRadius: 12 },
  title: { fontSize: 20, fontWeight: '700', color: '#1e1b4b' },
});

StyleSheet.create takes an object of named styles and returns an object with the same keys. In today's React Native it is a thin helper: it returns the object you gave it, but it gives you three real benefits.

  1. Type checking and autocomplete. In TypeScript, a typo like backgroundColour or fontWeight: 700 is flagged immediately.
  2. Named styles. styles.card reads better than a literal and can be reused.
  3. One place to look. Styles live at the bottom of the file, away from the markup.

Older articles say StyleSheet.create sends styles over the bridge once and passes numeric IDs afterwards. That was true in early versions and is no longer how it works, so do not rely on it as a performance trick. The practical rule is: use it for organization and types, and do not worry about inline styles being "slow" for ordinary screens.

Style arrays

The style prop also accepts an array, and arrays can be nested. React Native flattens them from left to right, so later entries override earlier ones. Falsy entries (false, null, undefined) are ignored, which makes conditional styling short:

TSX
<Pressable
  style={[styles.button, disabled && styles.buttonDisabled, style]}
/>

Here styles.button is the base, buttonDisabled applies only when disabled is true, and the style prop from the parent goes last, so a caller can override anything. This "base, variants, then caller" order is the standard pattern for reusable components.

The built-in helper that does the merging is StyleSheet.flatten. You rarely call it yourself, but it is useful when you need to read a final value, for example the final height of a style that may be an array. Try the logic with a stand-in that behaves like it:

JS A stand-in for StyleSheet.flatten

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

No output.

Look at the second line of output: backgroundColor is gray because buttonDisabled comes after button. In the third call the disabled style is skipped (the condition is false), the nested array is flattened, and the parent's borderRadius: 24 wins over the base 8.

There is no cascade

On the web, color and font-family set on a parent flow down to every child. In React Native, styles do not inherit between Views. If you set color: 'white' on a View, nothing happens: color is a text style and View does not accept it. Each Text gets its own text styles.

There is one exception, and it is narrow: a Text nested inside another Text inherits the parent's text styles. That is how you make a bold word inside a sentence:

TSX
<Text style={{ fontSize: 18, color: '#222' }}>
  Total: <Text style={{ fontWeight: '700' }}>$24.00</Text>
</Text>

Because nothing cascades, teams build small shared components (AppText, Card, Button) that carry the styling, rather than relying on inherited rules. We return to this idea in Theming and Dark Mode. Also notice there are no class names, media queries, :hover states or pseudo-elements. Pressed and focused states come from component state, covered in Pressable, Buttons and Touch Feedback.

Styling without CSS

Plain objects A style is a JavaScript object in camelCase.
StyleSheet.create Names and types, not magic.
Arrays Later entries win, falsy ones vanish.
1 / 2

Units: density-independent pixels

A phone screen with a 3x pixel density packs nine physical pixels into every logical square. React Native sizes are in density-independent pixels (dp), also called points on iOS: a width: 100 view looks about the same physical size on a modest phone and on a high-density one. The engine multiplies by the screen's pixel ratio when drawing.

Two consequences:

  • You never convert units yourself. Design in dp (the numbers in a design tool for a 390-wide phone frame map directly).
  • For a hairline border (the thinnest line the screen can show) use StyleSheet.hairlineWidth instead of a hand-picked 0.5.

Font sizes follow the same rule, and they are also scaled by the user's accessibility font size setting by default. We look at that in Accessibility in React Native. Note that lineHeight is a number in dp, not a multiplier as in CSS: lineHeight: 1.5 would be a tiny 1.5 dp line, while lineHeight: 24 is what you want.

Seeing styles turn into output

The real engine produces native views. The stand-in below turns the same style objects into HTML so you can see them. It converts the React Native-only shorthands (paddingHorizontal, marginVertical) and treats lineHeight as dp, the way React Native does, because plain CSS would not.

Stand-in: View and Text rendered as HTML

The "Shipped" badge uses the array pattern: base style, then active && styles.badgeActive. The "Custom" badge passes a style prop that goes last, so its borderRadius: 4 replaces the default. In a real app the View and Text imports come from react-native and the output is native, but the style logic is the same.

Platform differences to watch for

Most styles behave the same on iOS and Android, but a few do not:

  • Shadows. iOS uses shadowColor, shadowOffset, shadowOpacity and shadowRadius. Android uses a single number, elevation. Newer React Native versions also support a CSS-like boxShadow on both, so check the version you ship. Platform-Specific Code shows how to combine them.
  • Default fonts. iOS uses San Francisco, Android uses Roboto, and custom font names must match exactly what you loaded.
  • Text padding and clipping. Android adds extra internal padding to Text and TextInput, which includeFontPadding: false turns off when you need pixel-tight alignment.

A good habit is to test every new screen on both platforms before polishing details.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the style prop accept in React Native?

    style takes a plain object or an array that is flattened left to right. StyleSheet.create is optional: inline objects work too.
  2. 2. Given style={[a, cond && b, c]} where cond is false and both a and c set padding, which padding applies?

    Entries merge left to right, so c overrides a. The false entry is skipped. Values are replaced, never summed.
  3. 3. You set color: 'white' on a View that contains several Text components. What happens?

    There is no cascade between Views. Put text styles on Text, or build an AppText component. Only a Text nested in another Text inherits.
  4. 4. What does lineHeight: 1.5 mean in React Native?

    lineHeight is a number of dp, not a multiplier. For a 16 dp font you might use lineHeight: 24.

Summary

  • Styles are camelCase JavaScript objects passed to style; numbers are density-independent pixels, percentages and weights are strings.
  • StyleSheet.create returns the same object but adds names and type checking; it is not a performance trick any more.
  • Style arrays flatten left to right, ignore falsy entries, and let a caller's style go last to override.
  • Nothing cascades between Views; only a Text nested in a Text inherits text styles.
  • lineHeight is in dp, there is no border shorthand, and shadows differ between iOS and Android.
  • The live examples are stand-ins: they reproduce the merge and rendering ideas in the browser, not the native engine.

Next: Flexbox in React Native