@carbon/element-styles

中文文档
0.4.4发布于 2026/11/05Apache-2.0ES Module构建来源已验证近 30 天下载 1848

An experimental styling system for native HTML elements, relying on semantic, attribute-focused selectors instead of class names.

原始使用文档
文档目录15 节
  1. Getting started
  2. Installation
  3. Importing individual elements
  4. Using prebuilts
  5. Emit Carbon tokens
  6. Configure layout options
  7. Density
  8. Mode
  9. Size
  10. Guiding principles
  11. 1. Platform proximity
  12. 2. Logical layouts
  13. 3. Feature foresight
  14. License
  15. IBM Telemetry

@carbon/element-styles

@carbon/element-styles is an experimental styling system for native HTML elements, relying on semantic, attribute-focused selectors instead of class names. There is no 1:1 parity between Carbon element styles and the core React and Web Component implementations of the Carbon Design System. It's use case is targeted more towards simple web pages and editorial content such as styling markdown documents.

Getting started

Installation

npm i @carbon/element-styles

CDN

You can also use any of the prebuilt stylesheets via our CDN:

<!-- specific version (recommended) -->
<link
  rel="stylesheet"
  href="https://1.www.s81c.com/common/carbon/element-styles/version/v[x.y.z]/prebuilt/expressive.css"
/>

<!-- "latest" tag -->
<link
  rel="stylesheet"
  href="https://1.www.s81c.com/common/carbon/element-styles/tag/v0/latest/prebuilt/expressive.css"
/>

<!-- "next" tag -->
<link
  rel="stylesheet"
  href="https://1.www.s81c.com/common/carbon/element-styles/tag/v0/next/prebuilt/expressive.css"
/>

Importing individual elements

To make use of individual elements, import them via @use and include their styles mixin in your Sass stylesheets:

@use "@carbon/element-styles/scss/elements/button";

@include button.styles;

Emitting on custom selectors

Every element has a default selector on which it's styles are emitted. You can configure this selector, but need to make sure the underlying HTML tag and semantics are kept.

The button element for example expects to be emitted on a <button> tag. By default, it's selector is simply button. If you only want to emit these styles on buttons that are explicitly of type button, you can do so via the with syntax of Sass or each time you include the mixin.

@use "@carbon/element-styles/scss/elements/button" with (
  $config: (
    selector: 'button[type="button"]',
  )
);

@include button.styles;
@use "@carbon/element-styles/scss/elements/button";

@include button.styles(
  (
    selector: 'button[type="button"]',
  )
);

Configuring elements

Some elements have additional configuration options to adjust their visual appearance. You can configure these options the same way as you can configure the selector: via the with syntax of Sass or each time you include the mixin:

@use "@carbon/element-styles/scss/elements/button" with (
  $config: (
    kind: "ghost",
  )
);

@include button.styles;
@use "@carbon/element-styles/scss/elements/button";

@include button.styles(
  (
    kind: "ghost",
  )
);

Combining these options with custom selectors means you can emit multiple variants of the same element depending on their context:

@use "@carbon/element-styles/scss/elements/button";

@include button.styles;
@include button.styles(
  (
    selector: 'button[type="submit"]',
    kind: "primary",
  )
);

Specificity

All emitted element styles are wrapped in a CSS cascade layer (@layer carbon-element-styles) to ensure lower specificity and allow easy overrides without high-specificity selectors.

If necessary, you can configure the layer name through the config module:

@use "@carbon/element-styles/scss/config" with (
  $cascade-layer-name: "custom-layer-name"
);
@use "@carbon/element-styles/scss/elements/button";

@include button.styles;

/**
 * ↪ @layer custom-layer-name {
 *     button { … }
 *   }
 */

Using prebuilts

This library offers several prebuilt (opinionated) stylesheets you can use out-of-the-box:

Productive

Includes all available elements with their default selectors. Uses a productive style with medium sized elements. Intended for interaction-heavy pages.

SCSS /scss/prebuilt/productive/index.scss
CSS /css/prebuilt/productive.css
CDN https://1.www.s81c.com/common/carbon/element-styles/tag/v0/latest/prebuilt/productive.css

Expressive

Includes all available elements with their default selectors. Uses an expressive style with large sized elements. Intended for content-heavy pages such as marketing or documentation.

SCSS /scss/prebuilt/expressive/index.scss
CSS /css/prebuilt/expressive.css
CDN https://1.www.s81c.com/common/carbon/element-styles/tag/v0/latest/prebuilt/expressive.css

Editorial

Only includes non-interactive elements with their default selectors. Uses an expressive style with large elements. Intended for content-only pages such as blogs and styling raw markdown.

SCSS /scss/prebuilt/editorial/index.scss
CSS /css/prebuilt/editorial.css
CDN https://1.www.s81c.com/common/carbon/element-styles/tag/v0/latest/prebuilt/editorial.css

Emit Carbon tokens

