Box Shadows and Text Shadows: Depth and Elevation
Box Shadows and Text Shadows: Depth and Elevation
Have you ever wondered how cards on modern websites look like they are floating gently above the screen, or how buttons appear to rise up toward you when you hover your mouse over them? 🪄
A computer screen is completely flat (2D). But by using Shadows, web designers create the illusion of physical Depth and 3D Elevation!
In this lesson, you will master:
- 1The 5 values of the
box-shadowformula - 2Creating soft, realistic card elevations (resting vs hovering)
- 3The
insetshadow for sunken or pressed effects - 4The
text-shadowproperty for glows and cinema titles
The Classroom Sunlight & Shadow Puppet Analogy ☀️
Think of the morning sunlight streaming through your classroom window:
+-------------------------------------------------------------------------+ | THE PHYSICS OF REAL-WORLD SHADOWS | +-------------------------------------------------------------------------+ | 1. HAND RESTING ON THE DESK (Low Elevation): | | • The shadow is tight, dark, and sharp (Blur: 2px). | | • The hand feels close to the table. | | | | 2. HAND LIFTED 1 FOOT IN THE AIR (High Elevation): | | • The shadow becomes much larger, softer, and diffused (Blur: 20px). | | • The hand feels like it is floating high above the table! | +-------------------------------------------------------------------------+
The 5 Values of box-shadow
A complete box-shadow declaration contains up to 5 values:
Let us break down each parameter:
- 1
offset-x(Horizontal Shift):
- Positive (
5px): Shadow moves to the right. - Negative (
-5px): Shadow moves to the left. 0px: Shadow is centered directly behind the box.
- 1
offset-y(Vertical Shift):
- Positive (
8px): Shadow moves downwards (standard for overhead light). - Negative (
-8px): Shadow moves upwards.
- 1
blur-radius(Fuzziness):
0px: Completely sharp, hard-edged shadow.16px: Soft, natural, diffused blur.
- 1
spread-radius(Optional Size Expansion):
- Expands the physical footprint of the shadow in all directions before blurring.
- 1
color:
- Always use semi-transparent
rgba()! (e.g.,rgba(0, 0, 0, 0.08)). Solid pure black (#000) looks harsh, fake, and dated.
The 3-Tier Modern Card Elevation System
Professional UI designers use a consistent elevation hierarchy to show importance:
The Interactive Hover Lift Effect ⭐
You can combine box-shadow with transform: translateY() to make cards smoothly float up when hovered:
The inset Shadow (Sunken / Pressed Effect)
By adding the keyword inset, the shadow is drawn inside the box instead of outside. This creates a carved-out, sunken effect, perfect for pressed buttons, search inputs, or well containers:
text-shadow (Glows & Title Effects)
Just like boxes, text can have its own drop shadow using text-shadow!
text-shadow: offset-x offset-y blur-radius color;
(Notice: text-shadow does NOT have a spread radius).
1. Subtle Title Depth:
2. Neon Sci-Fi Glow Effect:
By setting both offset-x and offset-y to 0 and using a bright neon color with blur:
Common Beginner Mistakes (Do's and Don'ts)
| What Beginners Do (Don't ❌) | What You Should Do Instead (Do ✅) | Why? |
|---|---|---|
Using solid black: box-shadow: 5px 5px 0 black; | Use rgba(0, 0, 0, 0.08). | Solid black shadows look like 1995 clip art. Soft translucent shadows mimic natural sunlight. |
Making shadows too dark (rgba(0,0,0,0.8)). | Keep alpha between 0.05 and 0.2. | Heavy dark shadows make your website look muddy and unrefined. |
Forgetting transition when changing shadow on hover. | Always add transition: all 0.2s ease;. | Without transition, the shadow will abruptly snap instead of smoothly gliding. |
Quick Revision Summary
- Shadows create the visual illusion of 3D depth and elevation on a 2D screen.
box-shadowformula:offset-x offset-y blur-radius spread-radius color.- Always use
rgba(0, 0, 0, 0.08)for realistic, soft shadows. - The
insetkeyword casts shadows inside the container for sunken buttons and search inputs. - Combine
box-shadowwithtransform: translateY(-4px)for an interactive hover float effect. text-shadowadds depth to text and creates neon glow effects.
Practice Quiz
Test your understanding with these multiple-choice questions:
1. In the declaration box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);, what does the value 8px represent?
A. Horizontal offset (offset-x) B. Vertical offset (offset-y) C. Blur radius D. Spread radius Answer: B Explanation: In the box-shadow syntax, the first value is offset-x (horizontal) and the second value is offset-y (vertical). 8px pushes the shadow 8 pixels downwards.
2. Which keyword is placed inside a box-shadow declaration to make the shadow appear inside the element instead of outside?
A. inside B. inner C. inset D. internal Answer: C Explanation: The inset keyword changes the shadow from an outer drop shadow to an inner shadow that sits inside the box's borders.
3. Why do professional web designers prefer rgba(0, 0, 0, 0.1) over solid black for card shadows?
A. rgba() compiles faster in the browser B. Translucent shadows blend softly with backgrounds, mimicking natural diffused sunlight C. Solid black is not allowed in modern CSS D. rgba() works without a graphics card Answer: B Explanation: Solid black creates a harsh, cartoonish outline. Low-alpha rgba() mimics real optical physics by letting background colors show through soft shadow edges.
4. What visual effect does text-shadow: 0 0 15px #38bdf8; produce on text?
A. The text is mirrored backwards B. A centered glowing neon halo appears around the letters C. The text is underlined in blue D. The text drops to the bottom of the page Answer: B Explanation: With zero horizontal and vertical offsets, the blur radiates symmetrically in all directions, producing a glowing neon aura.
5. Which parameter is present in box-shadow but completely absent from text-shadow?
A. Blur radius B. Color C. Spread radius D. Vertical offset Answer: C Explanation: text-shadow takes only x-offset, y-offset, blur-radius, and color. It does not support a spread-radius parameter.
Practice Challenge (Try It Yourself)
- 1Open your code editor and create
floating-card.html. - 2Build an interactive, floating project card with a smooth elevation hover state:
- 1Open the file in your browser and hover your mouse over the card. Feel how alive, responsive, and tactile the floating card elevation feels! 🎯
Styling Links with Pseudo-classes
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Border Radius: Rounded Corners, Pills, and Circular Avatars | Styling Links with Pseudo-classes |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.