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
1
vote
1 answer

TypeError: Cannot destructure property 'theme' of '(0 , react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)' as it is null

I'm working on a Next.js application with TypeScript and I have this error: "TypeError: Cannot destructure property 'theme' of '(0 , react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)' as it is null." This is my _app.tsx file: import { useContext }…
Ton Almeida
  • 173
  • 2
  • 2
  • 9
1
vote
1 answer

React native styled components cannot override component's style

I have a weird problem with stlyed components. I have a component Header with a basic style but when a try to use this component and extend the style nothing happens. Can someone tell me what going on? import styled from…
Vitoo
  • 21
  • 5
1
vote
1 answer

How to style ::selection pseudo element in a styled component?

I want to do that with a styled component: .jTHnFZ ::selection { background-color: pink; } I write this styled component: const MyDiv = styled.div` ::selection { background-color: pink; } ` But the result is: .jTHnFZ::selection…
1
vote
1 answer

my output is not displaying on the browser console

I am creating a form with some handling functions. This is my functions that handle the events. const [name, setUsername] = useState(""); const [age, setAge] = useState(""); const handleNameChange = (event) => { …
ClericGame
  • 63
  • 7
1
vote
1 answer

Uncaught TypeError: can't access property "button", theme.typography is undefined

I'm setting up a react application using MUI, and since we will be using styled components for our own custom styles & containers, I figure I may as well configure MUI with styled-components as per the docs. We will also be using yarn. The…
agmcleod
  • 13,321
  • 13
  • 57
  • 96
1
vote
1 answer

Why are the Styled Component CSS Styles I specified for a larger screen (1440px) overridden those I specified for lower screen(1280px)?

import styled from "styled-components"; import { devices } from "./devices"; export const HeaderContainer = styled.div` padding: 20px; @media ${devices.lg} { padding: 40px 90px; } @media ${devices.xl} { padding: 40px 215px 40px…
1
vote
1 answer

How to remove bottom space of flex-item?

I have the following layout: export const NewsLetterContainer = styled.div` display: flex; flex-direction: row; flex-wrap: wrap; position: absolute; background-color: white; top: 64px; padding: 64px 64px 64px 64px; width: 808px; …
ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
1
vote
1 answer

How to pass in two arguments to a props function in styled components

I have a styled-components in my React component and it currently uses the props feature on styled components. I need to style using another property but I keep getting an error when I try to pass it in. This is what I currently have and this works…
Ire
  • 45
  • 1
  • 6
1
vote
1 answer

Is it a good practice to pass via props functions to styled component file?

I have this code: return ( Some Title ) Do you think it would be a…
1
vote
1 answer

Changing charts component with useState and style of Header

I'am currently learning to code, and build a react-component with four charts, which are changing when the header is clicked. Everything is working fine, but the active header should have the className "open", which i have defined above in the…
Roman
  • 137
  • 1
  • 14
1
vote
1 answer

How do I set a option to selected using styled-components?

How do I use styled-component attributes to set the selected property of my styled option so that the option is selected within the select element depending on whether or not the isSelected property is truthy? Right now I have this: export const…
Liam Pillay
  • 592
  • 1
  • 4
  • 19
1
vote
1 answer

polished function throws error only in test suite, not in actual app/browser

I have a button that utilises the lighten helper from polished (styled-componenents). I invoke it on hover like so: FAB.styles.tsx &:hover, &:focus { * { cursor: pointer; } background: ${(props) => lighten(0.1,…
1
vote
2 answers

using styled components i got stuck while displaying a image. i don't know where things went wrong

here is my code I am trying to display an image on my document but it is not visible: is something wrong with the styled component or I have placed something wrong somewhere this code is from the lama dev React e-commerce video I am practicing that…
1
vote
0 answers

How to reference a styled component to another styled component

I'm trying to change background-color of a styled component, inside another styled-component onHover. I wan't StyledDevider to change background-color when ReferenceInput is hovered. The components are styling antd components. This is my code: const…
Michael
  • 11
  • 2
1
vote
1 answer

How to get rid of border and add background color to MUI TextField variant outlined

I want to customize outlined variant of TextField in Material UI and to below field I want to remove border or give color to white and give different background color. I'm using styled components in my app, but also tried tried makeStyles and it…
marcinb1986
  • 708
  • 1
  • 11
  • 30