0Pricing
Tailwind CSS Academy · 课时

盒阴影

使用 shadow-sm 到 shadow-2xl 增加层次感,使用 shadow-inner 创建内嵌效果,并使用 shadow-none 移除阴影。

盒阴影 是 CoddyKit 上的免费 Tailwind CSS Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Tailwind CSS Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Tailwind CSS Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Shadows and Visual Depth

Box shadows create a sense of depth and elevation in flat interfaces. They make elements appear to float above the page, drawing the user's eye and indicating hierarchy. Tailwind's shadow-* utilities provide a carefully designed scale of box shadows that work harmoniously together. The values range from the barely perceptible shadow-sm to the dramatic shadow-2xl, giving you full control over elevation.

<!-- Shadow elevation scale -->
<div class="grid grid-cols-2 gap-6 p-8 bg-gray-50">
  <div class="bg-white p-4 rounded-lg shadow-sm">shadow-sm</div>
  <div class="bg-white p-4 rounded-lg shadow">shadow</div>
  <div class="bg-white p-4 rounded-lg shadow-md">shadow-md</div>
  <div class="bg-white p-4 rounded-lg shadow-lg">shadow-lg</div>
  <div class="bg-white p-4 rounded-lg shadow-xl">shadow-xl</div>
  <div class="bg-white p-4 rounded-lg shadow-2xl">shadow-2xl</div>
</div>

Shadow Utility Reference

Each shadow in Tailwind's scale is designed for a specific elevation level. shadow-sm (very subtle, 1px blur) works for inputs and small UI elements. shadow and shadow-md suit cards and dropdowns. shadow-lg is ideal for popovers and modals. shadow-xl and shadow-2xl are reserved for large modals, full-screen overlays, and dramatic hero elements that need to appear significantly elevated.

<!-- Semantic shadow usage by component type -->
<div class="space-y-4 max-w-sm">
  <input class="w-full border border-gray-300 rounded-lg px-4 py-2 shadow-sm" placeholder="Input with shadow-sm" />
  <div class="bg-white border rounded-xl p-4 shadow-md">Card with shadow-md</div>
  <div class="bg-white rounded-2xl p-6 shadow-xl">Modal panel with shadow-xl</div>
</div>

shadow-inner for Inset Shadows

shadow-inner applies an inward shadow, making an element look recessed or pressed into the surface. This is the visual opposite of the standard elevation shadows. Common uses include pressed button states, search input fields to indicate they are active, concave containers, and toggle switch tracks that should look sunken to contrast with the raised knob element.

<!-- Inset shadow for recessed / pressed effects -->
<div class="flex gap-4">
  <!-- Search input looking recessed -->
  <div class="flex-1 bg-gray-100 rounded-lg px-4 py-3 shadow-inner flex items-center gap-2">
    <svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-4.35-4.35M17 11A6 6 0 111 11a6 6 0 0116 0z" />
    </svg>
    <span class="text-gray-400 text-sm">Search...</span>
  </div>

  <!-- Pressed button state -->
  <button class="bg-gray-200 shadow-inner px-4 py-2 rounded-lg font-medium text-gray-700">
    Pressed
  </button>
</div>

shadow-none to Remove Shadows

shadow-none removes all box shadows from an element. This is useful when you need to override an inherited shadow (for example, removing a card shadow on mobile where the card is full-width and the shadow looks awkward at the screen edges). The pattern shadow-md md:shadow-lg uses a smaller shadow on mobile and a larger one on desktop, where floating cards look better.

<!-- Remove shadow on mobile, restore on tablet+ -->
<div class="bg-white p-4 rounded-none sm:rounded-xl shadow-none sm:shadow-lg">
  <h3 class="font-bold">Responsive Card Shadow</h3>
  <p class="text-gray-600 mt-1">No shadow on mobile, shadow on larger screens.</p>
</div>

Colored Box Shadows

Tailwind v3 introduced colored shadows: shadow-blue-500 makes the shadow appear in blue. This creates vibrant, glow-like effects popular in dark themes and landing pages. The shadow color is applied using the --tw-shadow-color CSS variable, so you still need a shadow-* size utility alongside the color class — for example, shadow-lg shadow-blue-500/50.