@carbon/element-styles relies on the tokens defined by @carbon/styles. Therefore, it's not meant to be used standalone but in a container that has emitted a Carbon theme.

Important: in order for button styles to work, you must emit the Carbon's component tokens for buttons. If you're using @carbon/styles that's likely already the case. For convenience, this library also provides an emit-carbon-tokens mixin which includes the necessary component tokens.

@use "@carbon/element-styles";

:root {
  @include element-styles.emit-carbon-tokens("white");
}

To also generate CSS custom properties of all available colors, use the emit-carbon-colors mixin:

@use "@carbon/element-styles";

:root {
  @include element-styles.emit-carbon-colors;
}

/**
 * ↪ :root {
 *     --cds-black-100: #000000;
 *     --cds-blue-10: #edf5ff;
 *     --cds-blue-20: #d0e2ff;
 *     …
 *   }
 */

Configure layout options

All elements are built on contextual layout tokens that define properties like size and density. You can specify these layout tokens for individual parts of your page.

Density

Density

Controls the density of elements, mostly through inline padding.

Supported values

  • condensed
  • normal (default)

Emitted tokens

  • $density--padding

Example

@use "@carbon/element-styles/scss/layout";

:root {
  @include layout.density("condensed");
}

.callout {
  padding: layout.$density--padding;
}
Mode

Mode

Controls the overall visual impression through font sizes and spacings.

Supported values

  • productive (default)
  • expressive

Emitted tokens

  • $mode--max-inline-size
  • $mode--margin-block
  • $mode--transition-duration
  • $mode--transition-timing-function

Available mixins

  • mode--body
  • mode--body-compact
  • mode--heading
  • mode--heading-compact
  • mode--label
  • mode--helper-text
  • mode--code
  • mode--quotation
  • mode--heading-level-1
  • mode--heading-level-2
  • mode--heading-level-3
  • mode--heading-level-4
  • mode--heading-level-5
  • mode--heading-level-6

Example

@use "@carbon/element-styles/scss/layout";

:root {
  @include layout.mode("expressive");
}

.callout {
  margin-block: layout.$mode--margin-block;
}

.callout header {
  @include layout.mode--heading-compact;
}
Size

Size

Controls the height of elements.

Supported values

  • xs
  • sm
  • md (default)
  • lg
  • xl

Emitted tokens

  • $size--block-size
  • $size--padding-block-start
  • $size--padding-block-end

Example

@use "@carbon/element-styles/scss/layout";

:root {
  @include layout.size("lg");
}

.callout header {
  block-size: layout.$size--block-size;
}

Guiding principles

1. Platform proximity

The goal of this library is to provide opinionated styles for native HTML elements and ARIA patterns. Several components from the core Carbon libraries are intentionally missing from this project due to a lack of matching native counterparts. The intent is to not rely on class names or special attributes.

However, there is one exception to this: the tile element. Tiles are one of the most fundamental and widely used components in Carbon. The default selector for tile styles is [data-tile].

2. Logical layouts

Layout decisions that should be consistent across multiple elements are not the responsibility of those elements. Rather, their parent layout (page or zone) should control aspects like the size, density, and overall apperance.

3. Feature foresight

Not every element in this library is fully compatible with all major browsers yet. This project intentionally uses some of the latest features available to some browsers in order to validate whether they are suitable to support current and upcoming concepts of the Carbon Design System.

License

Licensed under the Apache 2.0 license.

IBM Telemetry IBM Telemetry

This package uses IBM Telemetry to collect de-identified and anonymized metrics data. By installing this package as a dependency you are agreeing to telemetry collection. To opt out, see Opting out of IBM Telemetry data collection. For more information on the data being collected, please see the IBM Telemetry documentation.

版本与发布记录

@carbon/element-styles 共发布 60 个版本,首次发布于 2026/03/25;过去 12 个月发布了 60 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.4latest2026/11/054 天前
0.4.4-rc.0next2026/11/045 天前
0.4.32026/10/2911 天前
0.4.3-rc.02026/10/2812 天前
0.4.22026/10/2218 天前
0.4.2-rc.02026/10/2119 天前

关于 @carbon/element-styles 的常见问题

@carbon/element-styles 是什么?

An experimental styling system for native HTML elements, relying on semantic, attribute-focused selectors instead of class names.

如何安装 @carbon/element-styles?

在项目目录运行 npm install @carbon/element-styles,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@carbon/styles(^1.115.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@carbon/element-styles 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/carbon__element-styles 是否存在。

@carbon/element-styles 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@carbon/element-styles 可以商用吗?

@carbon/element-styles 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@carbon/element-styles 的下载量有多少?

根据 npm 官方下载统计,@carbon/element-styles 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,848 次,日均约 62 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carbon/element-styles 还在维护吗?

最新版本 0.4.4 发布于 2026/11/05(4 天前);至今共发布 60 个版本,其中过去 12 个月发布了 60 个;首次发布于 2026/03/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。