返回介绍

Background Color

发布于 2024-07-30 22:16:04 字数 126890 浏览 0 评论 0 收藏 0

Utilities for controlling an element's background color.

Quick reference

ClassPreview 
bg-inherit
bg-current
bg-transparent
bg-black
bg-white
bg-slate-50
bg-slate-100
bg-slate-200
bg-slate-300
bg-slate-400
bg-slate-500
bg-slate-600
bg-slate-700
bg-slate-800
bg-slate-900
bg-slate-950
bg-gray-50
bg-gray-100
bg-gray-200
bg-gray-300
bg-gray-400
bg-gray-500
bg-gray-600
bg-gray-700
bg-gray-800
bg-gray-900
bg-gray-950
bg-zinc-50
bg-zinc-100
bg-zinc-200
bg-zinc-300
bg-zinc-400
bg-zinc-500
bg-zinc-600
bg-zinc-700
bg-zinc-800
bg-zinc-900
bg-zinc-950
bg-neutral-50
bg-neutral-100
bg-neutral-200
bg-neutral-300
bg-neutral-400
bg-neutral-500
bg-neutral-600
bg-neutral-700
bg-neutral-800
bg-neutral-900
bg-neutral-950
bg-stone-50
bg-stone-100
bg-stone-200
bg-stone-300
bg-stone-400
bg-stone-500
bg-stone-600
bg-stone-700
bg-stone-800
bg-stone-900
bg-stone-950
bg-red-50
bg-red-100
bg-red-200
bg-red-300
bg-red-400
bg-red-500
bg-red-600
bg-red-700
bg-red-800
bg-red-900
bg-red-950
bg-orange-50
bg-orange-100
bg-orange-200
bg-orange-300
bg-orange-400
bg-orange-500
bg-orange-600
bg-orange-700
bg-orange-800
bg-orange-900
bg-orange-950
bg-amber-50
bg-amber-100
bg-amber-200
bg-amber-300
bg-amber-400
bg-amber-500
bg-amber-600
bg-amber-700
bg-amber-800
bg-amber-900
bg-amber-950
bg-yellow-50
bg-yellow-100
bg-yellow-200
bg-yellow-300
bg-yellow-400
bg-yellow-500
bg-yellow-600
bg-yellow-700
bg-yellow-800
bg-yellow-900
bg-yellow-950
bg-lime-50
bg-lime-100
bg-lime-200
bg-lime-300
bg-lime-400
bg-lime-500
bg-lime-600
bg-lime-700
bg-lime-800
bg-lime-900
bg-lime-950
bg-green-50
bg-green-100
bg-green-200
bg-green-300
bg-green-400
bg-green-500
bg-green-600
bg-green-700
bg-green-800
bg-green-900
bg-green-950
bg-emerald-50
bg-emerald-100
bg-emerald-200
bg-emerald-300
bg-emerald-400
bg-emerald-500
bg-emerald-600
bg-emerald-700
bg-emerald-800
bg-emerald-900
bg-emerald-950
bg-teal-50
bg-teal-100
bg-teal-200
bg-teal-300
bg-teal-400
bg-teal-500
bg-teal-600
bg-teal-700
bg-teal-800
bg-teal-900
bg-teal-950
bg-cyan-50
bg-cyan-100
bg-cyan-200
bg-cyan-300
bg-cyan-400
bg-cyan-500
bg-cyan-600
bg-cyan-700
bg-cyan-800
bg-cyan-900
bg-cyan-950
bg-sky-50
bg-sky-100
bg-sky-200
bg-sky-300
bg-sky-400
bg-sky-500
bg-sky-600
bg-sky-700
bg-sky-800
bg-sky-900
bg-sky-950
bg-blue-50
bg-blue-100
bg-blue-200
bg-blue-300
bg-blue-400
bg-blue-500
bg-blue-600
bg-blue-700
bg-blue-800
bg-blue-900
bg-blue-950
bg-indigo-50
bg-indigo-100
bg-indigo-200
bg-indigo-300
bg-indigo-400
bg-indigo-500
bg-indigo-600
bg-indigo-700
bg-indigo-800
bg-indigo-900
bg-indigo-950
bg-violet-50
bg-violet-100
bg-violet-200
bg-violet-300
bg-violet-400
bg-violet-500
bg-violet-600
bg-violet-700
bg-violet-800
bg-violet-900
bg-violet-950
bg-purple-50
bg-purple-100
bg-purple-200
bg-purple-300
bg-purple-400
bg-purple-500
bg-purple-600
bg-purple-700
bg-purple-800
bg-purple-900
bg-purple-950
bg-fuchsia-50
bg-fuchsia-100
bg-fuchsia-200
bg-fuchsia-300
bg-fuchsia-400
bg-fuchsia-500
bg-fuchsia-600
bg-fuchsia-700
bg-fuchsia-800
bg-fuchsia-900
bg-fuchsia-950
bg-pink-50
bg-pink-100
bg-pink-200
bg-pink-300
bg-pink-400
bg-pink-500
bg-pink-600
bg-pink-700
bg-pink-800
bg-pink-900
bg-pink-950
bg-rose-50
bg-rose-100
bg-rose-200
bg-rose-300
bg-rose-400
bg-rose-500
bg-rose-600
bg-rose-700
bg-rose-800
bg-rose-900
bg-rose-950

Basic usage

Setting the background color

Use utilities like bg-white, bg-indigo-500 and bg-transparent to control the background color of an element.

<button class="bg-indigo-500 ...">
  Save changes
</button>

Changing the opacity

Use the color opacity modifier to control the opacity of an element’s background color.

bg-sky-500

bg-sky-500/75

bg-sky-500/50

<button class="bg-sky-500/100 ..."></button>
<button class="bg-sky-500/75 ..."></button>
<button class="bg-sky-500/50 ..."></button>

You can use any value defined in your opacity scale, or use arbitrary values if you need to deviate from your design tokens.

<div class="bg-sky-500/[.06] ..."></div>

Applying conditionally

Hover, focus, and other states

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:bg-cyan-600 to only apply the bg-cyan-600 utility on hover.

Try hovering over the button to see the background color change

<button class="bg-cyan-500 hover:bg-cyan-600 ...">Subscribe</button>

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

Breakpoints and media queries

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:bg-green-500 to apply the bg-green-500 utility at only medium screen sizes and above.

<button class="bg-blue-500 md:bg-green-500 ...">Subscribe</button>

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.


Using custom values

Customizing your theme

By default, Tailwind makes the entire default color palette available as background colors. You can customize your color palette by editing theme.colors or theme.extend.colors in your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        'regal-blue': '#243c5a',
      },
    }
  }
}

Alternatively, you can customize just your background colors by editing theme.backgroundColor or theme.extend.backgroundColor in your tailwind.config.js file.

Learn more about customizing the default theme in the theme customization documentation.

Arbitrary values

If you need to use a one-off color value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

<p class="bg-[#50d71e]">
  <!-- ... -->
</p>

Learn more about arbitrary value support in the arbitrary values documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文