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

推荐订阅源

F
Full Disclosure
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recorded Future
Recorded Future
Y
Y Combinator Blog
Cloudbric
Cloudbric
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cybersecurity and Infrastructure Security Agency CISA
TaoSecurity Blog
TaoSecurity Blog
Security Latest
Security Latest
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
C
Cisco Blogs
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
Vulnerabilities – Threatpost
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
A
About on SuperTechFans
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
C
CXSECURITY Database RSS Feed - CXSecurity.com
Schneier on Security
Schneier on Security
T
Tenable Blog
N
News and Events Feed by Topic
W
WeLiveSecurity
有赞技术团队
有赞技术团队
AI
AI
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
T
The Blog of Author Tim Ferriss
S
Security Affairs
Know Your Adversary
Know Your Adversary
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
Google DeepMind News
Google DeepMind News
The Cloudflare Blog

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 Tricks to add 3D Effects to your Text 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
A scalable CSS only Typewriter Effect
Temani Afif · 2021-08-28 · via CSS Articles by Temani Afif

You want a Typewriter effect for your text but you are lost within all the variations that use JS and the ones that use a complex CSS code.

Search no more! Here is a simple typewriter effect with only a few lines of CSS where you don't need to deal with any complex code. It's scalable and works with any kind of text.

See it in play:

How does it work?

The logic is pretty simple and relies on a basic HTML code:

<span class="type"><span>CSS typewriter</span></span> 

You put the text within two <span> and you are done. You don't need to deal with pseudo-element, duplicated texts, data-attribute, etc.

The CSS now:

.type {
  display:inline-flex;
}
.type span {
  word-break: break-all;
  height: 1.2em;
  width:0%;
  overflow: hidden;
  animation:t 2s linear infinite alternate;
}
.type span:before {
  content:" ";
  display:inline-block;
}
@keyframes t{
  90%,100% {width:100%}
}

No, I forget nothing. This is all the CSS needed for the typewriter effect.

3 tricks are used to achieve the final result:

1) cyclic percentage size

This is a powerful CSS quirk to calculate the width (or the height) of elements. I used a first <span> having display:inline-flex so its size depends on its content (it's an inline level element). The content inside (the other <span>) is using a percentage width so the width is based on its container. We have a cyclic calculation since each element depends on the other!

The Specification details such behavior. I don't recommend reading it because you will get lost (I had to read it 10 times to understand only a few cases).

I will try to use easy words to explain what is happening in our case.

  • First the browser will ignore the percentage width to define the width of the container. The child element will have width:auto. Our first span will have its width equal to the width of its child (so the width of the text).
  • Then the browser will get back to calculate the width of the child based on the width of the parent found previously BUT the width of the parent will not change again to not fall into an infinite loop.

In other words, the width:X% I am applying to the span is based on its own content that's why we don't need any complex calculation. Animating the element from 0% to 100% is all that we need.

This is what we get:

2) breaking the words

Now let's add word-break: break-all; to the previous code:

We are getting closer. Our text is wrapping letter by letter.

break-all

Breaking is allowed within “words” ref

3) fix the height to one line

The final trick is to set the height of the element to be equal to one line. By default, the height of a line is around 1.2xfont-size that's why I using 1.2em. You have to adjust this value based on your case or based on the value of line-height (it should be equal to line-height)

That's it! we have our typewriter effect.

Wait, there is an issue with the first letter!

Yes, good catch. That's why in the initial code I am adding:

.type span:before {
  content:" ";
  display:inline-block;
}

This will create an invisible first letter that will take the place of the real first letter.

✔️ No Javascript
✔️ A basic HTML code
✔️ No complex CSS code. Less than 10 declarations and no hard-coded values
✔️ Accessible. The text is written within the HTML code (no pseudo-element, no duplicated text)
✔️ You can use any text you want without changing the code.
✔️ Doesn't require monospace fonts
✔️ No browser support issue. All the properties I am using are supported by most of the browsers. We can also remove the use of flexbox: https://codepen.io/t_afif/pen/VwWvmxe
❌ Doesn't support multi-line of text. Well, I need one drawback 😜


What about the caret?

You can easily add one using box-shadow:


What about multiple texts?

The above was only the first part of the "real" typewriter effect I am aiming for.

See it in play:

The HTML code is still a basic one:

I am <span class="type">
  <span>
    <span>a CSS Hacker</span>
    <span>an expert web developer</span>
    <span>a lazy person!</span>
  </span>
</span>

The CSS:

.type {
  display:inline-block;
}
.type > span {
  display:grid;
  overflow: hidden;
  height:1.2em;
}
.type span span {
  width:0%;
  max-width:max-content;
  overflow: hidden;
  height:inherit;
  word-break:break-all;
  animation:
    c 0.5s infinite steps(1),  
    t 2s linear infinite alternate,
    m 12s steps(3) infinite;
}
.type span span:before {
  content:" ";
  display:inline-block;
}
@keyframes t{
  90%,100% {width:100%}
}
@keyframes c{
  0%,100%{box-shadow:5px 0 0 #0000}
  50%    {box-shadow:5px 0 0 red  }
}
@keyframes m{
  100% {transform:translateY(-300%)}
}

The trick is to make all the spans under each other (that's why I used display:grid on their parent container) so that the longest word will define the width of the main element.

Then each span will animate like previously. The only difference is that, with a small transform trick, I will show only one at a time.

If you remove the overflow:hidden from the second span you can see what is happening:

Notice the use of max-width:max-content to limit the caret to the width of the actual text and not the width of the main element.

You can also notice that 3 is used with many values:

  • 12s = 3*(2*2s)
  • steps(3)
  • translateY(-300%) = translateY(3*-100%)

Yes, that value can be a CSS variable. Our code will become scalable and we can easily add as much text as we want:

We have our scalable CSS only Typewriter Effect:

✔️ No Javascript
✔️ A basic HTML code
✔️ No complex CSS code
✔️ Accessible
✔️ Works with any text content
✔️ Can use any font
✔️ Scalable
✔️ No browser support issue