<!-- Colorful glow effects with colored shadows -->
<div class="flex gap-6 p-8 bg-gray-900 justify-center">
  <button class="bg-blue-500 text-white px-6 py-3 rounded-xl font-bold shadow-lg shadow-blue-500/50">
    Blue Glow
  </button>
  <button class="bg-purple-500 text-white px-6 py-3 rounded-xl font-bold shadow-lg shadow-purple-500/50">
    Purple Glow
  </button>
  <button class="bg-pink-500 text-white px-6 py-3 rounded-xl font-bold shadow-lg shadow-pink-500/50">
    Pink Glow
  </button>
</div>

Hover-Lifted Card Pattern

One of the most common interactive patterns is the hover-lift effect: a card starts with a subtle shadow and gains a deeper shadow on hover, creating the illusion of lifting. Combine shadow-md hover:shadow-xl with transition-shadow duration-300 for a smooth animation. This signals to users that the card is interactive and makes dashboards and product listings feel polished.

<!-- Interactive card with hover-lift effect -->
<div class="grid grid-cols-3 gap-6 p-6">
  <div class="bg-white rounded-xl p-5 shadow-md hover:shadow-xl transition-shadow duration-300 cursor-pointer">
    <div class="w-10 h-10 bg-blue-100 rounded-lg flex items-center justify-center mb-3">
      <span class="text-blue-600">📊</span>
    </div>
    <h3 class="font-bold">Analytics</h3>
    <p class="text-sm text-gray-500 mt-1">View your stats</p>
  </div>
</div>

Drop Shadow vs Box Shadow

Tailwind provides both box shadows (shadow-*) and drop shadows (drop-shadow-*). The key difference: shadow-* applies to the element's bounding box (rectangle), while drop-shadow-* is a CSS filter that follows the element's actual visible shape, including transparent areas. drop-shadow-* is ideal for irregularly shaped elements like PNG icons with transparent backgrounds.

<!-- Box shadow (rectangular) vs drop shadow (shape-aware) -->
<div class="flex gap-8 p-6 items-center">
  <!-- Box shadow on transparent PNG -->
  <div>
    <p class="text-sm mb-2">shadow-lg (rectangular)</p>
    <svg class="w-16 h-16 text-blue-500 shadow-lg" viewBox="0 0 24 24" fill="currentColor">
      <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" />
    </svg>
  </div>

  <!-- Drop shadow follows shape -->
  <div>
    <p class="text-sm mb-2">drop-shadow-lg (shape-aware)</p>
    <svg class="w-16 h-16 text-blue-500 drop-shadow-lg" viewBox="0 0 24 24" fill="currentColor">
      <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" />
    </svg>
  </div>
</div>

Layering Multiple Shadows

CSS supports multiple shadows on one element, but Tailwind's built-in utilities provide only one shadow per class. For multiple layers, extend Tailwind's theme in tailwind.config.js with a custom shadow that includes a comma-separated list of shadow definitions. This allows sophisticated layering like a material design elevation effect or a subtle outer glow combined with an inner definition shadow.

/* In tailwind.config.js */
module.exports = {
  theme: {
    extend: {
      boxShadow: {
        /* Layered: outer glow + standard shadow */
        'glow-blue': '0 0 20px rgba(59,130,246,0.5), 0 4px 6px rgba(0,0,0,0.1)',
        /* Material elevation --*/
        'material': '0 2px 1px rgba(0,0,0,0.05), 0 4px 8px rgba(0,0,0,0.1), 0 16px 24px rgba(0,0,0,0.06)',
      }
    }
  }
}

Shadows in Dark Mode

Shadows become less visible on dark backgrounds because they typically use translucent black. In dark mode, Tailwind colored shadows — or lighter shadow tones — maintain visibility. A common pattern is shadow-gray-700/50 in dark mode to create a visible shadow against a dark surface. Alternatively, use borders instead of shadows on dark backgrounds since borders remain visible regardless of background lightness.

