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

React.js styled-components importing images and using them as div background

I am using styled-components and am trying to set a background image like so const HeaderImage= styled.div` background-image: url('../../assets/image.png'); '; I've also tried without the quotes, like so const HeaderImage= styled.div` …
vedran
  • 1,145
  • 2
  • 13
  • 19
30
votes
5 answers

@types/styled-components Duplicate identifier FormData

If I add @types/styled-components in my project, I will have a bunch of errors in the build output: ERROR in /Users/me/projects/react/node_modules/@types/react-native/globals.d.ts(36,15): TS2300: Duplicate identifier 'FormData'. ERROR in…
Dmitry Maksakov
  • 1,591
  • 1
  • 16
  • 22
30
votes
4 answers

styled-components not applying style to custom functional react component

When using styled-components to style a custom functional react component, the styles are not being applied. Here is a simple example where the styles are not being applied to the StyledDiv: const Div = () => (
test
) const StyledDiv =…
jjbskir
  • 8,474
  • 9
  • 40
  • 53
30
votes
6 answers

How to easily inspect styled-components using dev tools?

I am using styled-components in a React project. When the components are rendered in the browser they are assigned a randomly generated classname, for example:
This class name does not help me identify from which…
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
29
votes
3 answers

What is the benefit of using styled components over css modules in Reactjs

I've researching about what's the best way to style your page in React application. I see a lot of libraries to style in react like Styled Components, Glamor, CSS Modules. In my current project, I am using CSS Modules(mainly to get rid of css…
Bharat Soni
  • 2,824
  • 6
  • 23
  • 48
29
votes
3 answers

Using 'ref' on React Styled Components is not working

I am having difficulty using refs with Styled Components. When I try to access them in my class methods like below, I get the following error: Edit.js:42 Uncaught TypeError: this.....contains is not a function constructor(props) { .... …
nikjohn
  • 20,026
  • 14
  • 50
  • 86
29
votes
4 answers

How to get the theme outside styled-components?

I know how to get the theme from components that are created using the styled way: const StyledView = styled.View` color: ${({ theme }) => theme.color}; `; But how to get from normal components or apply it for different properties?…
Bruno Lemos
  • 8,847
  • 5
  • 40
  • 51
27
votes
5 answers

How to wrap up Ant Design with Styled Components and TypeScript?

I want to wrap up my ant-design components with styled-components, I know that this is possible (https://gist.github.com/samuelcastro/0ff7db4fd54ce2b80cd1c34a85b40c08) however I'm having troubles to do the same with TypeScript. This is what I have…
Samuel Castro
  • 291
  • 1
  • 3
  • 5
27
votes
3 answers

target child element styled components

I'm trying to change the nth-child(2) background color of the ColorBox element by targeting it as a child of ImgGrid but I just can't seem to get it to work, I have tried multiple variations with different elements and nothing seems to be…
tom harrison
  • 3,273
  • 11
  • 42
  • 71
25
votes
4 answers

Could not find a declaration file for module 'styled-components/native'

If you add styled-components to your React Native project, there's a sub-directory for the native components specifically: import styled from 'styled-components/native`; export const Container = styled.View` ... `; If you try to do this in a…
TheScrappyDev
  • 4,375
  • 2
  • 21
  • 25
25
votes
3 answers

React hook useRef not working with styled-components and typescript

I've some problem using the useRef hook with a styled component. Linter alerts me that Object is possibly 'null' inside the didMount useEffect. Any idea about this? This is not a duplicate for 2 reason: The old answer refers to the ref used in a…
vlk
  • 1,414
  • 1
  • 13
  • 22
25
votes
3 answers

Create new component and inherit styles from styled-component

const Button = styled.button` display: inline-block; width: 300px; background-color: black; ` const ButtonHref = styled.a` ${Button} ` So I have two styled-components. I want to inherit 'Button' styles but create another tag. I use…
Lamer_2005
  • 387
  • 2
  • 4
  • 9
25
votes
1 answer

Module not found: Can't resolve 'styled-component'

I created a new react app using create-react-app and then installed styled-component using npm. But when I use it in component, I am getting following error. Failed to compile. ./src/Components/Lightbox/styledLightbox.js Module not found: Can't…
Ishan Patel
  • 5,571
  • 12
  • 47
  • 68
24
votes
5 answers

(Next.js) How can I change the color of SVG in next/image?

import Image from 'next/image' ... emtion I want to change the SVG color in next/image. But in CSS, img { fill="#ffffff" } is not working. How can I solve this?
Dann1y
  • 257
  • 1
  • 3
  • 8
24
votes
1 answer

JavaScript optional chaining dynamic property

I am trying to access a dynamic property with safety provided by optional chaining that is available in TS. However it appears that this is not valid. export const theme = { headers: { h1: { }, h6: { color: '#828286' }, …
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291