Tailwind CSS Shadow Styling Guide

published on 03 October 2024

Tailwind CSS makes adding shadows to your web elements quick and easy. Here's what you need to know:

  • Tailwind offers pre-built shadow classes from shadow-sm to shadow-2xl
  • Use classes like shadow-md to instantly add shadows to any element
  • Customize shadows in your tailwind.config.js file
  • Combine shadow classes with other Tailwind utilities for dynamic effects
  • Use shadows wisely to avoid slowing down your page

Key shadow classes:

Class Effect
shadow-sm Tiny shadow
shadow Default shadow
shadow-md Medium shadow
shadow-lg Large shadow
shadow-xl Extra large shadow
shadow-2xl Double extra large shadow
shadow-inner Inner shadow
shadow-none No shadow

To add a shadow, simply use:

<div class="shadow-md">
  This box has a medium shadow
</div>

Customize shadows, use responsive designs, and create hover effects - all without writing custom CSS. This guide covers everything from basic usage to advanced techniques for Tailwind CSS shadows.

Tailwind CSS Shadow Basics

Tailwind CSS

Tailwind CSS makes adding shadows easy. Here's what you need to know:

Common Shadow Classes

Tailwind offers these shadow utilities:

Class Effect
shadow-sm Tiny shadow
shadow Default shadow
shadow-md Medium shadow
shadow-lg Large shadow
shadow-xl Extra large shadow
shadow-2xl Double extra large shadow
shadow-inner Inner shadow
shadow-none No shadow

To use a shadow, add the class to your element:

<div class="shadow-md">
  This box has a medium shadow
</div>

Picking Shadow Sizes

Your design needs dictate the shadow size:

  • shadow-sm for subtle depth
  • shadow for general use
  • shadow-lg and up for standout elements

For a prominent button:

<button class="bg-blue-500 text-white px-4 py-2 rounded shadow-xl">
  Sign Up Now
</button>

Inner vs. Outer Shadows

Tailwind gives you both:

  • Outer shadows (like shadow-md) for a lifted look
  • shadow-inner for depth within an element

For a text input with inner shadow:

<input type="text" class="border rounded p-2 shadow-inner" placeholder="Enter your name">

You can mix shadow classes with other Tailwind utilities. For a hover effect:

<div class="shadow-md hover:shadow-xl transition-shadow duration-300">
  This box shadow grows on hover
</div>

How to Use Shadows in Tailwind CSS

Tailwind CSS makes adding shadows a breeze. Here's the lowdown:

Adding Shadow Classes

Want a shadow? Just slap on a class:

<div class="shadow-md">Medium shadow here</div>

Tailwind's shadow sizes:

Class Effect
shadow-sm Small
shadow Default
shadow-md Medium
shadow-lg Large
shadow-xl Extra large
shadow-2xl Double XL
shadow-inner Inner
shadow-none None

Need something to pop? Go big:

<button class="bg-blue-500 text-white px-4 py-2 rounded shadow-lg">
  Sign Up Now
</button>

Mixing Shadows with Other Classes

Shadows play nice with other Tailwind classes. Mix and match for cool effects:

<div class="bg-white rounded-lg p-6 shadow-md hover:shadow-xl transition-shadow duration-300">
  Shadow grows on hover
</div>

This combo uses:

  • shadow-md by default
  • hover:shadow-xl for bigger shadow on hover
  • transition-shadow and duration-300 for smooth change

Dark theme? Add -strong:

<div class="bg-gray-800 text-white p-4 shadow-lg-strong">
  Dark mode, strong shadow
</div>

Changing Shadow Settings

Tailwind CSS lets you customize shadows. Here's how:

Shadow Color

Use built-in colors:

<div class="shadow-black">Black shadow</div>
<div class="shadow-red-500">Red shadow</div>

Or add custom colors in tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'custom-purple': '0 4px 6px -1px rgba(128, 0, 128, 0.1)',
      },
    },
  },
}

Then use it:

<div class="shadow-custom-purple">Purple shadow</div>

Shadow Opacity

Adjust opacity in your config:

