This article improves and simplifies the CSS text threading demonstration of Florin Pop. The original demonstration has problems such as text duplication, line length to magic value, and dependence on JavaScript. The solution in this article avoids these problems.
HTML structure
The original demo uses Splitting.js to split the title text into span elements of a single letter. This article uses HTML preprocessor to generate HTML directly, avoiding the need for text duplication and the use of additional libraries.
<h1 aria-label="We Love to Play" role="image"> <span class="letter">W</span> <span class="letter">e</span> <span class="letter"> </span> <span class="letter rev">L</span> <span class="letter">o</span> <span class="letter">v</span> <span class="letter">e</span> <span class="letter"> </span> <span class="letter">t</span> <span class="letter">o</span> <span class="letter"> </span> <span class="letter rev">P</span> <span class="letter">l</span> <span class="letter">a</span> <span class="letter">y</span> </h1>
To ensure accessibility, role="image"
and aria-label
attributes are set throughout the element.
Basic style
Center the title using grid layout:
body { display: grid; place-content: center; }
You can enhance your visual effects with custom fonts and backgrounds.
Create lines with absolutely positioned ::after
pseudo-element, with thickness (height) of $h
:
$h: .125em; $r: .5*$h; h1 { position: relative; &::after { position: absolute; top: calc(50% - #{$r}); right: 0; height: $h; border-radius: 0 $r $r 0; background: criminal; width: calc(50vw 50%); /* Calculate line width*/ animation: slide 2s ease-out 1s backwards; /* Add animation*/ } } @keyframes slide { 0% { transform: translateX(-100%); } }
The line width is calculated by calc(50vw 50%)
, ensuring that the line extends from the left side of the viewport to the right side of the title text.
Lines interspersed up and down
Control the display of letters above or below the line by randomly assigning .over
class:
.over { position: relative; z-index: 1; }
Using z-index
instead of translateZ
has better compatibility and easier implementation.
Animation effect
The animation is implemented using @keyframes slide
, and the lines slide in from the left. animation-fill-mode: backwards
ensures that the line before the animation starts is in the initial position.
3D effect
To achieve the effect of lines passing through each letter, 3D transformation is required:
- Set
transform-style: preserve-3d
on the title element. - Use the
.rev
class to control the rotation direction of letters about the Y-axis. - Set
display: flex
andwhite-space: pre
to solve the problem of invalid transformation of inline element and zero space element width. -
scalex
to offset horizontal compression caused by Y-axis rotation.
$a: 2deg; $f: 1/cos($a); .letter { white-space: pre; transform: rotateY($a) scaleX($f); } .rev { transform: rotateY(-$a) scaleX($f); }
The final effect enables the lines to pass through each letter with an animation effect. It should be noted that font selection will affect the final effect.
The above is the detailed content of Weaving a Line Through Text in CSS. For more information, please follow other related articles on the PHP Chinese website!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
