Unlock Advanced CSS Shadow Tricks: From Single‑Side to 3D Effects
This article explores a wide range of CSS shadow techniques—including single‑side box‑shadow, background animations, 3D pseudo‑element shadows, multi‑layer text shadows, long shadows, colorful gradients, filter‑based shadows, and neon effects—providing code samples and practical tips for modern front‑end development.
Single‑Side Shadow
By setting only one offset in
box-shadowyou can create a shadow on a single side. Example:
<code>box-shadow: 1px 0 3px #333;</code>Negative spread values and matching blur radius can hide the shadow unless an offset is applied.
Shadow Background / Background Animation
Multiple
box-shadowlayers can be animated, while
background‑image: linear‑gradient()cannot be interpolated.
Example of using
box-shadowto simulate a background image:
<code>.shadow { position: relative; width: 250px; height: 250px; }
.shadow::before { content: ""; position: absolute; top: -50px; left: -50px; width: 100%; height: 100%; box-shadow: 0 0 0 #000, 50px 50px #000; }</code>3D (Stereoscopic) Shadow
Using pseudo‑elements, rotate and position them to create a 3‑D look. The key is to generate a sibling element with
transformand apply a shadow.
<code>div { position: relative; width: 600px; height: 100px; background: hsl(48,100%,50%); border‑radius: 20px; }
div::before { content: ""; position: absolute; top: 50%; left: 5%; right: 5%; bottom: 0; background: hsl(48,100%,20%); transform: translate(0,-15%) rotate(-4deg); box-shadow: 0 0 20px 15px hsl(48,100%,20%); }</code>Text Long Shadow
Stacking many
text-shadowlayers creates a long, progressive shadow. A SASS function can generate 50 layers automatically.
<code>@function makeLongShadow($color) { $val: 0px 0px $color; @for $i from 1 through 50 { $val: #{$val}, #{$i}px #{$i}px $color; } @return $val; }
.div { text-shadow: makeLongShadow(hsl(14,100%,30%)); }</code>Colored Shadow
Although
box-shadowand
text-shadoware single‑color, a blurred
filter: blur()on a duplicated element can simulate a gradient‑colored shadow.
<code>.avatar { position: relative; background: url($img) no‑repeat center; background-size: 100% 100%; }
.avatar::after { content: ""; position: absolute; top: 10%; width: 100%; height: 100%; background: inherit; filter: blur(10px) brightness(80%) opacity(.8); z-index: -1; }</code>Neon and Light Effects
Multiple large
box-shadowlayers with bright colors produce a neon glow around text.
<code>.neon { color: #fff; text-shadow: 0 0 5px #0ff, 0 0 10px #0ff, 0 0 20px #0ff, 0 0 40px #0ff; }</code>Single‑Tag TikTok Logo
Combining a base
divwith two pseudo‑elements, using
filter: drop‑shadow()for one side and
box-shadowfor the other, reproduces the TikTok logo shape.
<code>div { position: relative; width: 37px; height: 218px; background: #fff; filter: drop‑shadow(-10px -10px 0 #24f6f0) contrast(150%) brightness(110%); box‑shadow: 11.6px 10px 0 #fe2d52; }
/* pseudo‑elements create the curved parts */</code>Update (2018‑11‑09)
The logo can also be built with two overlapping “J” shapes using
mix‑blend‑modefor the correct color blending.
<code>.logo { mix‑blend‑mode: screen; /* ... */ }</code>These techniques demonstrate how CSS shadows can be extended beyond simple depth cues to create complex visual effects without images or JavaScript.
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.