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: 16is16px, andlineHeight: 20is20px, as on native.flex,flexGrow,flexShrink,opacity,zIndex,aspectRatio, andfontWeightstay unitless. - Strings pass through.
'50%'and'auto'work as they do on native. - Colors use the React Native parser. Named colors, hex,
rgb(),hsl(), andhwb()all work. A color that React Native can't parse is dropped.
Flex
flex follows the React Native rules, not the CSS ones:
| Value | CSS | Meaning |
|---|---|---|
flex: 1 | 1 1 0% | Grow and shrink, from a size of 0 |
flex: 0 | 0 0 auto | Keep the size of the content |
flex: -1 | 0 1 auto | Keep 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:
| Prop | CSS |
|---|---|
marginStart, marginEnd, paddingStart, paddingEnd | margin-inline-start and the others |
start, end | inset-inline-start, inset-inline-end |
borderStartWidth, borderEndWidth, borderStartColor, borderEndColor | border-inline-start-width and the others |
borderTopStartRadius, borderTopEndRadius, borderBottomStartRadius, borderBottomEndRadius | border-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
transformtakes the React Native array, for example[{ translateX: 10 }, { rotate: '45deg' }], or a CSS string.transformOriginworks too.boxShadowtakes the React Native object array or a CSS string.shadowColor,shadowOffset,shadowOpacity,shadowRadiusbecome abox-shadowwhen you don't setboxShadow.textShadowColor,textShadowOffset,textShadowRadiusbecome atext-shadow.filtertakes 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%'] }],
},
],
}}
/>
backgroundImagetakes the React Native array oflinear-gradientandradial-gradientobjects, or a CSS string. One invalid layer drops the whole value, as on native.backgroundSize,backgroundPosition, andbackgroundRepeattake 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 withoutexperimental_wins. - A string goes to CSS as it is. So CSS that native can't read works only on the web.
- A
circlewith asizeof{ x, y }usesxas 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. fontVarianttakes 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:
| Key | On the web |
|---|---|
touchAction | Web only. Set it on an element that you drag along the axis of a scroll on a touch screen. See PanResponder. |
transform | Takes 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