Skip to main content

CSS Processing

There are significant differences between the CSS standard and how styles are handled in React Native. Most notably, โ€‹Text styles don't inherit from โ€‹View styles. The reconciliation is handled by the โ€‹TRE.

Inheritance#

As stated before, a React Native โ€‹View cannot receive textAlign style. It could even crash the native app. The โ€‹@native-html/css-processor and โ€‹@native-html/transient-render-engine libraries handle inheritable CSS properties by merging inheritable properties in the โ€‹TRT.

CSS Inheritance Example
import React from 'react';
import { useWindowDimensions } from 'react-native';
import RenderHtml from 'react-native-render-html';
const source = {
html: `<div style="text-align: center;">
This text is centered!
</div>`
};
export default function App() {
const { width } = useWindowDimensions();
return (
<RenderHtml
contentWidth={width}
source={source}
/>
);
}
Press on the button below to show how this code renders on your device.

Translation#

Every CSS property has a validator registered in the CSS processor, in charge of validating values for those rules. Validators are multipurposed:

  1. Handle special units such as absolute (pt), relative (rem) and keywords such as small (font-size) and thin (for borders) and translate those values in DIP (device independent pixels).

  2. Group properties by inheritability, native target (block for โ€‹View and text for โ€‹Text), and compatibility (native for properties translatable as native styles and web for other properties).

  3. Discard properties which values are invalid.

important

Note that special inherit, initial and unset values are not supported.

Mixed Styles Declaration#

Mixed styles declarations (โ€‹MixedStyleDeclaration) are a blend between React Native styles (ViewStyle, TextStyles) and CSS properties such as those handled by the CSS processor. This format enables features that cannot be handled directly by React Native style engine. For example โ€‹white-space collapsing, โ€‹list-style-type, font selection (see โ€‹Textual page) or โ€‹object-fit support in images (see โ€‹Images page).

A mixed styles declaration.
const mixedStyles = {
whiteSpace: 'normal',
color: 'gray'
}

Specificity#

Despite its lack of complex selectors, CSS specificity will be honoured by this library. Most notably, a CSS property will be resolved by applying the following precedence in ascending order (latest will override formers):

  1. Inherited styles

  2. User Agent styles (see โ€‹Transient Render Engine, element model)

  3. Tags styles (โ€‹tagsStyles prop)

  4. Classes styles (โ€‹classesStyles prop)

  5. IDs styles (โ€‹idsStyles prop)

  6. Inline styles (โ€‹style DOM node attribute)

So inline styles will take precedence over IDs styles, which will take precedence over classes styles, ...etc.

important

important! directives, complex selectors such as div > *, pseudo-classes and pseudo-elements are not supported.