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

Overlap Button on Buttons using Styled Components

I have here a problem on overlapping the button on another button. I wanted to follow the pic I've attached below Codesandbox CLICK HERE const MainButton = styled.button` border-radius: 50%; border: 2px solid red; position: relative; …
Joseph
  • 7,042
  • 23
  • 83
  • 181
1
vote
1 answer

How to create a border radius based off props in react native

I have a tick icon and I'm trying to create a circle background fill in it. How do I correctly create a border radius which is half the size of the svg icon? I have tried using border radius: 50% which doesnt work in React Native, and also tried to…
walker1
  • 341
  • 1
  • 5
  • 18
1
vote
0 answers

Next Js Form Values Disappear

I have a form that takes data as well as images. Everything works fine till you leave the tab and come back or on mobile, when you try uploading images it opens a folder on full screen where you can choose. Once back to the form it is empty. I…
Laspeed
  • 791
  • 1
  • 5
  • 27
1
vote
2 answers

I want to reuse certain CSS properties in Emotion/styled component

import styled from '@emotion/styled'; type ColorProps = { Coloured: boolean, } const BoxStyled = styled.div` ${(props:ColorProps) => props.Coloured ? { background: "#304f8f", border: "1.85px solid #304f8f", color:…
mason
  • 153
  • 2
  • 9
1
vote
0 answers

How to position all elements of Table Header in one line using React Table

In the project we use React Table to render table data. I need to fix a bug and need to achieve the result that all elements in React Table column header are in one line. So far title and arrow for sorting are in one line, but filtering icon is…
marcinb1986
  • 708
  • 1
  • 11
  • 30
1
vote
1 answer

What should be the type of props when styling another Styled Component?

I am using styled components along with typescript in my NextJS project. I have styled a div element as following: export const ClippedOverlay = styled( ( props: React.DetailedHTMLProps< React.HTMLAttributes, …
1
vote
1 answer

Styled Components with more readable class names

I created few components using styled components and their output generates a hashed class name that makes debugging harder. const StyledMessageBlock = styled.div` display: flex; font-weight: 200; height: 25px; `; So, I tried to import…
vbotio
  • 1,566
  • 3
  • 26
  • 53
1
vote
2 answers

How do I get the state of the parent component in a child styled component using tagged template literals?

I have a react component App as following, it has a state WHRatio, and my div component will use the WHRatio value to calculate the height value. In my following way, it can work, it can get the parent component state WHRatio successfully. import…
Huan
  • 208
  • 2
  • 8
1
vote
0 answers

Styled Components and Material Ui

Can I use Styled Components and Material Ui at the same time for the same react app that I'm building for the front end? will there be any problems that I will face during the development phase?
Amir Mazed
  • 11
  • 1
1
vote
1 answer

Typechecking React library with Styled Components

I'm currently working in a design system library to test some things. Basically, the library is a Styled Component wrapper in order to create themes. I built this library with Typescript, React, Styled Components and Rollup as bundler. The problem…
1
vote
1 answer

Passing a React.MutableRefObject to sibling elements

I'm sure I'm explaining this wrong but I'm trying to pass a reference to an div that may or may not be there and read that reference in a sibling component. Let me explain, (test code in it's entirety at the end): I define a Overlay component that…
JakeB
  • 153
  • 2
  • 9
1
vote
2 answers

MUI 5 using styled components with child component props

I'm converting from MUI 4 to 5. Working on converting from makeStyles() to styled components. Is there an example somewhere using the styled() method with a component that has child component props? For example, has…
Kizmar
  • 2,465
  • 3
  • 20
  • 27
1
vote
1 answer

Using ThemeProvider in separate style sheets

I've been attempting to integrate the ThemeProvider from styled-components into an existing project. But run into the error below when accessing the theme's items in a style sheet: TypeError: Cannot read properties of undefined (reading…
Ivaldir
  • 185
  • 12
1
vote
1 answer

Using Styled-Components `.attrs` with TypeScript (Props don't merge)

When I do this: const Button = styled.button.attrs((props:ButtonProps) => ({ primary: props.buttonType === 'primary', secondary: props.buttonType === 'secondary', critical: props.buttonType === 'critical', small: props.buttonSize ===…
1
vote
3 answers

React JS Access id_lang variable outside of scope?

How can I access the let number whilst it's outside of the render function to pass down to check if const Navbar = () => { const getID = async (id) => { let id_lang = id; console.log(id_lang); } return (
1 2 3
99
100