Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Shadow Utilities in Bootstrap 5

Shadow Utilities: Add or remove shadows from elements with various intensity levels and responsive variations.

Shadow Classes

Shadow Intensity Levels
No Shadow
.shadow-none
Small Shadow
.shadow-sm
Regular Shadow
.shadow
Large Shadow
.shadow-lg
Extra Large Shadow
.shadow-xl (Custom)
XXL Shadow
.shadow-xxl (Custom)
<!-- Shadow utilities -->
<div class="shadow-none">No shadow (default)</div>
<div class="shadow-sm">Small shadow</div>
<div class="shadow">Regular shadow</div>
<div class="shadow-lg">Large shadow</div>

<!-- Custom shadow classes (via SCSS) -->
<div class="shadow-xl">Extra large shadow</div>
<div class="shadow-xxl">XXL shadow</div>

<!-- Inline shadows -->
<span class="shadow-sm d-inline-block p-2">Inline element with shadow</span>

<!-- Cards with shadows -->
<div class="card shadow-sm">Small shadow card</div>
<div class="card shadow">Regular shadow card</div>
<div class="card shadow-lg">Large shadow card</div>

<!-- Buttons with shadows -->
<button class="btn btn-primary shadow-sm">Button with shadow</button>
<button class="btn btn-success shadow">Button with regular shadow</button>
<button class="btn btn-warning shadow-lg">Button with large shadow</button>

<!-- Images with shadows -->
<img src="image.jpg" class="img-fluid shadow" alt="Image with shadow">

<!-- Text shadows (requires custom CSS) -->
<h1 class="text-shadow">Text with shadow</h1>

Shadow Comparison

Shadow Properties
ClassBox Shadow ValueBlur RadiusSpread RadiusOpacityUse Case
.shadow-nonenone---Remove shadows
.shadow-sm0 .125rem .25rem rgba(0,0,0,.075)0.25rem07.5%Subtle elevation
.shadow0 .5rem 1rem rgba(0,0,0,.15)1rem015%Default elevation
.shadow-lg0 1rem 3rem rgba(0,0,0,.175)3rem017.5%High elevation
.shadow-xl (custom)0 2rem 4rem rgba(0,0,0,.2)4rem020%Very high elevation
Visual Comparison
.shadow-none
.shadow-sm
.shadow
.shadow-lg
<!-- Default shadow values in Bootstrap -->
.shadow-none {
  box-shadow: none !important;
}

.shadow-sm {
  box-shadow: 0 .125rem .25rem rgba(0,0,0,.075) !important;
}

.shadow {
  box-shadow: 0 .5rem 1rem rgba(0,0,0,.15) !important;
}

.shadow-lg {
  box-shadow: 0 1rem 3rem rgba(0,0,0,.175) !important;
}

/* Custom shadow classes (add via SCSS) */
.shadow-xl {
  box-shadow: 0 2rem 4rem rgba(0,0,0,.2) !important;
}

.shadow-xxl {
  box-shadow: 0 3rem 6rem rgba(0,0,0,.25) !important;
}

/* Inset shadow (custom) */
.shadow-inset {
  box-shadow: inset 0 2px 4px rgba(0,0,0,.1) !important;
}

/* Multiple shadows (custom) */
.shadow-multi {
  box-shadow: 
    0 4px 6px rgba(0,0,0,.1),
    0 1px 3px rgba(0,0,0,.08) !important;
}

Shadow on Different Elements

Applying Shadows to Various Components
Cards
Small Shadow

Card with subtle shadow.

Regular Shadow

Card with default shadow.

Buttons
Badges
Badge 1Badge 2Badge 3
Images & Avatars
Image
JD
Alerts
Alert with small shadow
Navigation
Input Groups
@
<!-- Cards with shadows -->
<div class="card shadow-sm">
  <div class="card-body">Small shadow card</div>
</div>

<div class="card shadow">
  <div class="card-body">Regular shadow card</div>
</div>

<div class="card shadow-lg">
  <div class="card-body">Large shadow card</div>
</div>

<!-- Buttons with shadows -->
<button class="btn btn-primary shadow-sm">Button</button>
<button class="btn btn-success shadow">Button</button>
<button class="btn btn-warning shadow-lg">Button</button>

<!-- Images with shadows -->
<img src="image.jpg" class="img-fluid rounded shadow" alt="Image">

<!-- Avatars with shadows -->
<div class="rounded-circle bg-primary shadow-lg" 
     style="width: 80px; height: 80px;">
  Avatar
</div>

<!-- Badges with shadows -->
<span class="badge bg-primary shadow-sm">Badge</span>
<span class="badge bg-success shadow">Badge</span>
<span class="badge bg-danger shadow-lg">Badge</span>

<!-- Navigation with shadow -->
<nav class="navbar navbar-light bg-light shadow">
  <!-- Nav content -->
</nav>

<!-- Input groups with shadows -->
<div class="input-group shadow">
  <input type="text" class="form-control">
  <button class="btn btn-primary">Button</button>
</div>

<!-- Alerts with shadows -->
<div class="alert alert-info shadow-sm">
  Alert message
