GitHub
StylingStyling

Styling

Because a document without styles would be very boring, react-pdf ships a powerful styling solution using CSS and Flexbox.

StyleSheet API

React-pdf also sticks with the primitives specs when it comes to styling.

StyleSheet.create()

Creates a stylesheet from a plain object of CSS definitions. Each key becomes a style you can hand to a component through its style prop.

import { StyleSheet, Page, Text } from '@react-pdf/renderer';

const styles = StyleSheet.create({
  page: { padding: 40 },
  title: { fontSize: 18, marginBottom: 12 },
});

const Report = () => (
  <Page style={styles.page}>
    <Text style={styles.title}>Quarterly report</Text>
  </Page>
);

Inline styling

There's no need to call StyleSheet.create in order to style components. A plain JS object works just as well.

<Text style={{ fontSize: 18, marginBottom: 12 }}>Quarterly report</Text>

Mixing both solutions

The style prop also accepts an array. Entries are merged left to right, and falsy ones are skipped, which is what you want for styles that depend on props.

const Badge = ({ overdue }) => (
  <Text style={[styles.badge, overdue && { color: 'tomato' }]}>
    {overdue ? 'Overdue' : 'Paid'}
  </Text>
);

Media queries

There may be times in which you'll need to apply different styles based on the document context. For that, we provide media-queries support (just as you would do it for the web!). You can query based on both width and height (min and max), and also orientation:

const styles = StyleSheet.create({
  row: {
    flexDirection: 'row',
    '@media max-width: 400': { flexDirection: 'column' },
    '@media orientation: landscape': { gap: 20 },
  },
});

The example below renders the same component on a 500pt page and a 300pt one:


Valid units

UnitMeaning
ptPoints. The default, based on the 72 dpi PDF document
inInches
mmMillimeters
cmCentimeters
%Percentage of the parent
vwPercentage of the page width
vhPercentage of the page height

Valid CSS properties

Flexbox
  • flex
  • flexDirectionrow · column · row-reverse · column-reverse
  • flexWrapnowrap · wrap · wrap-reverse
  • flexFlow
  • flexGrow
  • flexShrink
  • flexBasis
  • alignContent
  • alignItems
  • alignSelf
  • justifyContent
  • gap
  • rowGap
  • columnGap
Layout
  • displayflex · none
  • positionstatic · relative · absolute
  • top
  • right
  • bottom
  • left
  • zIndex
  • overflowhidden
  • aspectRatio
  • floatleft · right · none
  • clearleft · right · both · none
  • shapeOutsidecircle · ellipse · polygon · inset
Dimension
  • width
  • height
  • minWidth
  • minHeight
  • maxWidth
  • maxHeight
Spacing
  • margin
  • marginHorizontal
  • marginVertical
  • marginTop
  • marginRight
  • marginBottom
  • marginLeft
  • padding
  • paddingHorizontal
  • paddingVertical
  • paddingTop
  • paddingRight
  • paddingBottom
  • paddingLeft
Border
  • border
  • borderWidth
  • borderColor
  • borderStylesolid · dashed · dotted
  • borderRadius
  • borderTop
  • borderTopWidth
  • borderTopColor
  • borderTopStyle
  • borderRight
  • borderRightWidth
  • borderRightColor
  • borderRightStyle
  • borderBottom
  • borderBottomWidth
  • borderBottomColor
  • borderBottomStyle
  • borderLeft
  • borderLeftWidth
  • borderLeftColor
  • borderLeftStyle
  • borderTopLeftRadius
  • borderTopRightRadius
  • borderBottomRightRadius
  • borderBottomLeftRadius
Color
  • color
  • backgroundColor
  • opacity
Text
  • fontFamily
  • fontSize
  • fontStylenormal · italic · oblique
  • fontWeight
  • fontFeatureSettings
  • letterSpacing
  • lineHeight
  • textAlignleft · center · right · justify
  • textDecorationunderline · line-through · none
  • textDecorationColor
  • textDecorationStyle
  • textIndent
  • textOverflowellipsis
  • textTransformuppercase · lowercase · capitalize · upperfirst
  • verticalAlignsub · super
  • directionltr · rtl
  • maxLines
Image
  • objectFitfill · contain · cover · scale-down · none
  • objectPosition
Transform
  • transformrotate · scale · translate · skew · matrix
  • transformOrigin

On this page