Skip to main content

Styles

You style with the style prop and StyleSheet.create, as on native. At runtime, each declaration becomes one atomic CSS class, and every element with the same declaration shares that class.

const styles = StyleSheet.create({
card: { padding: 16, borderRadius: 12, backgroundColor: '#fff' },
})

Prefer StyleSheet.create for static styles. Its objects are frozen, so their class names compile once. An inline style object compiles again on each render.

Defaults​

Every View starts with the React Native defaults: display: flex, flexDirection: 'column', alignItems: 'stretch', flexShrink: 0, position: 'relative', and a border box.

Every Text starts at 14 px, black, in the system font, and keeps its spaces and line breaks.

Any style of yours wins over a default, as on native. For example, flex: 1 beats the default flexShrink: 0.

Values​

  • Numbers get px. padding: 16 is 16px, and lineHeight: 20 is 20px, as on native. flex, flexGrow, flexShrink, opacity, zIndex, aspectRatio, and fontWeight stay unitless.
  • Strings pass through. '50%' and 'auto' work as they do on native.
  • Colors use the React Native parser. Named colors, hex, rgb(), hsl(), and hwb() all work. A color that React Native can't parse is dropped.

Flex​

flex follows the React Native rules, not the CSS ones:

ValueCSSMeaning
flex: 11 1 0%Grow and shrink, from a size of 0
flex: 00 0 autoKeep the size of the content
flex: -10 1 autoKeep the size of the content, shrink if needed

gap, inset, display: 'contents', and position: 'static' pass through to CSS.

Shorthands and longhands​

A longhand always wins over a shorthand, whatever the key order, as on native. So flexShrink or flexBasis wins over the matching part of flex:

// The top padding is 16. The other three sides are 8.
const styles = StyleSheet.create({ box: { paddingTop: 16, padding: 8 } })

Right-to-left​

The start and end props map to CSS logical properties, so the browser flips them when the page direction is right-to-left:

PropCSS
marginStart, marginEnd, paddingStart, paddingEndmargin-inline-start and the others
start, endinset-inline-start, inset-inline-end
borderStartWidth, borderEndWidth, borderStartColor, borderEndColorborder-inline-start-width and the others
borderTopStartRadius, borderTopEndRadius, borderBottomStartRadius, borderBottomEndRadiusborder-start-start-radius and the others

marginHorizontal and paddingHorizontal set the left and right sides, as on native.

marginLeft, paddingLeft, left, and their right partners swap sides in a right-to-left layout, as on native. I18nManager.swapLeftAndRightInRTL(false) keeps them on the physical side. See I18nManager.

Transforms, shadows, and filters​

  • transform takes the React Native array, for example [{ translateX: 10 }, { rotate: '45deg' }], or a CSS string. transformOrigin works too.
  • boxShadow takes the React Native object array or a CSS string.
  • shadowColor, shadowOffset, shadowOpacity, shadowRadius become a box-shadow when you don't set boxShadow.
  • textShadowColor, textShadowOffset, textShadowRadius become a text-shadow.
  • filter takes the React Native array, for example [{ blur: 4 }], or a CSS string.

Fonts​

  • fontFamily: 'System' is the system font stack of the browser.
  • The named weights ('thin', 'light', 'medium', 'semibold', 'heavy', 'black', and the others) become their numbers.
  • fontVariant takes the React Native array.

Native-only style props​

elevation, borderCurve, includeFontPadding, and overlayColor have no web effect. The package accepts them and ignores them.

Types​

The style props take the types of React Native 0.88: ViewStyle, TextStyle, and ImageStyle. A style that type-checks on native type-checks here, and StyleSheet.create checks each key and value, as on native.

The types differ from native for three keys:

KeyOn the web
touchActionWeb only. Set it on an element that you drag on a touch screen. See PanResponder.
transformTakes no Animated value, as a plain View can't animate it. Use Animated.View.
resizeModeHas no 'none' yet.

The package passes any other CSS property through to CSS, but the types don't have it. Cast the style:

import type { ViewStyle } from 'react-native'

const fade = { transition: 'opacity 200ms' } as ViewStyle

// React Native's cursor is 'auto' or 'pointer', so another value needs `unknown` first.
const grab = { cursor: 'grab' } as unknown as ViewStyle