</div>

<!-- List groups with shadows -->
<div class="list-group shadow">
  <div class="list-group-item">Item 1</div>
  <div class="list-group-item">Item 2</div>
</div>

Hover Shadows

Interactive Shadow Effects
Card with Hover Effect
Hover Card

Shadow increases on hover.

Button with Hover Effect
Image with Hover Effect
Image
Animated Shadow on Click
Shadow Transition Classes
EffectCSS TransitionHover StateUse Case
Elevatebox-shadow 0.3s ease.shadow → .shadow-lgCards, buttons
Pressall 0.2s ease.shadow-lg → .shadow-smClickable elements
Floatall 0.3s easeShadow + translateY(-5px)Product cards
<!-- Hover shadow effects (custom CSS required) -->
<style>
  /* Simple hover elevation */
  .hover-shadow {
    transition: box-shadow 0.3s ease;
  }
  
  .hover-shadow:hover {
    box-shadow: 0 .5rem 1rem rgba(0,0,0,.15) !important;
  }
  
  /* Button hover effect */
  .btn-hover-shadow {
    transition: all 0.3s ease;
  }
  
  .btn-hover-shadow:hover {
    box-shadow: 0 1rem 3rem rgba(0,0,0,.175) !important;
    transform: translateY(-2px);
  }
  
  /* Click effect */
  .btn-click-shadow:active {
    box-shadow: 0 0.25rem 0.5rem rgba(0,0,0,.1) !important;
    transform: translateY(2px);
  }
  
  /* Card hover with multiple shadows */
  .card-hover-effect {
    transition: all 0.3s ease;
  }
  
  .card-hover-effect:hover {
    box-shadow: 
      0 1rem 3rem rgba(0,0,0,.175),
      0 0.5rem 1rem rgba(0,0,0,.1) !important;
    transform: translateY(-5px);
  }
  
  /* Image hover with stronger shadow */
  .img-hover-shadow {
    transition: all 0.3s ease;
  }
  
  .img-hover-shadow:hover {
    box-shadow: 0 1.5rem 4rem rgba(0,0,0,.25) !important;
  }
</style>

<!-- Usage -->
<div class="card shadow-sm hover-shadow">
  <!-- Hover to increase shadow -->
</div>

<button class="btn btn-primary shadow btn-hover-shadow">
  <!-- Hover for elevation effect -->
</button>

<img src="image.jpg" class="img-fluid rounded img-hover-shadow" alt="">

<!-- Bootstrap-only approach (no transitions) -->
<div class="shadow-sm shadow-hover shadow-lg-hover">
  <!-- Requires custom CSS for hover -->
</div>

Responsive Shadows

Breakpoint-based Shadows
Responsive Shadow Card

.shadow-none .shadow-sm-md .shadow-lg-lg

  • Mobile: No shadow
  • Tablet: Small shadow
  • Desktop: Large shadow
Shadow on Larger Screens Only
Desktop-Only Shadow

.shadow-none .shadow-lg-lg

Only shows large shadow on desktop screens

Mobile Shadow Only
.shadow-lg .shadow-lg-none

Large shadow on mobile only

Tablet Shadow Only
.shadow-none .shadow-md-lg .shadow-lg-none

Large shadow on tablet only

Responsive Shadow Table
BreakpointClass PrefixExample
Allshadow-.shadow-lg
≥576pxshadow-sm-.shadow-sm-lg
≥768pxshadow-md-.shadow-md-lg
≥992pxshadow-lg-.shadow-lg-none
≥1200pxshadow-xl-.shadow-xl-sm
<!-- Responsive shadow utilities -->
<div class="shadow-none shadow-md-sm shadow-lg-lg">
  <!-- 
    No shadow on mobile
    Small shadow on tablet
    Large shadow on desktop
  -->
</div>

<div class="shadow-lg shadow-md-none">
  <!-- 
    Large shadow on mobile
    No shadow on tablet+
  -->
</div>

<div class="shadow-sm shadow-lg-xl">
  <!-- 
    Small shadow on mobile
    Extra large shadow on desktop
  -->
</div>

<!-- All breakpoint variants -->
<div class="shadow-lg">Large always</div>
<div class="shadow-sm-lg">Large on sm+</div>
<div class="shadow-md-lg">Large on md+</div>
<div class="shadow-lg-lg">Large on lg+</div>
<div class="shadow-xl-lg">Large on xl+</div>

<div class="shadow-none">None always</div>
<div class="shadow-sm-none">None on sm+</div>
<div class="shadow-md-none">None on md+</div>

<!-- Practical examples -->
<!-- Mobile-optimized cards -->
<div class="card shadow-lg shadow-md-sm">
  <!-- More prominent on mobile, subtle on desktop -->
</div>

<!-- Desktop-optimized navigation -->
<nav class="navbar shadow-none shadow-lg-lg">
  <!-- No shadow on mobile, shadow on desktop -->
</nav>

<!-- Responsive buttons -->
<button class="btn btn-primary shadow shadow-lg-none">
  <!-- Shadow on mobile, none on desktop -->
</button>

