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.

A style value that moves, such as a position that you set from state on each frame, doesn't add a CSS rule for each value. In the browser, a property gets a class for its first 64 values. A new value after that goes into the style attribute of the element. The result on the screen is the same. For an animation, use Animated, which writes the style attribute too.

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.

Background gradients​

<View
style={{
height: 120,
backgroundImage: [
{
type: 'linear-gradient',
direction: 'to bottom',
colorStops: [{ color: '#6750a4' }, { color: 'transparent', positions: ['80%'] }],
},
],
}}
/>
  • backgroundImage takes the React Native array of linear-gradient and radial-gradient objects, or a CSS string. One invalid layer drops the whole value, as on native.
  • backgroundSize, backgroundPosition, and backgroundRepeat take the React Native array, with one item for each layer, or a CSS string.
  • The experimental_ names of these four keys work too. When you set both names, the name without experimental_ wins.
  • A string goes to CSS as it is. So CSS that native can't read works only on the web.
  • A circle with a size of { x, y } uses x as its radius.

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 two keys:

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

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