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
153
votes
7 answers

Target another styled component on hover

What is the best way to handle hovers in styled-components. I have a wrapping element that when hovered will reveal a button. I could implement some state on the component and toggle a property on hover but was wondering if there is a better way to…
nickspiel
  • 5,170
  • 6
  • 33
  • 48
132
votes
6 answers

conditional rendering in styled components

How can I use conditional rendering in styled-components to set my button class to active using styled-components in React? In css I would do it similarly to this:
tom harrison
  • 3,273
  • 11
  • 42
  • 71
127
votes
12 answers

Warning: Received `false` for a non-boolean attribute. How do I pass a boolean for a custom boolean attribute?

Warning: Received `false` for a non-boolean attribute `comingsoon`. If you want to write it to the DOM, pass a string instead: comingsoon="false" or comingsoon={value.toString()}. How do I pass a boolean in a custom attribute for React? I'm…
desilijic
  • 1,405
  • 2
  • 9
  • 5
112
votes
13 answers

How to use google analytics with next.js app?

I'm using styled-components with next.js so my styles need to be server-side rendered, hence how can I add google analytics to my website? I checked next.js google analytics example but as I said my _document file is different because of using…
0xsh
  • 1,395
  • 3
  • 10
  • 18
104
votes
8 answers

Using styled-components with props and TypeScript

I'm trying to integrate TypeScript into our project and so far I stumbled upon one issue with styled-components library. Consider this component import * as React from "react"; import styled from "styled-components/native"; import { TouchableOpacity…
Ilja
  • 44,142
  • 92
  • 275
  • 498
84
votes
17 answers

Warning: Prop `className` did not match. when using styled components with semantic-ui-react

I use this code to margin my Button from top: const makeTopMargin = (elem) => { return styled(elem)` && { margin-top: 1em !important; } `; } const MarginButton = makeTopMargin(Button); and whenever i use…
79
votes
6 answers

Before and After pseudo classes used with styled-components

What is the proper way to apply :before and :after pseudo classes to styled components? I know that you can use &:hover {} to apply the :hover pseudo class to a styled-component. Does this work for All pseudo elements like before and after? I…
rimraf
  • 3,925
  • 3
  • 25
  • 55
72
votes
2 answers

styled-components - how to set styles on html or body tag?

Ordinarily, when using pure CSS, I have a style sheet that contains: html { height: 100%; } body { font-size: 14px; } When using styled-components in my React project, how do I set styles for the html or body tags? Do I maintain a separate…
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
71
votes
3 answers

idiomatic way to share styles in styled-components?

trying to port some code from jss to styled-components, jss code looks something like: //... const styles = { myStyles: { color: 'green' } } const {classes} = jss.createStyleSheet(styles).attach() export default function(props) { return…
tony_k
  • 1,983
  • 2
  • 20
  • 27
70
votes
5 answers

Using styled components with Typescript, prop does not exist?

Here is my styled component. import * as React from 'react'; import styled from 'styled-components'; import { ComponentChildren } from 'app-types'; interface Props { children: ComponentChildren; emphasized: boolean; } const HeadingStyled…
user1283776
  • 19,640
  • 49
  • 136
  • 276
66
votes
7 answers

React — Passing props with styled-components

I just read in the styled-components documentation that the following is wrong and it will affect render times. If that is the case, how can I refactor the code and use the required props to create a dynamic style? Thank you in advance. Tab…
Diego Oriani
  • 1,647
  • 5
  • 22
  • 31
60
votes
9 answers

How to disable the hover effect of material-ui button inside of a styled component

I added the css hover property to disable the button's hover effect, but it seems not work for my case, how should I fix this? import Button from 'material-ui/Button' import styled from 'styled-components' const StyledButton = styled(Button)` …
ccd
  • 5,788
  • 10
  • 46
  • 96
60
votes
5 answers

How to extend styled component without passing props to underlying DOM element?

I have a styled component that is extending a third-party component: import Resizable from 're-resizable'; ... const ResizableSC = styled(Resizable)``; export const StyledPaneContainer = ResizableSC.extend` flex: 0 0 ${(props) =>…
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
57
votes
2 answers

Styled-components and react-bootstrap?

Is there a way to use styled-components together with react-bootstrap? react-bootstrap exposes bsClassproperties instead of className for their component which seems to be incompatible with styled-components. Any experiences?
jpfollenius
  • 16,456
  • 10
  • 90
  • 156
55
votes
7 answers

You should not use Route or withRouter() outside a Router when using react-router 4 and styled-component in react

I'm trying to build my first portfolio website and got stuck in routing using react-router-dom 4.2.2 and styled-components 2.2.3. error message: You should not use Route or withRouter() outside a Router I also try using Link instead of NavLink but…
JongHun Lee
  • 551
  • 1
  • 5
  • 4
1
2 3
99 100