Top Related Projects
👩🎤 CSS-in-JS library designed for high performance style composition
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
Documentation about css-modules
:zap: Toolkit for component-oriented styling
Zero-runtime CSS in JS library
Quick Overview
JSS (JavaScript Style Sheets) is a powerful CSS-in-JS library that allows developers to use JavaScript to describe styles. It provides a way to dynamically generate CSS, enabling the creation of reusable, maintainable, and scalable styling solutions for web applications.
Pros
- Offers a high level of flexibility and customization for styling
- Provides better performance through runtime optimization and code splitting
- Enables easy theming and dynamic style changes
- Integrates well with popular frameworks like React
Cons
- Steeper learning curve compared to traditional CSS
- Requires additional setup and tooling
- May lead to larger bundle sizes if not optimized properly
- Some developers prefer the separation of concerns that traditional CSS provides
Code Examples
- Basic usage:
import jss from 'jss'
import preset from 'jss-preset-default'
jss.setup(preset())
const styles = {
myButton: {
color: 'green',
margin: {
top: 5,
right: 0,
bottom: 0,
left: '1rem'
},
'& span': {
fontWeight: 'bold'
}
}
}
const { classes } = jss.createStyleSheet(styles).attach()
document.body.innerHTML = `
<button class="${classes.myButton}">
<span>Click me!</span>
</button>
`
- Using dynamic values:
import jss from 'jss'
const styles = {
myComponent: {
fontSize: props => props.big ? '20px' : '12px'
}
}
const sheet = jss.createStyleSheet(styles)
const className = sheet.classes.myComponent
sheet.update({ big: true })
- Creating a theme:
import jss from 'jss'
import preset from 'jss-preset-default'
jss.setup(preset())
const theme = {
primaryColor: 'blue',
secondaryColor: 'red'
}
const styles = {
myElement: {
color: theme => theme.primaryColor,
backgroundColor: theme => theme.secondaryColor
}
}
const sheet = jss.createStyleSheet(styles, { theme })
sheet.attach()
Getting Started
To start using JSS in your project:
-
Install JSS and its default preset:
npm install jss jss-preset-default
-
Set up JSS with the default preset:
import jss from 'jss' import preset from 'jss-preset-default' jss.setup(preset())
-
Create and use a style sheet:
const styles = { myButton: { background: 'blue', color: 'white' } } const { classes } = jss.createStyleSheet(styles).attach()
-
Apply the generated class to your HTML:
document.body.innerHTML = `<button class="${classes.myButton}">Click me</button>`
Competitor Comparisons
👩🎤 CSS-in-JS library designed for high performance style composition
Pros of emotion
- Simpler API and easier learning curve
- Better performance due to optimized runtime
- Stronger TypeScript support out of the box
Cons of emotion
- Less flexible than JSS for complex use cases
- Smaller ecosystem and fewer plugins available
- Limited server-side rendering capabilities compared to JSS
Code Comparison
emotion:
import { css } from '@emotion/react'
const style = css`
color: hotpink;
`
const SomeComponent = ({ children }) => (
<div css={style}>{children}</div>
)
JSS:
import { createUseStyles } from 'react-jss'
const useStyles = createUseStyles({
myButton: {
color: 'hotpink',
},
})
const SomeComponent = ({ children }) => {
const classes = useStyles()
return <div className={classes.myButton}>{children}</div>
}
Both emotion and JSS are popular CSS-in-JS libraries, each with its own strengths. emotion offers a more straightforward API and better performance, making it easier for beginners to adopt. It also provides robust TypeScript support. However, JSS offers more flexibility for complex styling scenarios and has a larger ecosystem with more plugins. JSS also has better server-side rendering capabilities. The code comparison shows the difference in syntax between the two libraries, with emotion using a tagged template literal approach and JSS using a more object-oriented style.
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
Pros of styled-components
- More intuitive syntax for React developers, using tagged template literals
- Better integration with React components, including prop-based styling
- Automatic critical CSS extraction and vendor prefixing
Cons of styled-components
- Larger bundle size compared to JSS
- Steeper learning curve for developers new to CSS-in-JS
- Limited support for global styles and keyframes
Code Comparison
styled-components:
const Button = styled.button`
background-color: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'blue'};
padding: 10px 20px;
`;
JSS:
const styles = {
button: {
backgroundColor: props => props.primary ? 'blue' : 'white',
color: props => props.primary ? 'white' : 'blue',
padding: '10px 20px',
},
};
Both styled-components and JSS are popular CSS-in-JS solutions, each with its own strengths. styled-components offers a more React-centric approach with its component-based styling, while JSS provides a more flexible and lightweight option. The choice between the two often depends on project requirements, team preferences, and performance considerations.
Documentation about css-modules
Pros of CSS Modules
- Simpler syntax, closer to traditional CSS
- Better compatibility with existing CSS tooling and workflows
- Easier to integrate into existing projects without major refactoring
Cons of CSS Modules
- Less dynamic, can't easily change styles based on props or state
- Limited runtime manipulation of styles
- Requires additional build step or tooling for full functionality
Code Comparison
CSS Modules:
.button {
background: blue;
color: white;
}
import styles from './Button.css';
const Button = () => <button className={styles.button}>Click me</button>;
JSS:
const styles = {
button: {
background: 'blue',
color: 'white',
},
};
const Button = () => <button className={classes.button}>Click me</button>;
CSS Modules offers a more familiar CSS-like syntax and easier integration with existing projects, but lacks the dynamic capabilities of JSS. JSS provides more flexibility for runtime style manipulation and prop-based styling but requires learning a new syntax and may be overkill for simpler projects. The choice between the two often depends on project requirements and team preferences.
:zap: Toolkit for component-oriented styling
Pros of Styletron
- Better performance due to atomic CSS generation and deduplication
- Smaller CSS output size, especially for large applications
- Built-in support for server-side rendering
Cons of Styletron
- Less intuitive syntax compared to JSS's object-based approach
- Limited support for global styles and keyframes
- Steeper learning curve for developers familiar with traditional CSS
Code Comparison
JSS:
const styles = {
button: {
backgroundColor: 'blue',
color: 'white',
padding: '10px 20px',
'&:hover': {
backgroundColor: 'darkblue'
}
}
};
Styletron:
const button = styletron.useStyletron({
backgroundColor: 'blue',
color: 'white',
padding: '10px 20px',
':hover': {
backgroundColor: 'darkblue'
}
});
Both JSS and Styletron are CSS-in-JS solutions, but they differ in their approach and features. JSS offers a more familiar object-based syntax and better support for global styles, while Styletron focuses on performance and atomic CSS generation. The choice between the two depends on project requirements, team preferences, and performance considerations.
Zero-runtime CSS in JS library
Pros of Linaria
- Zero runtime CSS-in-JS with static extraction
- Better performance due to compile-time CSS generation
- Supports critical CSS extraction for improved loading times
Cons of Linaria
- Less dynamic styling capabilities compared to JSS
- Steeper learning curve for developers used to traditional CSS
- Limited runtime style modifications
Code Comparison
Linaria:
import { css } from 'linaria';
const button = css`
background: blue;
color: white;
`;
JSS:
import { createUseStyles } from 'react-jss';
const useStyles = createUseStyles({
button: {
background: 'blue',
color: 'white',
},
});
Linaria focuses on static CSS extraction at build time, resulting in zero runtime overhead and improved performance. It generates plain CSS files, which can be beneficial for SEO and initial page load times. However, this approach limits dynamic styling capabilities compared to JSS.
JSS, on the other hand, offers more flexibility for runtime style modifications and dynamic theming. It has a lower learning curve for developers familiar with CSS-in-JS concepts but may have a slight performance overhead due to its runtime nature.
Both libraries provide solutions for writing CSS in JavaScript, but they cater to different use cases and development preferences. Linaria is ideal for projects prioritizing performance and static styling, while JSS is better suited for applications requiring more dynamic and flexible styling options.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Status
This project is no longer maintained. I am working on a new tool called Webstudio. It allows you to visually build and generate optimized CSS and React components along with a Remix app. Similar to Webflow, but open-source and goes beyond marketing sites.
About
A lib for generating Style Sheets with JavaScript.
For documentation see our docs.
Community
- Join our Discord
- Use Discussions to discuss ideas or ask for help
Backers
Support us with a monthly donation and help us continue our activities. [Become a backer]
Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]
Supporters
Thanks to BrowserStack for providing the infrastructure that allows us to run our tests in real browsers and to all awesome contributors.
Top Related Projects
👩🎤 CSS-in-JS library designed for high performance style composition
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
Documentation about css-modules
:zap: Toolkit for component-oriented styling
Zero-runtime CSS in JS library
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot