styled-components vs emotion vs glamorous vs styled-jsx
CSS-in-JS Libraries
styled-componentsemotionglamorousstyled-jsx类似的npm包:

CSS-in-JS Libraries

CSS-in-JS库允许开发者在JavaScript中直接编写CSS样式,提供了更好的组件化和动态样式管理。这些库通常与React等现代前端框架紧密集成,支持样式的动态生成和主题化,增强了开发者的灵活性和效率。它们解决了传统CSS的作用域问题,使样式更易于维护和重用。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
styled-components8,899,79541,0321.84 MB3368 天前MIT
emotion761,393---5 年前MIT
glamorous03,626-07 年前MIT
styled-jsx07,7941.03 MB8310 个月前MIT

功能对比: styled-components vs emotion vs glamorous vs styled-jsx

性能

  • styled-components:

    Styled-components通过将样式与组件绑定来提高性能,避免了样式冲突。它支持服务器端渲染,确保在初始加载时样式的快速应用。

  • emotion:

    Emotion通过优化样式生成和使用CSS类名的方式,提供了卓越的性能。它支持静态和动态样式,能够根据需要生成最小的CSS。

  • glamorous:

    Glamorous的性能相对较好,但在处理大量动态样式时可能会受到影响。它的设计旨在简化样式的创建,但在复杂应用中可能不如Emotion高效。

  • styled-jsx:

    Styled-jsx的性能表现良好,特别是在Next.js中。它通过局部样式的方式减少了全局样式的冲突,但在大型应用中可能需要额外的优化。

样式管理

  • styled-components:

    Styled-components通过标签模板字面量提供了一种直观的样式管理方式,支持全局样式和主题,适合大型应用和复杂的样式需求。

  • emotion:

    Emotion允许开发者使用对象语法或标签模板字面量来定义样式,提供了极大的灵活性。它支持主题化和动态样式,适合复杂的UI需求。

  • glamorous:

    Glamorous提供了一种简单的方式来创建可复用的样式组件,支持样式的继承和组合,适合快速开发和小型项目。

  • styled-jsx:

    Styled-jsx专注于局部样式的管理,允许开发者在组件内定义样式,避免了样式冲突,适合中小型项目。

学习曲线

  • styled-components:

    Styled-components的学习曲线适中,虽然其功能强大,但需要一定的时间来掌握标签模板字面量的使用。

  • emotion:

    Emotion的API相对简单,易于上手,特别是对于熟悉JavaScript的开发者。它的灵活性和功能丰富性使得学习过程较为顺畅。

  • glamorous:

    Glamorous的学习曲线较为平缓,适合初学者。其简单的API设计使得开发者能够快速上手并创建样式。

  • styled-jsx:

    Styled-jsx的学习曲线较低,特别适合已经熟悉React的开发者。其局部样式的概念易于理解和应用。

社区支持

  • styled-components:

    Styled-components是最受欢迎的CSS-in-JS库之一,拥有庞大的社区和丰富的资源,开发者可以轻松找到帮助和示例。

  • emotion:

    Emotion拥有活跃的社区和良好的文档支持,开发者可以很容易找到解决方案和最佳实践。

  • glamorous:

    Glamorous的社区相对较小,虽然功能简单,但在大型项目中可能缺乏足够的支持和资源。

  • styled-jsx:

    Styled-jsx在Next.js社区中得到了广泛支持,文档清晰,适合使用Next.js的开发者。

扩展性

  • styled-components:

    Styled-components支持主题和全局样式的扩展,适合大型应用的样式管理。

  • emotion:

    Emotion具有很高的扩展性,支持插件和主题功能,适合需要定制化样式的复杂应用。

  • glamorous:

    Glamorous的扩展性较弱,主要集中在基础样式的创建,适合小型项目。

  • styled-jsx:

    Styled-jsx的扩展性有限,主要用于局部样式,适合中小型项目。

如何选择: styled-components vs emotion vs glamorous vs styled-jsx

  • styled-components:

    选择Styled-components如果你喜欢使用标签模板字面量来定义样式,并希望利用其强大的主题和全局样式功能。它是CSS-in-JS库中最流行的选择,拥有广泛的社区支持。

  • emotion:

    选择Emotion如果你需要高性能的CSS-in-JS解决方案,支持强大的主题功能和动态样式。Emotion提供了灵活的API,适合需要复杂样式的应用。

  • glamorous:

    选择Glamorous如果你希望使用简单的API来创建可复用的样式组件,且不需要过多的配置。Glamorous专注于简化样式的创建和管理。

  • styled-jsx:

    选择Styled-jsx如果你在使用Next.js等框架,并希望轻松实现局部样式。Styled-jsx是为React设计的,支持CSS模块化和动态样式。

styled-components的README


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

downloads: 600k/month gzip size module formats: umd, cjs, esm Code Coverage

Upgrading from v5? See the migration guide.

Utilizing tagged template literals (a recent addition to JavaScript) and the power of CSS, styled-components allow 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!

const Button = styled.button`
  color: grey;
`;

Alternatively, you may use style objects. This allows for easy porting of CSS from inline styles, while still supporting the more advanced styled-components capabilities like component selectors and media queries.

const Button = styled.button({
  color: 'grey',
});

Equivalent to:

const Button = styled.button`
  color: grey;
`;

styled-components is compatible with both React (for web) and React Native – meaning it's the perfect choice even for truly universal apps! It also supports React Server Components (RSC) through automatic runtime detection. See the documentation about React Native for more information.

Supported by Front End Center. Thank you for making this possible!


Docs

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

Quicklinks to some of the most-visited pages:


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

function MyUI() {
  return (
    // 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:


Looking for v5?

The main branch is for the most-current version of styled-components, currently v6. For changes targeting v5, please point your PRs at the legacy-v5 branch.


Built with styled-components

A lot of hard work goes into community libraries, projects, and guides. A lot of them make it easier to get started or help you with your next project! There are also a whole lot of interesting apps and sites that people have built using styled-components.

Make sure to head over to awesome-styled-components to see them all! And please contribute and add your own work to the list so others can find it.


Contributing

If you want to contribute to styled-components please see our contributing and community guidelines, they'll help you get set up locally and explain the whole process.

Please also note that all repositories under the styled-components organization follow our Code of Conduct, make sure to review and follow it.


Badge

Let everyone know you're using styled-componentsstyle: styled-components

[![style: styled-components](https://img.shields.io/badge/style-%F0%9F%92%85%20styled--components-orange.svg?colorB=daa357&colorA=db748e)](https://github.com/styled-components/styled-components)

Contributors

This project exists thanks to all the people who contribute. [Contribute].


Backers

Thank you to all our backers! 🙏 [Become a backer]


Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]


License

Licensed under the MIT License, Copyright © 2016-present Glen Maddern and Maximilian Stoiber.

See LICENSE for more information.


Acknowledgements

This project builds on a long line of earlier work by clever folks all around the world. We'd like to thank Charlie Somerville, Nik Graf, Sunil Pai, Michael Chan, Andrey Popp, Jed Watson & Andrey Sitnik who contributed ideas, code or inspiration.

Special thanks to @okonet for the fantastic logo.