Get started with 33% off your first certification using code: 33OFFNEW

How to adjust `shadow` properties in Tailwind

3 min read
Published on 20th March 2024

Tailwind CSS is a highly customizable utility-first CSS framework that empowers developers to style their applications efficiently. Among its many features, the (box) shadow utility stands out for adding depth and emphasis to elements, enhancing the user interface. However, the default shadow sizes may not always fit your design requirements. Fortunately, Tailwind allows you to customize these properties, offering you complete control over your shadows' appearance. Let's dive into how you can adjust the shadow properties for each size in Tailwind CSS.

Understanding Tailwind's Default Shadows

Tailwind provides a range of shadow utilities out of the box, from shadow-sm for a subtle look to shadow-2xl for a more pronounced effect. These default utilities are designed to cover most use cases, but customization is a core philosophy of Tailwind, encouraging you to tweak them as needed.

Step 1: Setting Up Your Tailwind Configuration

To customize shadow sizes, you'll need to modify your tailwind.config.js file. This file is where you define your custom configurations, overriding the defaults provided by Tailwind.

If you haven't already customized your configuration, start by creating a tailwind.config.js file in your project root (if Tailwind is installed but you haven't customized it yet, you can generate this file by running npx tailwindcss init).

Step 2: Customizing Shadow Sizes

In your tailwind.config.js, you can extend the default theme or completely override the shadow utilities. To adjust shadow properties specifically, focus on the extend section under theme to add to or override the existing shadows.

Here's how to customize a specific shadow size:

module.exports = {
  theme: {
    extend: {
      boxShadow: {
        'sm': '0 1px 2px 0 rgba(0, 0, 0, 0.05)',
        'md': '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.05)',
        // Add your custom shadows here
        'custom-1': '0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.05)',
      },
    },
  },
}

In this example, 'custom-1' is a new shadow size we've introduced. You can name your custom shadows as you like and define their properties using the CSS box-shadow property syntax.

Step 3: Applying Your Custom Shadows

After defining your custom shadows in the tailwind.config.js file, you can apply them to your elements using class names that correspond to the keys in your configuration.

For instance, to apply the 'custom-1' shadow we defined earlier, you would add the class shadow-custom-1 to your HTML element:

<div class="shadow-custom-1 ...">
  <!-- Your content here -->
</div>

Step 4: Experiment and Refine

The beauty of Tailwind CSS lies in its flexibility and ease of experimentation. Try different shadow configurations to see what works best for your design. Consider factors like the color, opacity, spread, and blur radius of your shadows. Adjusting these values can significantly impact the depth and emphasis of elements on your page.

Best Practices

  • Aim for a consistent shadow style across your UI to maintain a cohesive design language.
  • Be mindful of performance. Excessive use of large, complex shadows can impact rendering times, especially on lower-powered devices.
  • Ensure that the use of shadows does not compromise text readability or the overall accessibility of your application.

Customizing shadow sizes in Tailwind CSS allows you to add depth and sophistication to your web design projects. By adjusting shadow properties in your tailwind.config.js file, you can achieve the exact look and feel you desire, enhancing the visual appeal of your application. Tailwind's utility-first approach, combined with its extensive customization capabilities, makes it an invaluable tool for modern web development, empowering you to build beautifully designed, highly responsive user interfaces.