YouTip LogoYouTip

Tailwindcss Shadow

In modern web design, shadow effects are an important tool for creating visual hierarchy and depth. Tailwind CSS provides a powerful and flexible set of shadow utility classes to easily implement various shadow effects. This article will provide a comprehensive introduction to the shadow system in Tailwind CSS, including box-shadow, text-shadow, shadow color and opacity control, and the application of inner shadow effects. * * * ## What is CSS Shadow? Before diving into Tailwind's specific implementation, let's understand the basic concepts of CSS shadows. CSS shadows are mainly divided into two types: 1. **Box Shadow**: Applied to the entire box model of an element 2. Text Shadow: Applied only to the text content of an element Shadow effects can: * Enhance the visual hierarchy of elements * Create a "floating" effect * Simulate lighting and depth * Highlight important content * * * ## Box-shadow in Tailwind Tailwind provides a series of preset box-shadow utility classes, ranging from subtle to strong, divided into 6 levels: ## Example
Small Shadow
Default Shadow
Medium Shadow
Large Shadow
Extra Large Shadow
2XL Shadow
[Try it Β»](#) ### Shadow Level Comparison Table | Class Name | Description | Use Case | | --- | --- | --- | | shadow-sm | Very subtle shadow | Button hover, slight elevation | | shadow | Default shadow (medium size) | Cards, regular elements | | shadow-md | Medium shadow | Modals, important notifications | | shadow-lg | Large shadow | Floating panels, highlighted elements | | shadow-xl | Extra large shadow | Special emphasis elements | | shadow-2xl | 2XL shadow | Elements that need very obvious emphasis | ### Code Examples and Practice ## Example
Tailwind Shadow Example

This is a card example using shadow-md

[Try it Β»](#) **Effect Description**: * Creates a card with white background * Uses `shadow-md` class to add medium shadow * Combined with rounded corners (`rounded-xl`) to make the shadow effect more natural * * * ## Text-shadow in Tailwind Tailwind does not include text-shadow utility classes by default, but they can be added in the following ways: ### Method 1: Using Plugin Install `tailwindcss-textshadow` plugin: ## Example npm install tailwindcss-textshadow Then configure in `tailwind.config.js`: ## 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

Title 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: * `shadow-{color}`: Sets shadow color (e.g., blue-500) * `/{opacity}`: Sets shadow opacity (e.g., 50 means 50%) ### 2. Available Colors and Opacity Tailwind supports all built-in colors and any opacity values: ## Example
Red Semi-transparent Shadow
Emerald Green Strong Shadow
Purple Light Shadow
### 3. Custom Shadow Color Extend in `tailwind.config.js`: ## Example module.exports={ theme:{ extend:{ boxShadow:{ 'custom':'0 4px 6px -1px rgba(156, 39, 176, 0.3)', } } } } * * * ## Inner Shadow Effect (inset) Inner shadows can create a sunken or pressed effect, commonly used for interactive elements like buttons and input fields. ### 1. Using Preset Inner Shadow Classes ## Example
Inner Shadow Effect
### 2. Custom Inner Shadow Add in configuration file: ## Example module.exports={ theme:{ extend:{ boxShadow:{ 'inner-lg':'inset 0 4px 6px 0 rgba(0, 0, 0, 0.1)', } } } } ### 3. Practical Application Example ## Example **Effect Description**: * Default state has slight inner shadow * Inner shadow intensifies on hover (`hover:shadow-inner-lg`) * Creates a visual effect of the button being pressed * * * ## Advanced Tips and Best Practices ### 1. Combine Inner and Outer Shadows ## Example
Outer Shadow + Inner Shadow Combination
### 2. Responsive Shadow ## 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 medium shadow by default, shadow grows and changes color on hover 2. Design an input box with inner shadow, inner shadow disappears when focused 3. Implement a text title using text-shadow to create a neon effect 4. Combine inner and outer shadows to create a 3D button * * * ## Summary Points ### Core Utility Classes Quick Reference | Function | Class Example | Description | | --- | --- | --- | | Box Shadow | shadow, shadow-lg | Different sizes of outer shadow | | Inner Shadow | shadow-inner | Inner shadow of element | | Shadow Color | shadow-blue-500 | Sets shadow color | | Shadow Opacity | shadow-blue-500/50 | Sets shadow opacity | | Text Shadow | text-shadow (requires custom) | Adds shadow to text | ### Best Practice Suggestions 1. **Use in Moderation**: Shadows should enhance the design, not distract 2. **Stay Consistent**: Maintain similar shadow hierarchies throughout the project 3. **Consider Performance**: Too many complex shadows may affect rendering performance 4. **Combine with Other Properties**: Shadows work better when combined with rounded corners and transition effects
← Regexp Positional MatchingCss3 Quiz β†’