How to use Styled Components in your React App

Styled Components is a CSS tool that helps you organize your React project. Learn how to use it here!

Nishit Maheta
Nov 11 · 2 min read

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅

This library helps you build small, reusable components responsible for the look of your app.

styled-components allows you to write actual CSS code to style your components. It also removes the mapping between components and styles – using components as a low-level styling construct could not be easier!

styled-components is compatible with both React (for web) and React Native – meaning it's the perfect choice even for truly universal apps! See the documentation about React Native for more information.

Installation

Installing styled-components only takes a single command and you’re ready to roll:

npm install --save styled-components

Getting Started

styled-components utilises tagged template literals to style your components.

Follow this Document to Getting started with Styled Components

Docs

See the documentation at styled-components.com/docs for more information about using styled-components!

Quicklinks to some of the most-visited pages:

Version 5

styled-components v5 is now officially in beta! Try it out in your project today:

npm install styled-components@beta

Notes:

  • Make sure you’re on a hooks-compatible version of react (>= 16.8 for react, react-dom, and react-is)
  • If you use jest-styled-components, upgrade

Example

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;
`;

// Use them like any other React component – except they're styled!
<Wrapper>
<Title>Hello World, this is my first styled component!</Title>
</Wrapper>

This is what you’ll see in your browser:

Image Source

Official GitHub Link: https://github.com/styled-components/styled-components

Official Website Link: https://www.styled-components.com/

I would like to thank you to the creator and Contributors of this Library for a Wonderful job.

JavaScript in Plain English

Learn the web's most important programming language.

Nishit Maheta

Written by

Technology Lead At Mobio Solutions. http://mobiosolutions.com/. Follow me on https://www.linkedin.com/in/nishit-mehta/

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade