颜色 · Bootstrap v5.3

分类: 365bet日博 作者: admin 时间: 2026-02-02 18:16:31 阅读: 419
颜色 · Bootstrap v5.3

辅助功能提示:仅使用颜色添加含义会提供视觉指示,但不会传达给屏幕阅读器等辅助技术的用户。请确保含义从内容本身(例如,具有 足够 色彩对比度 的可见文本)中显而易见,或通过其他方式(例如,使用 .visually-hidden 类隐藏的附加文本)包含在内。

颜色

使用颜色实用程序给文本着色。如果你想给链接着色,可以使用 .link-* 帮助类,它们具有 :hover 和 :focus 状态。

从我们的原始 $theme-colors Sass 映射生成的 .text-* 等颜色实用程序目前不会响应颜色模式,但是任何 .text-*-emphasis 实用程序都会响应。这将在 v6 中解决。

.text-primary

.text-primary-emphasis

.text-secondary

.text-secondary-emphasis

.text-success

.text-success-emphasis

.text-danger

.text-danger-emphasis

.text-warning

.text-warning-emphasis

.text-info

.text-info-emphasis

.text-light

.text-light-emphasis

.text-dark

.text-dark-emphasis

.text-body

.text-body-emphasis

.text-body-secondary

.text-body-tertiary

.text-black

.text-white

.text-black-50

.text-white-50

html

.text-primary

.text-primary-emphasis

.text-secondary

.text-secondary-emphasis

.text-success

.text-success-emphasis

.text-danger

.text-danger-emphasis

.text-warning

.text-warning-emphasis

.text-info

.text-info-emphasis

.text-light

.text-light-emphasis

.text-dark

.text-dark-emphasis

.text-body

.text-body-emphasis

.text-body-secondary

.text-body-tertiary

.text-black

.text-white

.text-black-50

.text-white-50

弃用:自 v5.1.0 起,随着添加了 .text-opacity-* 实用程序和文本实用程序的 CSS 变量,.text-black-50 和 .text-white-50 已弃用。它们将在 v6.0.0 中被移除。

弃用:自 v5.3.0 起,随着添加了扩展的主题颜色和变量,.text-muted 实用程序已弃用。它的默认值也已重新分配给新的 --bs-secondary-color CSS 变量,以更好地支持颜色模式。它将在 v6.0.0 中被移除。

不透明度

在 v5.1.0 中添加

从 v5.1.0 起,文本颜色实用程序使用 Sass 和 CSS 变量生成。这允许实时更改颜色,无需编译和动态更改 alpha 透明度。

工作原理

考虑我们的默认 .text-primary 实用程序。

.text-primary {

--bs-text-opacity: 1;

color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important;

}

我们使用 --bs-primary(值为 13, 110, 253)CSS 变量的 RGB 版本,并附加了第二个 CSS 变量 --bs-text-opacity,用于 alpha 透明度(默认值为 1,这要归功于本地 CSS 变量)。这意味着现在每当你使用 .text-primary 时,计算得出的 color 值为 rgba(13, 110, 253, 1)。每个 .text-* 类中的本地 CSS 变量避免了继承问题,因此实用程序的嵌套实例不会自动修改 alpha 透明度。

示例

要更改该不透明度,请通过自定义样式或内联样式覆盖 --bs-text-opacity。

这是默认主要文本

这是 50% 不透明度的主要文本

html

This is default primary text

This is 50% opacity primary text

或者,从任何 .text-opacity 实用程序中进行选择

这是默认主要文本

这是 75% 不透明度的主要文本

这是 50% 不透明度的主要文本

这是 25% 不透明度的主要文本

html

This is default primary text

This is 75% opacity primary text

This is 50% opacity primary text

This is 25% opacity primary text

特殊性

有时由于另一个选择器的特殊性,无法应用上下文类。在某些情况下,一个足够好的解决方法是将元素的内容包装在具有所需类的

或更多语义元素中。

CSS

除了以下 Sass 功能之外,请考虑阅读我们包含的 CSS 自定义属性(又称 CSS 变量)以获取颜色等信息。

Sass 变量

大多数 color 实用程序都是由我们的主题颜色生成的,这些颜色从我们的通用调色板变量中重新分配。

scss/_variables.scss

$blue: #0d6efd;

$indigo: #6610f2;

$purple: #6f42c1;

$pink: #d63384;

$red: #dc3545;

$orange: #fd7e14;

$yellow: #ffc107;

$green: #198754;

$teal: #20c997;

$cyan: #0dcaf0;

scss/_variables.scss

$primary: $blue;

$secondary: $gray-600;

$success: $green;

$info: $cyan;

$warning: $yellow;

$danger: $red;

$light: $gray-100;

$dark: $gray-900;

灰度颜色也可用,但仅使用一个子集来生成任何实用工具。

scss/_variables.scss

$white: #fff;

$gray-100: #f8f9fa;

$gray-200: #e9ecef;

$gray-300: #dee2e6;

$gray-400: #ced4da;

$gray-500: #adb5bd;

$gray-600: #6c757d;

$gray-700: #495057;

$gray-800: #343a40;

$gray-900: #212529;

$black: #000;

scss/_maps.scss

$theme-colors-text: (

"primary": $primary-text-emphasis,

"secondary": $secondary-text-emphasis,

"success": $success-text-emphasis,

"info": $info-text-emphasis,

"warning": $warning-text-emphasis,

"danger": $danger-text-emphasis,

"light": $light-text-emphasis,

"dark": $dark-text-emphasis,

);

在浅色和深色模式下设置 .text-*-emphasis 实用工具中颜色的变量

scss/_variables.scss

$primary-text-emphasis: shade-color($primary, 60%);

$secondary-text-emphasis: shade-color($secondary, 60%);

$success-text-emphasis: shade-color($success, 60%);

$info-text-emphasis: shade-color($info, 60%);

$warning-text-emphasis: shade-color($warning, 60%);

$danger-text-emphasis: shade-color($danger, 60%);

$light-text-emphasis: $gray-700;

$dark-text-emphasis: $gray-700;

scss/_variables-dark.scss

$primary-text-emphasis-dark: tint-color($primary, 40%);

$secondary-text-emphasis-dark: tint-color($secondary, 40%);

$success-text-emphasis-dark: tint-color($success, 40%);

$info-text-emphasis-dark: tint-color($info, 40%);

$warning-text-emphasis-dark: tint-color($warning, 40%);

$danger-text-emphasis-dark: tint-color($danger, 40%);

$light-text-emphasis-dark: $gray-100;

$dark-text-emphasis-dark: $gray-300;

Sass 映射

然后将主题颜色放入 Sass 映射中,以便我们可以循环遍历它们来生成我们的实用工具、组件修改器等。

scss/_variables.scss

$theme-colors: (

"primary": $primary,

"secondary": $secondary,

"success": $success,

"info": $info,

"warning": $warning,

"danger": $danger,

"light": $light,

"dark": $dark

);

灰度颜色也可用作 Sass 映射。此映射不用于生成任何实用工具。

scss/_variables.scss

$grays: (

"100": $gray-100,

"200": $gray-200,

"300": $gray-300,

"400": $gray-400,

"500": $gray-500,

"600": $gray-600,

"700": $gray-700,

"800": $gray-800,

"900": $gray-900

);

RGB 颜色是从单独的 Sass 映射生成的

scss/_maps.scss

$theme-colors-rgb: map-loop($theme-colors, to-rgb, "$value");

颜色不透明度基于它们自己的映射,该映射由实用工具 API 使用

scss/_maps.scss

$utilities-text: map-merge(

$utilities-colors,

(

"black": to-rgb($black),

"white": to-rgb($white),

"body": to-rgb($body-color)

)

);

$utilities-text-colors: map-loop($utilities-text, rgba-css-var, "$key", "text");

$utilities-text-emphasis-colors: (

"primary-emphasis": var(--#{$prefix}primary-text-emphasis),

"secondary-emphasis": var(--#{$prefix}secondary-text-emphasis),

"success-emphasis": var(--#{$prefix}success-text-emphasis),

"info-emphasis": var(--#{$prefix}info-text-emphasis),

"warning-emphasis": var(--#{$prefix}warning-text-emphasis),

"danger-emphasis": var(--#{$prefix}danger-text-emphasis),

"light-emphasis": var(--#{$prefix}light-text-emphasis),

"dark-emphasis": var(--#{$prefix}dark-text-emphasis)

);

颜色模式自适应文本颜色也可用作 Sass 映射

scss/_maps.scss

$theme-colors-text: (

"primary": $primary-text-emphasis,

"secondary": $secondary-text-emphasis,

"success": $success-text-emphasis,

"info": $info-text-emphasis,

"warning": $warning-text-emphasis,

"danger": $danger-text-emphasis,

"light": $light-text-emphasis,

"dark": $dark-text-emphasis,

);

scss/_maps.scss

$theme-colors-text-dark: (

"primary": $primary-text-emphasis-dark,

"secondary": $secondary-text-emphasis-dark,

"success": $success-text-emphasis-dark,

"info": $info-text-emphasis-dark,

"warning": $warning-text-emphasis-dark,

"danger": $danger-text-emphasis-dark,

"light": $light-text-emphasis-dark,

"dark": $dark-text-emphasis-dark,

);

Sass 实用工具 API

颜色实用工具在 scss/_utilities.scss 中的实用工具 API 中声明。了解如何使用实用工具 API。

scss/_utilities.scss

"color": (

property: color,

class: text,

local-vars: (

"text-opacity": 1

),

values: map-merge(

$utilities-text-colors,

(

"muted": var(--#{$prefix}secondary-color), // deprecated

"black-50": rgba($black, .5), // deprecated

"white-50": rgba($white, .5), // deprecated

"body-secondary": var(--#{$prefix}secondary-color),

"body-tertiary": var(--#{$prefix}tertiary-color),

"body-emphasis": var(--#{$prefix}emphasis-color),

"reset": inherit,

)

)

),

"text-opacity": (

css-var: true,

class: text-opacity,

values: (

25: .25,

50: .5,

75: .75,

100: 1

)

),

"text-color": (

property: color,

class: text,

values: $utilities-text-emphasis-colors

),

相关推荐