0Pricing
Tailwind CSS Academy · Lekcja

Cienie elementów

Dodawać głębi za pomocą klas od shadow-sm do shadow-2xl, używać shadow-inner dla efektów wewnętrznych oraz usuwać cienie przez shadow-none.

Cienie elementów to bezpłatna lekcja Tailwind CSS Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Tailwind CSS Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Tailwind CSS Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Cienie elementów” jest bezpłatna?

Tak — pełny tekst „Cienie elementów” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Tailwind CSS Academy, przejdź na CoddyKit PRO. Kurs Tailwind CSS Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Cienie elementów”?

Dodawać głębi za pomocą klas od shadow-sm do shadow-2xl, używać shadow-inner dla efektów wewnętrznych oraz usuwać cienie przez shadow-none. Ćwiczysz Tailwind CSS Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Tailwind CSS Academy?

Nie wymagamy żadnego doświadczenia. Tailwind CSS Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Cienie elementów”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Tailwind CSS Academy?

Tak. Każda lekcja Tailwind CSS Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Szerokość i kolor obramowania
  2. Promień obramowania i zaokrąglone narożniki
  3. Cienie elementów
  4. Narzędzia obrysu i pierścienia
← Powrót do Tailwind CSS Academy