DEV Community

Cover image for Style React component with styled-components : Part-1
Shahjada Talukdar
Shahjada Talukdar

Posted on β€’ Edited on

7 3

Style React component with styled-components : Part-1

Styling your React component is an important part for any real world application.
We can style react components in a couple of ways. such as –

  1. inline styling
  2. CSS modules
  3. emotion
  4. styled-components

We will talk about styled-components in this article.

We are gonna create one simple animated loading spinner component.

We can install the package from npmjs using npm or yarn cli.
npm i styled-components --save
Or
yarn add styled-components

We can import that in our Component module like
import styled from "styled-components";

Now I will use the styled API to create the spinner.We are using one DIV as a target for that spinner.

const StyledSpinner = styled.div`
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid #3498db;
  width: 120px;
  height: 120px;
  -webkit-animation: spin 2s linear infinite; /* Safari */
  animation: spin 2s linear infinite;</code>

  @keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
`;
Enter fullscreen mode Exit fullscreen mode

Now we can use this like a react component.

class Spinner extends Component {
  render() {
    return (
      <StyledSpinner />
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

We don’t need any other tool or webpack to build this css. It will work just fine.

I will continue writing more on styled-components.

Update Part 2 is available at
Style React component with styled-components : Part-2

Cheers!
πŸ‘‹

As I am trying to contribute contents on the Web, you can buy me a coffee for my hours spent on all of these ❀️😊🌸
Buy Me A Coffee

PS: You can also have a look on my blog site https://shahjada.me

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay