Questions tagged [styled-components]

styled-components is a JavaScript library for styling React applications. It removes the mapping between styles and components, and lets you write actual CSS augmented with JavaScript.

styled-components is an open source project developed by Glen Maddern and Max Stoiber that aims to make styling component-based systems, right now solely focussed on React, easier.


This is what using styled-components looks like:

import React from 'react';
import styled from 'styled-components';

// Create a <Title> react component that renders an <h1> which is
// centered, palevioletred and sized at 1.5em
const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;

// Create a <Wrapper> react component that renders a <section> with
// some padding and a papayawhip background
const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;

These two variables, Title and Wrapper, are now a React components you can render like any other React component:

<Wrapper>
  <Title>Hello World, this is my first styled component!</Title>
</Wrapper>

The above code rendered in the browser(Live demo)

For more information and examples see the official documentation!

4586 questions
24
votes
9 answers

How do I access Material-ui's theme in Styled Component

I'm using CRA with Material-ui and Styled Components type of styling. When building my CSS I want to access Material-ui's default theme. part of package.json: "dependencies": { "react": "^16.8.6", "react-dom": "^16.8.6", …
wojjas
  • 1,046
  • 1
  • 9
  • 21
23
votes
2 answers

Add Classes to Styled Component

I am trying to add class names to a React Component to make it easier for me to customize that component using Styled Components. Here is a simplified outline of the component: const SignupForm = props => (
Moshe
  • 6,011
  • 16
  • 60
  • 112
23
votes
4 answers

Styled Component styles are disabled in Chrome DevTools

I am working on a static page that uses React, Gatsby, and styled-components. When styling a page, my development workflow usually heavily involves Chrome DevTools, tweaking styles there until I have something that I like, and then implementing them…
lusk
  • 541
  • 5
  • 21
23
votes
6 answers

Rollup Error: 'isValidElementType' is not exported by node_modules/react-is/index.js

I'm building a bundle with rollUp using styled-components. My rollup.config.js looks like: import resolve from 'rollup-plugin-node-resolve' import babel from 'rollup-plugin-babel' import commonjs from 'rollup-plugin-commonjs' export default { …
FabioCosta
  • 3,069
  • 6
  • 28
  • 50
23
votes
2 answers

Can't get Jest to work with Styled Components which contain theming

The Problem I've been using Jest and Enzyme to write tests for my React components build with the awesome Styled Components library. However, since I've implemented theming all my tests are breaking. Let me give you an example. This is the code of…
DavidWorldpeace
  • 519
  • 2
  • 7
  • 22
23
votes
3 answers

Adding transitions to styled components

I have following component in React: const Button = styled.div` width: 30px; height: 30px; position: absolute; right: 2em; top: 50%; transform: translateY(-50%); padding: 0; margin:…
H. Doe
  • 545
  • 2
  • 5
  • 12
21
votes
5 answers

Flash Of Unstyled Text (FOUT) on reload using next.js and styled components

I'm using global style from styled components with next.js and every time I reload my page I can see the font flickering. I have my font files in public/fonts/Inconsolata I've looked everywhere in spectrum chat, next.js github issues but can't…
m00
  • 297
  • 1
  • 5
  • 13
21
votes
5 answers

What is the use case of Styled-Components' .attrs() function?

I came across the .attrs() function in styled components, but I do not understand what it does, or what it does differently? I tried to understand the example in their docs, but as far as I am concerned, I can do the exact same thing without the…
R. Kohlisch
  • 2,823
  • 6
  • 29
  • 59
21
votes
5 answers

target first-child css styled-components

I am using styled-components and want to target the first child of Text, but am unable to do so. const Text = styled.p` font-size: 12px; &:first-child { margin-bottom: 20px; } `; ... component return(

I am…

peter flanagan
  • 9,195
  • 26
  • 73
  • 127
20
votes
5 answers

How can I use useTheme in Material UI 5?

I just started using Material UI 5.0.4 (with styled-components), and I wanted to access the theme in a component. I looked online and saw useTheme, so I checked the docs and found it - @mui/styles/useTheme. However, it was the legacy documentation,…
Ayush Garg
  • 2,234
  • 2
  • 12
  • 28
20
votes
3 answers

styled-component .attrs - React does not recognize prop

I am trying to pass down a prop into my styled component. It works as expected, but React throws the known 'Unknown Prop' error. I tried to use the spread operator at numerous places but neither did work. The styled component I want to pass down the…
psteinroe
  • 493
  • 1
  • 6
  • 18
20
votes
2 answers

How styled-components affect performance?

Is using styled-components slows down the web app more than stylesheets do? If I care about the performance and don't have any styles that depend on props, should I just ditch styled-components and use stylesheets instead?
user6898463
20
votes
6 answers

How to achieve tag agnostic styled components?

If I want a button but, only the presentational part of that, so if I do: import styled from 'styled-components' const Button = styled.button` color: red; text-align: center; ` I'm forced to render a button tag, but what about if semantically…
cl0udw4lk3r
  • 2,663
  • 5
  • 26
  • 46
20
votes
1 answer

Using Animated with styled-components (react-native)

I'm experiencing an error stating following (tested via iOS): Cannot read property 'getScrollableNode' of null While trying to use react-native's Animated along side styled-components styling tool for react and react-native. Here is example of a…
Ilja
  • 44,142
  • 92
  • 275
  • 498
19
votes
4 answers

Styling Nested Components in Styled-Components

I have created a Dropdown Component in React using Styled Components. Here is a simplified outline of the component: const Dropdown = ( {title} {children} …
Moshe
  • 6,011
  • 16
  • 60
  • 112