Styled Components is a popular library for styling React applications using tagged template literals. It allows you to write CSS directly in your JavaScript files, which makes it easier to manage styles that are scoped to components. Styled Components is part of a broader approach known as CSS-in-JS, where CSS is handled within JavaScript rather than in separate CSS files.
Styled Components is a library that utilizes tagged template literals to style React components. It allows you to define styles directly within your JavaScript code, ensuring that styles are scoped and modular.
Installation
Install the library using npm or yarn
npm install styled-components
# or
yarn add styled-components
Creating Styled Components
Use the styled
function to create styled components. These are essentially React components with styles attached.
Example:
import React from 'react';
import styled from 'styled-components';
// Define a styled button component
const Button = styled.button`
background-color: blue;
color: white;
font-size: 16px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
// Apply different styles based on props
${props => props.primary && `
background-color: green;
`}
`;
function App() {
return (
<div>
<Button>Default Button</Button>
<Button primary>Primary Button</Button>
</div>
);
}
export default App;
In this example, the Button
component is styled with a base style and additional style based on the primary
prop.
Dynamic Styling
You can use props to conditionally apply styles.
Example:
const Card = styled.div`
background-color: ${props => props.backgroundColor || 'white'};
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
`;
Theming
Styled Components supports theming, which allows you to define a set of theme values and use them across your application.
Example:
import React from 'react';
import styled, { ThemeProvider } from 'styled-components';
const theme = {
primaryColor: 'blue',
secondaryColor: 'green',
};
const Button = styled.button`
background-color: ${props => props.theme.primaryColor};
color: white;
padding: 10px;
border: none;
border-radius: 5px;
`;
function App() {
return (
<ThemeProvider theme={theme}>
<Button>Styled Button</Button>
</ThemeProvider>
);
}
export default App;
Global Styles
You can define global styles that apply to your entire application.
Example:
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
body {
margin: 0;
font-family: Arial, sans-serif;
}
`;
function App() {
return (
<>
<GlobalStyle />
<div>My App</div>
</>
);
}
export default App;
Styled Components provides a powerful way to manage component-level styling in React applications. By using tagged template literals, it allows for scoped, dynamic, and themeable styles. It integrates well with React, offering features like dynamic styling, theming, and global styles, making it a popular choice for many React developers.
@aCodeTutorials All Rights Reserved
privacy policy | about