<!-- Adaptive product cards -->
<div class="card shadow-sm shadow-md-lg shadow-xl-none">
  <!-- 
    Small on mobile
    Large on tablet
    None on large desktop
  -->
</div>

Custom Shadow Effects

Advanced Shadow Techniques
Inset Shadow
.shadow-inset

Inner shadow effect

Multiple Shadows
.shadow-multi

Layered shadow effect

Colored Shadow
.shadow-colored

Shadow with color tint

Directional Shadows
.shadow-top
.shadow-right
.shadow-bottom
.shadow-left
Shadow Utilities Table
Effect TypeCSS PropertyCustom ClassExample Value
Insetbox-shadow: inset.shadow-insetinset 0 2px 4px rgba(0,0,0,.1)
MultipleMultiple shadows.shadow-multiTwo layered shadows
ColoredRGBA with color.shadow-primaryrgba(13,110,253,.15)
DirectionalOffset shadows.shadow-topShadow on specific side
Blur-onlyNo offset.shadow-blur0 0 10px rgba(0,0,0,.1)
/* Custom shadow classes via SCSS */

// Inset shadow
.shadow-inset {
  box-shadow: inset 0 2px 4px rgba(0,0,0,.1) !important;
}

// Multiple shadows
.shadow-multi {
  box-shadow: 
    0 4px 6px rgba(0,0,0,.1),
    0 1px 3px rgba(0,0,0,.08) !important;
}

// Colored shadows using theme colors
.shadow-primary {
  box-shadow: 0 .5rem 1rem rgba(13, 110, 253, .15) !important;
}

.shadow-success {
  box-shadow: 0 .5rem 1rem rgba(25, 135, 84, .15) !important;
}

.shadow-danger {
  box-shadow: 0 .5rem 1rem rgba(220, 53, 69, .15) !important;
}

// Directional shadows
.shadow-top {
  box-shadow: 0 -4px 6px rgba(0,0,0,.1) !important;
}

.shadow-right {
  box-shadow: 4px 0 6px rgba(0,0,0,.1) !important;
}

.shadow-bottom {
  box-shadow: 0 4px 6px rgba(0,0,0,.1) !important;
}

.shadow-left {
  box-shadow: -4px 0 6px rgba(0,0,0,.1) !important;
}

// Blur-only shadow
.shadow-blur {
  box-shadow: 0 0 20px rgba(0,0,0,.1) !important;
}

// Glow effect
.shadow-glow {
  box-shadow: 0 0 20px rgba(13, 110, 253, .3) !important;
}

// Material Design elevation levels
.shadow-elevation-1 {
  box-shadow: 0 2px 1px -1px rgba(0,0,0,.2),
              0 1px 1px 0 rgba(0,0,0,.14),
              0 1px 3px 0 rgba(0,0,0,.12) !important;
}

.shadow-elevation-2 {
  box-shadow: 0 3px 3px -2px rgba(0,0,0,.2),
              0 3px 4px 0 rgba(0,0,0,.14),
              0 1px 8px 0 rgba(0,0,0,.12) !important;
}

// Add to Bootstrap utilities
$utilities: map-merge(
  $utilities,
  (
    "shadow-inset": (
      property: box-shadow,
      class: shadow-inset,
      values: (
        null: inset 0 2px 4px rgba(0,0,0,.1),
      )
    ),
    "shadow-primary": (
      property: box-shadow,
      class: shadow-primary,
      values: (
        null: 0 .5rem 1rem rgba(13, 110, 253, .15),
      )
    ),
  )
);

@import "bootstrap/scss/bootstrap";

Performance Considerations

Shadow Performance Tips
  • Avoid excessive shadows: Too many shadowed elements can impact rendering performance
  • Use simpler shadows: .shadow-sm performs better than .shadow-lg
  • Limit animated shadows: Animating box-shadow can be performance intensive
  • Consider alternatives: For many repeated shadowed elements, consider CSS filters or other techniques
  • Test on mobile: Shadows can impact mobile performance more than desktop
  • Use will-change: For animated shadows, add will-change: box-shadow
  • Optimize transitions: Use transform instead of box-shadow for hover effects when possible

Best Practices

Shadow Usage Guidelines
  • ✅ Use .shadow-sm for subtle elevation and depth
  • ✅ Use .shadow for default card elevation
  • ✅ Use .shadow-lg for modal dialogs and important elements
  • ✅ Use .shadow-none to remove shadows from Bootstrap components
  • ✅ Implement hover effects for interactive elements
  • ✅ Use responsive shadows for mobile optimization
  • ✅ Maintain consistency in shadow usage across your design system
  • ✅ Test shadow contrast for accessibility

Accessibility

Accessibility Considerations
  • Contrast: Ensure shadows don't reduce text contrast below WCAG standards
  • Focus indicators: Don't rely solely on shadows for focus states
  • Motion sensitivity: Provide reduced motion alternatives for animated shadows
  • Screen readers: Shadows don't affect screen reader announcements
  • Color blindness: Test shadow visibility for users with color vision deficiencies
  • High contrast mode: Shadows may be removed in Windows High Contrast Mode