<!-- Shadow strategies for dark mode -->
<div class="bg-gray-900 p-8 rounded-xl space-y-4">
  <!-- Standard shadow (invisible on dark bg) -->
  <div class="bg-gray-800 p-4 rounded-lg shadow-xl">
    Shadow barely visible
  </div>

  <!-- Colored shadow for dark mode -->
  <div class="bg-gray-800 p-4 rounded-lg shadow-lg shadow-gray-700/70">
    Shadow visible with colored tint
  </div>

  <!-- Border instead of shadow on dark bg -->
  <div class="bg-gray-800 border border-gray-700 p-4 rounded-lg">
    Border as separator on dark backgrounds
  </div>
</div>

Focus Shadow for Accessibility

Visible focus indicators are required by WCAG 2.1 AA accessibility standards. While Tailwind's ring-* utilities are the preferred modern approach, box shadows can also serve as focus indicators. The pattern focus:shadow-[0_0_0_3px_rgba(59,130,246,0.5)] creates a blue halo — essentially what ring-2 ring-blue-500 does, but using the shadow property for legacy browser compatibility.

<!-- Accessible focus indicators using shadow and ring -->
<div class="flex gap-4 flex-wrap">
  <!-- Using ring (preferred modern approach) -->
  <button class="bg-blue-500 text-white px-4 py-2 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
    Ring Focus
  </button>

  <!-- Using shadow for legacy support -->
  <button class="bg-purple-500 text-white px-4 py-2 rounded-lg focus:outline-none focus:shadow-[0_0_0_3px_rgba(168,85,247,0.5)]">
    Shadow Focus
  </button>
</div>

Complete Shadow Hierarchy

In a real UI, consistent shadow assignment creates a clear visual hierarchy. Use a systematic approach: shadow-sm for subtle borders on inputs, shadow-md for content cards, shadow-lg for dropdown menus, shadow-xl for side drawers, and shadow-2xl for modal dialogs. This hierarchy helps users understand spatial relationships and which elements are contextual versus primary.

<!-- Dashboard showing shadow hierarchy -->
<div class="bg-gray-100 p-6 space-y-4">
  <!-- Page-level card: shadow-md -->
  <div class="bg-white rounded-xl shadow-md p-5">
    <h2 class="font-bold text-lg">Analytics Overview</h2>

    <!-- Inline component: shadow-sm -->
    <div class="mt-4 grid grid-cols-3 gap-3">
      <div class="bg-gray-50 rounded-lg p-3 shadow-sm text-center">
        <div class="text-2xl font-bold text-blue-600">142</div>
        <div class="text-xs text-gray-500">Users</div>
      </div>
      <div class="bg-gray-50 rounded-lg p-3 shadow-sm text-center">
        <div class="text-2xl font-bold text-green-600">94%</div>
        <div class="text-xs text-gray-500">Uptime</div>
      </div>
      <div class="bg-gray-50 rounded-lg p-3 shadow-sm text-center">
        <div class="text-2xl font-bold text-purple-600">38</div>
        <div class="text-xs text-gray-500">Issues</div>
      </div>
    </div>
  </div>
</div>

Quick Check

Test your understanding of Tailwind CSS Mastery concepts from this lesson.

Lesson Recap

In this lesson you learned: shadow-sm through shadow-2xl provide an elevation scale for depth, shadow-inner creates a recessed appearance for pressed states and search inputs, and colored shadows like shadow-lg shadow-blue-500/50 create glow effects for dark themes. Next up we explore outline and ring utilities for accessible focus indicators.

常见问题解答

「盒阴影」课时是免费的吗?

是的 — 「盒阴影」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Tailwind CSS Academy 课程的其余内容,请升级到 CoddyKit PRO。 Tailwind CSS Academy 课程共包含 4 节课。

「盒阴影」这节课中我会学到什么?

使用 shadow-sm 到 shadow-2xl 增加层次感,使用 shadow-inner 创建内嵌效果,并使用 shadow-none 移除阴影。 你通过在浏览器中直接运行的动手代码来练习 Tailwind CSS Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Tailwind CSS Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Tailwind CSS Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「盒阴影」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Tailwind CSS Academy 课中编写并运行代码吗?

能。每节 Tailwind CSS Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 边框宽度与颜色
  2. 边框半径与圆角
  3. 盒阴影
  4. 轮廓与环形实用程序
← 返回 Tailwind CSS Academy