module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'faded': '0 4px 6px -1px rgba(0, 0, 0, 0.05)',
        'strong': '0 4px 6px -1px rgba(0, 0, 0, 0.3)',
      },
    },
  },
}

Apply it:

<div class="shadow-faded">Subtle shadow</div>
<div class="shadow-strong">Bold shadow</div>

Shadow Position and Blur

Customize in your config:

module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'offset': '5px 5px 5px rgba(0, 0, 0, 0.1)',
        'blurred': '0 0 10px rgba(0, 0, 0, 0.1)',
      },
    },
  },
}

Use them:

<div class="shadow-offset">Offset shadow</div>
<div class="shadow-blurred">Blurry shadow</div>

For quick tweaks, use arbitrary values:

<div class="shadow-[0_35px_60px_-15px_rgba(0,0,0,0.3)]">
  Custom shadow
</div>

Shadows for Different Screen Sizes

Tailwind CSS lets you change shadow styles based on screen size. This helps make your design look good on all devices.

Here's how to use different shadows for different screens:

<div class="shadow-md md:shadow-lg lg:shadow-xl">
  This box changes shadows on mobile, tablet, and desktop
</div>

This gives you:

  • A medium shadow on mobile
  • A large shadow on tablets
  • An extra-large shadow on desktops

When planning shadows:

  • Use light shadows on mobile
  • Step up the intensity for tablets
  • Go stronger on desktop for more impact

Here's a shadow that grows with screen size:

<div class="shadow-sm sm:shadow-md md:shadow-lg lg:shadow-xl xl:shadow-2xl">
  Shadow gets stronger as screens get bigger
</div>

Want custom shadows? Add this to your tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'custom-mobile': '0 1px 2px rgba(0, 0, 0, 0.1)',
        'custom-tablet': '0 4px 6px rgba(0, 0, 0, 0.1)',
        'custom-desktop': '0 10px 15px rgba(0, 0, 0, 0.1)',
      },
    },
  },
}

Then use them like this:

<div class="shadow-custom-mobile md:shadow-custom-tablet lg:shadow-custom-desktop">
  Custom shadow for each device
</div>

With these tricks, you can make your shadows work perfectly on any screen size.

sbb-itb-55aadfa

Advanced Shadow Techniques

Tailwind CSS lets you create complex shadows. Here's how to level up your shadow game:

Multiple Shadows

Want depth? Stack shadows:

<div class="shadow-[0_10px_15px_-3px_rgba(0,0,0,0.1),0_4px_6px_-4px_rgba(0,0,0,0.1)] p-6 bg-white rounded-lg">
  Multiple shadows
</div>

This adds two shadows with different offsets and blur. It's like layering light sources.

Interactive Shadows

Make shadows respond to user actions:

<button class="shadow-md hover:shadow-xl transition-shadow duration-300 ease-in-out p-4 bg-blue-500 text-white rounded">
  Hover me
</button>

The button starts with a medium shadow. Hover, and it smoothly grows to extra-large. It's subtle but effective.

Custom Shadow Effects

Need something unique? Tailwind's got you covered. Add this to your tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'neon': '0 0 10px #00ff00, 0 0 20px #00ff00',
      },
    },
  },
}

Then use it:

<div class="shadow-neon p-4 bg-black text-white">
  Neon glow effect
</div>

Boom! You've got a neon glow. Perfect for that retro vibe.

These techniques let you create shadows that aren't just functional, but part of your design language. Play around and see what you can come up with!

Making Shadows Work Well

Shadows add depth to your site, but they can slow things down. Here's how to use them right in Tailwind CSS:

Smart Shadow Use

Balance looks and speed:

  • Start small. Use shadow-sm or shadow-md for most things. Save shadow-xl for big, clickable stuff.
  • Show importance. Different shadow sizes = different importance:
<div class="shadow-md">
  <h2 class="text-xl">Main content</h2>
  <button class="shadow-lg">Important action</button>
</div>
  • Don't overdo it. Too many shadows = cluttered look and slow site.

Shadows and Page Speed

Shadows can slow your site. Here's how to keep it fast:

1. Trim your config

Only keep shadows you'll use in tailwind.config.js:

module.exports = {
  theme: {
    boxShadow: {
      sm: '0 1px 2px 0 rgba(0, 0, 0, 0.05)',
      DEFAULT: '0 1px 3px 0 rgba(0, 0, 0, 0.1)',
      md: '0 4px 6px -1px rgba(0, 0, 0, 0.1)',
      lg: '0 10px 15px -3px rgba(0, 0, 0, 0.1)',
    }
  }
}

This cuts down your CSS file size.

2. Use JIT mode

Just-in-Time mode makes CSS on-demand. Smaller files, faster loads.

3. Shrink your CSS

Use cssnano to minify CSS. With Tailwind CLI, add --minify:

npx tailwindcss -o build.css --minify

4. Check speed often

Test your site speed with Google PageSpeed Insights or WebPageTest. Watch the "Time to Interactive" - heavy CSS can mess with this.

Fixing Common Shadow Problems

Shadow issues in Tailwind CSS? Let's tackle them head-on.

Layer Order Woes

Shadows not showing up right? It's probably a stacking context problem. Here's the fix:

<div class="relative z-10 shadow-lg">
  <!-- Content goes here -->
</div>

This puts your shadowed element on top, where it belongs.

Chopped-off Shadows

Seeing your shadows get cut off? That's no fun. Try this:

<div class="p-4">
  <div class="shadow-xl">
    <!-- Content goes here -->
  </div>
</div>

Still not working? Adjust the parent's overflow:

<div class="overflow-visible">
  <div class="shadow-xl">
    <!-- Content goes here -->
  </div>
</div>

Now your shadows should be showing up in all their glory.

Wrap-up

Tailwind CSS shadows add depth to your designs. Here's what you need to know:

  • Tailwind offers shadow utilities from shadow-sm to shadow-2xl, plus shadow-inner for inset effects.

  • Customize shadows in your tailwind.config.js file under theme.boxShadow.

  • Use arbitrary values for unique shadows: shadow-[0_35px_60px_-15px_rgba(0,0,0,0.3)].

  • Combine shadows with other utilities for dynamic effects:

<button class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded shadow-md hover:shadow-lg transition duration-300">
  Click me
</button>

This button changes color and shadow on hover.

  • Use shadows wisely. Too many can slow down your page.

  • If shadows aren't showing up, check your z-index and overflow settings.

FAQs

How to set custom shadow in Tailwind?

To set a custom shadow in Tailwind CSS:

1. Open tailwind.config.js

2. Add a new key to theme.boxShadow

3. Use the custom class

Example:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'custom': '0 50px 25px -24px rgb(0 0 0 / 0.3)'
      }
    },
  },
}

In HTML:

<div class="shadow-custom">Custom shadow applied</div>

What is the default boxShadow in Tailwind?

Tailwind CSS offers these default shadow utilities:

Class Shadow Value
shadow-sm 0 1px 2px 0 rgb(0 0 0 / 0.05)
shadow 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)
shadow-md 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1)
shadow-lg 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1)
shadow-xl 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1)
shadow-2xl 0 25px 50px -12px rgb(0 0 0 / 0.25)
shadow-inner inset 0 2px 4px 0 rgb(0 0 0 / 0.05)
shadow-none none

How to add boxShadow using Tailwind CSS?

Adding a box shadow is simple:

<div class="shadow-md">
  Medium shadow here
</div>

For custom shadows:

<div class="shadow-[0_35px_60px_-15px_rgba(0,0,0,0.3)]">
  Custom shadow applied
</div>

How to give shadow using Tailwind CSS?

It's easy:

  1. Pick a shadow class
  2. Add it to your element

Like this:

<button class="bg-blue-500 text-white p-2 rounded shadow-lg">
  Big shadow button
</button>

How to add shadow with Tailwind?

Just use the right shadow class:

<div class="bg-white p-4 rounded shadow-xl">
  Extra large shadow box
</div>

For multiple shadows:

<div class="shadow-md shadow-blue-500/50">
  Medium blue shadow
</div>

Related posts

Read more

Built on Unicorn Platform