On this page10 sections
Lecture 035: Shadows & Text Effects
| Category | 7: Advanced Styling |
| Lecture | 035 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
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-shadowandbox-shadow, including several layers and inset shadows - Shorten overflowing text with
text-overflow: ellipsisandline-clamp - Control wrapping with
overflow-wrap,word-breakandtext-wrap
1. Text Shadow
text-shadow: x y blur colour;
Example
<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
<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:
<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 "…"
<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
<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
<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:
<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: balanceevens out line lengths, which suits headings.text-wrap: prettyavoids 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
- Write a soft grey text shadow.
- What does the fourth length in
box-shadow(the spread) do? - How do you make a shadow inside an element?
- Which three properties produce a one-line "…"?
- How do you limit a paragraph to three lines?
- Which
text-wrapvalue suits headings?
Practice Answers
text-shadow: 2px 2px 4px rgb(0 0 0 / 0.3);- It grows (or shrinks) the shadow before it's blurred.
- Add the
insetkeyword. white-space: nowrap; overflow: hidden; text-overflow: ellipsis;display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden;balance
Key Takeaways
text-shadowandbox-shadowtake x, y, blur (and spread) plus a colour; commas add layers.insetmakes inner shadows; soft, light shadows look most natural.- Ellipsis and
line-clampcut off long text neatly. overflow-wrap: anywhereandtext-wrap: balance / prettyfix awkward wrapping.