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
19
votes
3 answers

adding two classes (active class) using styled-components

I am migrating from css to styled-components. My react component looks like the following: class Example extends React.Component { ........code here render() { return (
peter flanagan
  • 9,195
  • 26
  • 73
  • 127
18
votes
3 answers

TypeScript issue with styled-component's "css" prop and Storybook

I'm having issues with enabling styled-component's css prop in TypeScript as soon as I import something from Storybook, because Storybook depends on @emotion/core, whose type declaration defines Emotion's own css prop. The error stems from the fact…
silvenon
  • 2,068
  • 15
  • 29
18
votes
1 answer

How to pass props to a styled component in emotion? Using TypeScript

I am using styled by emotion at: import styled from '@emotion/styled' I am trying to pass props to a styled component like the guide mentions: https://emotion.sh/docs/styled It doesn't work for some reason. I use TypeScript as well. I pass props…
Contentop
  • 1,163
  • 3
  • 20
  • 43
18
votes
4 answers

Isolated styled-components with @font-face

I'm using https://github.com/styled-components/styled-components. I'm trying to work out the best strategy for components that require @font-face. I want to make sure each component is independent of its context, so I'm defining font-family styles…
Aaron McAdam
  • 706
  • 2
  • 7
  • 20
17
votes
2 answers

How to extend OverridableComponent interface in Material-UI

I'm trying to use Container component with styled-components using ContainerProps but then I can't pass component prop which belongs to OverridableComponent interface. Code below gives me error which tells me that I can't pass component property.…
Dooomel
  • 536
  • 5
  • 17
17
votes
1 answer

How to configure Prettier to format Styled Components conditional rendering

I'm using Prettier, Eslint and Styled components - backtick style declaration. It works so far but Prettier formats the conditional rendering of the Styled components in the way that Eslint doesn't allow and after the formatting Eslint starts to…
Velidan
  • 5,526
  • 10
  • 48
  • 86
17
votes
2 answers

Combine multiple styled elements with styled-components

I know that we can extend or add styling to existing components with styled-components like the Link component of react-router-dom. The said feature is indicated here. But, my problem is, how can I combine two or more existing components then add…
sdabrutas
  • 1,477
  • 2
  • 14
  • 28
17
votes
2 answers

Style a Slider Thumb with Styled Components

I am trying to style a slider with styled-components for React, but I do not get how I can style the thumb. I have a CSS that looks like this: .faderInput::-webkit-slider-thumb { -webkit-appearance: none; width: 15px; height: 15px; …
modmoto
  • 2,901
  • 7
  • 29
  • 54
17
votes
3 answers

How to render pseudo before content dynamically in styled-component

I'm having a trouble of rendering pseudo before content dynamically in styled-components. Am I doing something wrong? I have no problem when I render pseudo before content statically, but it doesn't work when I try it dynamically. React…
user5809117
16
votes
3 answers

No overload matches this call when using styled-components

There are several questions about this error on SO but none from anybody in a similar situation and none of the solutions I have found work so I'm going to post a specific question. When using a Styled component inside another component and passing…
jonhobbs
  • 26,684
  • 35
  • 115
  • 170
16
votes
2 answers

Why does styled-components 5.x warn about "Expected style to contain units."

When styling a React Native app with Styled Components 5.x I'm getting the warning... Expected style "borderWidth: 2" to contain units. This didn't happen with previous versions. What does the warning mean?
GollyJer
  • 23,857
  • 16
  • 106
  • 174
16
votes
3 answers

How to use Media Queries inside a React Styled Components Keyframe?

I can get media queries to work properly inside a regular styled-components component however when I attempted to use it in a keyframe (via import from styled-components) it does not seem to work at all. Attempting to get a div to animate to a…
damon
  • 2,687
  • 8
  • 25
  • 35
16
votes
1 answer

styled-components: extend styles and change element type

Imagine I have the following styles: color: black; border: 1px solid white; and I want to apply them both to two elements of different types: const SomeImg = styled.img` margin: 2em; `; const SomeDiv = styled.div` margin: 3em; `; How can I…
abagshaw
  • 6,162
  • 4
  • 38
  • 76
16
votes
2 answers

React Cannot find module - ttf, otf fonts

I'm trying to load local font like the code below but I keep getting Cannot find module '@/landing/fonts/Gotham-Bold.ttf' error and have no idea what is really wrong this path. my folder structure looks like this, and the code I'm working on is…
Phillip YS
  • 784
  • 3
  • 10
  • 33
16
votes
9 answers

Use 'active' state from React Router in Styled Components

React Router adds an active class to NavLinks when you are on the page that they link to. How can I access this property with Styled Components. I need to styled menu items differently when you are on their page. const LinkElem = styled(NavLink)` …
Evanss
  • 23,390
  • 94
  • 282
  • 505