Pass CSS styles as props in React TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
2 min

banner

# Pass CSS styles as props in React TypeScript

Use the React.CSSProperties type to pass CSS styles as props in React TypeScript.

The CSSProperties type is used to type the style object that consists of CSS property names and values.

App.tsx
import React from 'react'; type ButtonProps = { // 👇️ type as React.CSSProperties style: React.CSSProperties; children: React.ReactNode; }; function Button({style, children}: ButtonProps) { return <button style={style}>{children}</button>; } const App = () => { return ( <div> <Button style={{ padding: '2rem', fontSize: '3rem', backgroundColor: 'lime', }} > Click </Button> <h2 style={{fontSize: '3rem'}}>Hello world</h2> </div> ); }; export default App;

pass css styles as props in react typescript

The code for this article is available on GitHub

We typed the style object as React.CSSProperties.

When passing styles to the Button component, you will get autocomplete for property names.

You can figure out what the expected type for a specific prop is by using your IDE.

style prop cssproperties

In most IDEs, you will be able to hover over the prop and see its value.

I use VS Code, so I right-clicked on the style prop and then clicked "Go to Definition".

The definition of the style prop shows that its type is either CSSProperties or undefined.

If you need to pass a function as props in React TypeScript, check out the following article.

# Extending the HTML element in a component's props

You might also need to extend an HTML element in a component's props.

App.tsx
// 👇️ extend button props interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> { style: React.CSSProperties; children: React.ReactNode; } function Button({style, children}: ButtonProps) { return <button style={style}>{children}</button>; } const App = () => { return ( <div> <Button style={{ padding: '2rem', fontSize: '3rem', backgroundColor: 'lime', }} > Click </Button> <h2 style={{fontSize: '3rem'}}>Hello world</h2> </div> ); }; export default App;

extending html element in component props

The code for this article is available on GitHub

The example shows how to extend a button element in the props of our custom component.

We used the React.ButtonHTMLAttributes type to extend a button element in the component's props.

You can add your custom props to the interface and your component can be passed any of the element-specific props.

You can read more about extending an HTML element in a component's props in React TypeScript in the following article.

I've also written a detailed guide on how to use create-react-app with TypeScript.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.