惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
O
OpenAI News
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
云风的 BLOG
云风的 BLOG
罗磊的独立博客
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
D
DataBreaches.Net
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
雷峰网
雷峰网
V
Visual Studio Blog
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
博客园 - 【当耐特】
G
Google Developers Blog
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
Martin Fowler
Martin Fowler
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
AI
AI
Google Online Security Blog
Google Online Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
Webroot Blog
Webroot Blog
PCI Perspectives
PCI Perspectives
爱范儿
爱范儿
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
CSS Tricks to add 3D Effects to your Text
2024-01-16 · via CSS Articles by Temani Afif

CSS Tricks to add 3D Effects to your Text

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In this article, we will explore a few tricks that allow you to add a touch of 3D to your text content. We won't use complex code and a ton of HTML, only a few lines of CSS and you can have the following result:

Multi-line 3D text

Creating a 3D box

Let’s start with the following HTML code

<span>A Title</span>

and here is the CSS to transform that single line of text into a 3D box:

span { 
  --d: .3em; /* control the depth */
  padding-block: .1em calc(.1em + var(--d));
  padding-inline:.2em calc(.2em + var(--d));
  background:
    conic-gradient(at right var(--d) bottom var(--d),
      #0004 37.5%,#0008 0 75%,#0000 0) 
    #d81a14; /* the main color */
  clip-path: polygon(0 0,calc(100% - var(--d)) 0,100% var(--d),100% 100%,var(--d) 100%, 0 calc(100% - var(--d)));
}

I am using a span to illustrate that the trick works with inline level elements but it can also be used with block level elements such as block and inline-block elements. In the end, we always have one 3D box with our text content.

See the Pen by Temani Afif () on .

Now let’s dissect the code. The first thing we are doing is defining the variable --d that will control the 3D effect. You can adjust the value to understand its effect. Then we define the padding.

Without the 3D effect our padding should be:

padding-block: .1em;
padding-inline: .2em;

But we need extra space on the right and bottom side

padding-block: .1em calc(.1em + var(--d)); /* top & bottom */
padding-inline:.2em calc(.2em + var(--d)); /* left & right */

.1em and .2em are arbitrary values so feel free to use whatever you want.

The next step is to add the gradient coloration

Oeverview of the gradient effect

We first apply a solid color as background-color and above it, we use a conic-gradient with three colors. A transparent one and two semi-transparent black to create the different shades of our 3D box.

background:
  conic-gradient(at right var(--d) bottom var(--d),
    #0004 37.5%,#0008 0 75%,#0000 0) 
  #d81a14; /* the main color */

We can use only the conic-gradient and define the final colors there but it requires us to manipulate three colors instead of only one.

The last step is to apply clip-path to cut to corners and get the final shape.

Overview of the clip-path

clip-path: 
  polygon(
   0 0, /* 1 */
   calc(100% - var(--d)) 0, /* 2 */
   100% var(--d), /* 3 */
   100% 100%, /* 4 */
   var(--d) 100%, /* 5 */
   0 calc(100% - var(--d)) /* 6 */
  );

That’s all. With only a few lines of CSS, you get a cool 3D effect. You can easily adjust the code to get a different variation.

See the Pen by Temani Afif () on .

I let you dissect the code as a small exercise to understand the changes I have made.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

Multi-line 3D effect

Let’s move to the interesting part. The previous effect works only with a single inline sentence or a block-level element but what we want is to support multi-line. We won’t wrap each line in a div or span but we will keep our code simple:

<span>A title <br> with many lines<br> of text</span>

I am using <br> to introduce line breaks but they can also occur naturally when the text doesn’t fit into one line.

If we apply the previous CSS we get the following:

It’s clearly broken but we can fix it with a few adjustments. First of all, clip-path doesn’t play well with inline elements having many lines so let’s remove it.

span { 
  --d: .3em; /* control the depth */
  line-height: 2; /* control the distance between lines */

  padding-block: .1em calc(.1em + var(--d));
  padding-inline:.2em calc(.2em + var(--d));
  background:
    conic-gradient(at right var(--d) bottom var(--d),
      #0004 37.5%,#0008 0 75%,#0000 0) 
    #d81a14; /* the main color */
}

I am introducing the line-height property to control the height of one line or, seen differently, the distance between lines.

A little better but still not there. It’s important to notice the effect of the background and how it splits between the lines, the same for the padding. We can control this effect using box-decoration-break: clone

Each box fragment is rendered independently with the specified border, padding, and margin wrapping each fragment. The border-radius, border-image, and box-shadow are applied to each fragment independently. The background is also drawn independently for each fragment, ref

We are getting closer. We are only missing the cut part that we did with clip-path previously. We are going to use mask for this. It relies on gradients so it will also get “cloned” for each line like the previous decorations:

mask: 
  linear-gradient(45deg,
    #0000   calc(       var(--d)*cos(45deg)),
    #000  0 calc(100% - var(--d)*cos(45deg)),
    #0000 0);

Our effect is now perfect. Here is the full demo with two variations

See the Pen by Temani Afif () on .

You can use whatever content you want and as many lines as you want. All you have to do is adjust a few values to control the color, the 3D depth, and the distance between the lines.

Let’s try another kind of 3D effect where the boxes stretch to the end of the line. This time we will work with block-level elements.

See the Pen by Temani Afif () on .

The logic of this effect is different and it’s all about repeating gradients following the y-axis and we will use the height of one line for this. Luckily the new lh is what we need here since 1lh is equal to one line of text (more precisely the height of the line box)

Let’s start by defining the parameters:

h2 { 
  --d: .3em; /* control the depth */
  --s: .2em; /* control the space between boxes */
  line-height: 2; /* control the height of the boxes */
}

Then we apply a background configuration similar to the previous effect:

Overview of the repeating gradient

background: 
  conic-gradient(at left var(--d) bottom var(--d),
    #0000 25%,#0008 0 62.5%,#0004 0) 0 0/100% 1lh 
  #2699DC

Note how the height of the gradient is equal to 1lh to make it repeat each line of text.

The next step is to apply a mask to create the space between the lines and at the same time cut the corner to create the 3D effect. Here is a figure to illustrate the gradient configuration of the mask:

Overview of the mask effect

A first gradient will cut the red part and a second one will cut the green part.

mask: 
 conic-gradient(from 90deg at var(--d) var(--s),
     #000 37.5%,#0000 0) 0     0      /51% 1lh repeat-y,
 conic-gradient(from 45deg at calc(100% - var(--d)) calc(100% - var(--s)),
     #0000 62.5%,#000 0) 100% var(--s)/51% 1lh repeat-y

We are almost good, we can already see our 3D effect across multiple lines:

Overview of the 3D effect

But if you look closely, you can notice that the text is not perfectly aligned inside the box. It’s closer to the bottom than the top. We need to offset the whole gradient configuration (background and mask). I will skip the math part and will give you the value of the offset which is (d - s)/2 and the full code will be:

h2 { 
  --d: .3em; /* control the depth */
  --s: .2em; /* control the space between boxes */
  line-height: 2; /* control the height of the boxes */

  padding-inline: calc(var(--d) + .2em) .2em;
  background: 
    conic-gradient(at var(--d) calc(100% - var(--d)),
      #0000 25%,#0008 0 62.5%,#0004 0) 0 calc((var(--d) - var(--s))/2)/100% 1lh 
    #2699dc; /* the main color */
  mask: 
    conic-gradient(from 90deg at var(--d) var(--s),
      #000 37.5%,#0000 0) 0    calc((var(--d) - var(--s))/2)/51% 1lh repeat-y,
    conic-gradient(from 45deg at calc(100% - var(--d)) calc(100% - var(--s)),
      #0000 62.5%,#000 0) 100% calc((var(--d) + var(--s))/2)/51% 1lh repeat-y;
}

The text is now centered within the boxes but the offset created another issue. The box of the last line is cut and we have a strange line at the top.

To fix the first issue we add a padding-bottom and for the second one, we use clip-path to hide the line at the top.

padding-bottom: calc(var(--d)/2);
clip-path: inset(calc(var(--d)/2) 0 0);

Now our effect is perfect. Here is the full demo again

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX HOSTING PLANS FOR WORDPRESS

with the discount code

AWESOME

Another 3D effect

let’s try a last one but this time we are going to put letters/numbers inside 3D boxes

As you can see in the code, we are not wrapping each letter inside a container. We are using the same technique as the previous effect but this time following the x-axis. Instead of “each line”, it’s “each letter” and for this, I will rely on the ch unit (instead of the lh). All the letters need to take up the same amount of space so only monospace fonts are suitable for this effect.

I won’t dig into the code because this will be your homework! Try to follow the same steps as I did previously and you will see that I am using almost the same tricks

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif