Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

tailwind CSS Shadows

Tailwind CSS Shadows

In modern web design, shadow effects are an important tool for creating visual hierarchy and a sense of depth.

Tailwind CSS provides a powerful and flexible set of shadow utility classes that can easily implement various shadow effects.

This article comprehensively introduces the shadow system in Tailwind CSS, including box-shadow, text-shadow, control of shadow color and opacity, and the application of inset shadow effects.


What Are CSS Shadows?

Before diving into Tailwind's specific implementation, let's first understand the basic concepts of CSS shadows.

CSS shadows are mainly divided into two types:

  1. Box Shadow (box-shadow): Applied to the entire box model of an element
  2. Text Shadow (text-shadow): Applied only to the text content of an element

Shadow effects can:


box-shadow in Tailwind

Tailwind provides a series of preset box-shadow utility classes, divided into 6 levels from subtle to strong:

Example

Small shadow

Default shadow

Medium shadow

Large shadow

Extra large shadow

Extra extra large shadow

Try it »

Shadow Level Comparison Table

Class Names Description Applicable scenarios
shadow-sm Very subtle shadow Button hover, slight lift
shadow Default shadow (medium size) Cards, regular elements
shadow-md Medium shadow Modals, important notifications
shadow-lg Large shadow Floating panels, prominent elements
shadow-xl Extra large shadow Special emphasis elements
shadow-2xl Extra extra large shadow Elements that need to stand out very prominently

Code Examples and Practice

Example

 

Tailwind Shadow Example

 

This is a card example using shadow-md

Try it »

Effect Description:


text-shadow in Tailwind

Tailwind does not include text-shadow utility classes by default, but you can add them in the following ways:

Method 1: Using a Plugin

Installation tailwindcss-textshadow plugin:

Example

npm

install

tailwindcss-textshadow

Then in tailwind.config.js Configure in :

Example

module.

exports

=

{

  plugins

:

[

    require

(

'tailwindcss-textshadow'

)

 

]

,

  theme

:

{

    textShadow

:

{

     

'sm'

:

'1px 1px 2px rgba(0, 0, 0, 0.5)'

,

     

'DEFAULT'

:

'2px 2px 4px rgba(0, 0, 0, 0.5)'

,

     

'lg'

:

'4px 4px 8px rgba(0, 0, 0, 0.5)'

,

   

}

 

}

}

Usage example:

Example

Heading with Shadow

Method 2: Custom Utility Class

Add in CSS:

Example

@layer utilities {

 

.text-shadow

{

   

text-shadow

:

2px

2px

4px

rgba

(

0

,

0

,

0

,

0.5

)

;

 

}

 

.text-shadow-sm

{

   

text-shadow

:

1px

1px

2px

rgba

(

0

,

0

,

0

,

0.5

)

;

 

}

}


Shadow Color and Opacity Control

Tailwind allows you to control the color and opacity of shadows through utility class combinations.

1. Using Shadow Color Utility Classes

Example

Blue semi-transparent shadow

Syntax Analysis:

2. Available Colors and Opacity

Tailwind supports all built-in colors and arbitrary opacity values:

Example

Red semi-transparent shadow

Emerald green strong shadow

Purple light shadow

3. Custom Shadow Colors

in tailwind.config.js Extend in :

Example

module.

exports

=

{

  theme

:

{

    extend

:

{

      boxShadow

:

{

       

'custom'

:

'0 4px 6px -1px rgba(156, 39, 176, 0.3)'

,

     

}

   

}

 

}

}


Inset Shadow Effect (inset)

Inset shadows can create a recessed or pressed-in effect, commonly used in interactive elements such as buttons and inputs.

1. Using Preset Inset Shadow Classes

Example

Inset Shadow Effect

2. Custom Inset Shadow

Add in the configuration file:

Example

module.

exports

=

{

  theme

:

{

    extend

:

{

      boxShadow

:

{

       

'inner-lg'

:

'inset 0 4px 6px 0 rgba(0, 0, 0, 0.1)'

,

     

}

   

}

 

}

}

3. Practical Application Examples

Example

Effect Description:


Advanced Techniques and Best Practices

1. Combining Outer and Inner Shadows

Example

Outer shadow + inner shadow combination

2. Responsive Shadows

Example

Small shadow on small screens, large shadow on large screens

3. Hover and Focus States

Example

4. Shadow Animation

Example


Practice challenges

  1. Create a card component with a medium shadow by default, which enlarges and changes color on hover
  2. Design an input with an inset shadow that disappears when focused
  3. Implement a text heading using text-shadow to create a neon effect
  4. Combine outer and inner shadows to create a button with a three-dimensional feel

Summary of key points

Core Utility Classes Quick Reference

Function Class Name Example Description
Box Shadow shadow, shadow-lg Outer shadows of different sizes
Inner Shadow shadow-inner Shadow inside the element
Shadow Color shadow-blue-500 Set the shadow color
Shadow Opacity shadow-blue-500/50 Set the shadow opacity
Text Shadow text-shadow (requires customization) Adds a shadow to text.

Best Practice Recommendations

  1. Use in moderation: Shadows should enhance the design, not distract from it
  2. Stay Consistent: Maintain similar shadow levels throughout the project
  3. Consider Performance: Too many complex shadows may affect rendering performance
  4. Combine with Other Properties: Shadows work better with rounded corners and transition effects