On this page10 sections
  1. Learning Objectives
  2. 1. Text Shadow
  3. 2. Box Shadow
  4. 3. Cutting Off Long Text
  5. 4. Long Words and Wrapping
  6. 5. Nicer Headings: text-wrap
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 035: Shadows & Text Effects

Category7: Advanced Styling
Lecture035 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelIntermediate

Shadows add depth: a card that lifts off the page, a glow around a button, text that stands out from a photo. This lecture also covers text effects: cutting off long text with "…", wrapping long words, and balancing headings.


Learning Objectives

  • Add text-shadow and box-shadow, including several layers and inset shadows
  • Shorten overflowing text with text-overflow: ellipsis and line-clamp
  • Control wrapping with overflow-wrap, word-break and text-wrap

1. Text Shadow

text-shadow: x y blur colour;

Example

html
<style>
  h2 { font: 800 32px system-ui, sans-serif; margin: 12px 0; }
</style>

<h2 style="text-shadow: 2px 2px 0 #adb5bd;">Hard shadow</h2>
<h2 style="text-shadow: 2px 2px 6px rgb(0 0 0 / 0.4);">Soft shadow</h2>
<h2 style="color: white; background: #212529; padding: 8px; text-shadow: 0 0 8px #4dabf7, 0 0 18px #4dabf7;">Neon glow</h2>
<h2 style="color: white; text-shadow: -1px -1px 0 #1971c2, 1px -1px 0 #1971c2, -1px 1px 0 #1971c2, 1px 1px 0 #1971c2;">Outlined</h2>

Several shadows are separated by commas.


2. Box Shadow

box-shadow: x y blur spread colour;

Example

html
<style>
  .card { display: inline-block; width: 140px; padding: 18px; margin: 14px; border-radius: 12px; background: white; font: 13px system-ui; }
</style>

<div class="card" style="box-shadow: 0 1px 3px rgb(0 0 0 / 0.2);">Subtle</div>
<div class="card" style="box-shadow: 0 10px 30px rgb(0 0 0 / 0.18);">Floating</div>
<div class="card" style="box-shadow: 0 0 0 4px #ffd43b;">Spread only (a ring)</div>
<div class="card" style="box-shadow: inset 0 2px 6px rgb(0 0 0 / 0.25);">Inset (pressed)</div>

Example: lift a card on hover:

html
<style>
  .lift { width: 220px; padding: 18px; border-radius: 12px; background: white; box-shadow: 0 2px 6px rgb(0 0 0 / 0.12);
          transition: transform 0.2s, box-shadow 0.2s; margin: 16px; }
  .lift:hover { transform: translateY(-4px); box-shadow: 0 14px 30px rgb(0 0 0 / 0.18); }
</style>

<div class="lift">Hover to lift me</div>

3. Cutting Off Long Text

One line, ending with "…"

html
<p style="width: 220px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border: 1px solid #ced4da; padding: 6px;">
  This is a very long product title that won't fit on a single line
</p>

All three are needed: white-space: nowrap, overflow: hidden and text-overflow: ellipsis.

Several lines with line-clamp

html
<p style="width: 260px; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden;">
  This description is cut off after two lines, which keeps cards in a grid the same height
  no matter how long each description is.
</p>

4. Long Words and Wrapping

Example

html
<style>
  .box { width: 160px; border: 1px solid #ced4da; padding: 6px; margin-bottom: 8px; }
</style>

<div class="box">Overflow: supercalifragilisticexpialidocious</div>
<div class="box" style="overflow-wrap: anywhere;">overflow-wrap: supercalifragilisticexpialidocious</div>
<div class="box" style="word-break: break-all;">word-break: break-all also splits ordinary words</div>

overflow-wrap: anywhere only breaks a word when it really doesn't fit, which makes it the best choice for long URLs.


5. Nicer Headings: text-wrap

Example: drag the preview corner:

html
<h2 style="max-width: 420px;">A long heading that breaks awkwardly leaving one word alone</h2>
<h2 style="max-width: 420px; text-wrap: balance;">A long heading that breaks awkwardly leaving one word alone</h2>
  • text-wrap: balance evens out line lengths, which suits headings.
  • text-wrap: pretty avoids a single word alone on the last line, which suits paragraphs.

Common Beginner Mistakes

1. Dark, heavy shadows. Real shadows are soft. Use low-opacity black with plenty of blur.

2. Ellipsis that doesn't appear. You need all three properties, plus a width.

3. Animating box-shadow on many elements. It can be slow; animate transform and keep the shadow change small.


Practice Questions

  1. Write a soft grey text shadow.
  2. What does the fourth length in box-shadow (the spread) do?
  3. How do you make a shadow inside an element?
  4. Which three properties produce a one-line "…"?
  5. How do you limit a paragraph to three lines?
  6. Which text-wrap value suits headings?

Practice Answers

  1. text-shadow: 2px 2px 4px rgb(0 0 0 / 0.3);
  2. It grows (or shrinks) the shadow before it's blurred.
  3. Add the inset keyword.
  4. white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  5. display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden;
  6. balance

Key Takeaways

  • text-shadow and box-shadow take x, y, blur (and spread) plus a colour; commas add layers.
  • inset makes inner shadows; soft, light shadows look most natural.
  • Ellipsis and line-clamp cut off long text neatly.
  • overflow-wrap: anywhere and text-wrap: balance / pretty fix awkward wrapping.