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
52
votes
6 answers

How to fix 'Static HTML elements with event handlers require a role.'?

My reactjs styledcomponent contains this code: this.gotoLink()}> This works fine but the eslint is complaining: Static HTML elements with event handlers require a role. How can I fix this…
bier hier
  • 20,970
  • 42
  • 97
  • 166
52
votes
3 answers

Styled-components organization

I want to use styled-components in my app and I am wondering how to organize it. Basically styled-components are assigned to specific component for reusability. But what about styled-components which I would like to use many times in other…
Gemmi
  • 1,252
  • 2
  • 13
  • 26
51
votes
7 answers

Typescript styled-component error: "Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'."

Getting a typescript error on my styled-component Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'.ts(2559) import React from 'react' import { NotificationSuccess, NotificationError } from…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
50
votes
2 answers

Styled-Components vs SASS (SCSS) or LESS

I came across a ReactJS Boilerplate which had good reps and is community driven. Styling section emphasized more on styled component CSS but never stopped from switching to conventional CSS styling methodologies. Although this pulled my interests…
NiRUS
  • 3,901
  • 2
  • 24
  • 50
39
votes
2 answers

Extending styles with styled-components not working

I'm trying to extend styles for a react component using styled-components but is not working. AFAIK, I'm doing it the right way, but perhaps I'm missing something... Here is what I have: import React from "react"; import styled from…
sebazelonka
  • 772
  • 2
  • 6
  • 13
36
votes
2 answers

How to extend styled components?

I have a styled component: interface FlexContainerProps { children: any; className?: string; } function FlexContainer(props: FlexContainerProps) { const Container = styled.div` display: flex; flex-direction: column; …
user1283776
  • 19,640
  • 49
  • 136
  • 276
36
votes
7 answers

React SVG component as background-image to div

I am trying to get styled-components to take an SVG that is a react component and set it as a background-image but I get the error: TypeError: Cannot convert a Symbol value to a string SVG component code: import React from "react"; const testSVG…
user8467470
  • 780
  • 3
  • 10
  • 25
36
votes
3 answers

Styled-Components: specify styles of children on parents hover

I have a simple component Here are 2 version of it - with and without styled-components: Without Styled Components
#container { width: 100px; height: 100px; } #kid { width: 20px; …
kurumkan
  • 2,635
  • 3
  • 31
  • 55
35
votes
2 answers

Impossible to edit styles in Chrome?

Since I've implemented server side rendering and managing the styles with styled-components, Im unable to edit styles in Chrome Dev Tools. The styles in devtools become italic and theres no checkbox to turn off/on specified style. It works properly…
35
votes
3 answers

Using Javascript Variables with styled-components

I'm using styled-components to build my components. All style properties which accept custom values are reused throughout my components (as it should be). With that in mind, I'd like to use some sort of global variables so that updates will…
colmtuite
  • 4,311
  • 11
  • 45
  • 67
35
votes
6 answers

Using ReactCSSTransitionGroup with styled-component

I'm using styled-components instead of tradition way of css. But I don't know how it can work together with ReactCSSTransitionGroup. Basically, ReactCSSTransitionGroup looks for certain classnames in css resource, then apply to a component…
Stanley Luo
  • 3,689
  • 5
  • 34
  • 64
34
votes
5 answers

How to integrate Nextjs + styled-components with material-ui

1. To build a next.js app using styled components it's really easy. You just have to use their _document.js snippet to enable SSR and prevent styles flickering on page load:…
Amet Alvirde
  • 1,453
  • 3
  • 13
  • 22
32
votes
2 answers

Styled-component vs jss vs emotion for React

I'm trying to understand the best choice (as a CTO) between jss emotion styled-component. I will try not to make the question "too wide" or "off-topic", because it's a very subjective topic. I'll try to answer (here) the question myself if no-one…
Cyril CHAPON
  • 3,556
  • 4
  • 22
  • 40
31
votes
2 answers

What is the purpose of template literals (backticks) following a function in ES6?

In GraphQL you can write something like this to define a query: const USER_QUERY = gql` { user(id: 2) { name } } ` In styled components you can define a styled component like this: const Button = styled.button` …
31
votes
5 answers

How to pass props to keyframes in styled-component with react?

I have following code and I want to pass value of y from react component to moveVertically keyframe. Is it possible to do so ? import React from 'react'; import styled, {keyframes} from 'styled-components'; const moveVertically =…
LoneCuriousWolf
  • 620
  • 1
  • 6
  • 16