This article mainly introduces how to use CSS to realize the special effect of a button text sliding. It has a certain reference value. Now I share it with you. Friends in need can refer to the source code download
Please download from github download.
https://github.com/comehope/front-end-daily-challenges/tree/master/001-button-text-staggered-sliding-effects
Code interpretation
Define dom, define the text of the button in a container, each letter has a span, each span has a data-text attribute, its value is the same as the letter in the span:
<p> <span>B</span> <span>U</span> <span>T</span> <span>T</span> <span>O</span> <span>N</span> </p>
The button is centered:
html, body { height: 100%; display: flex; align-items: center; justify-content: center; }
Set the size and text style of the button:
.box { width: 200px; height: 60px; border: 2px solid black; text-align: center; font-size: 30px; line-height: 60px; font-family: sans-serif; }
Each letter of the button is set as an inline block element so that the animation can be set individually:
.box span { display: inline-block; color: blue; }
Stagger the letters Displayed outside the button container, the odd-numbered elements are displayed on top, and the even-numbered elements are displayed below:
.box span:nth-child(odd) { transform: translateY(-100%); } .box span:nth-child(even) { transform: translateY(100%); }
Use pseudo elements to add a copy for each letter:
.box span::before { content: attr(data-text); position: absolute; color: red; }
Let the pseudo elements The letters of the element are also staggered and positioned relative to their original elements:
.box span:nth-child(odd)::before { transform: translateY(100%); } .box span:nth-child(even)::before { transform: translateY(-100%); }
Add a mouse-over style to the button and set the easing time to animate it:
.box:hover span { transform: translateY(0); } .box span { transition: 0.5s; }
Finally, hide the container External content:
.box { overflow: hidden; }
Done!
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
CSS3 to realize vertical text arrangement
Introduction to CSS to create striped background styles for web pages
The above is the detailed content of How to use CSS to achieve a button text sliding effect. For more information, please follow other related articles on the PHP Chinese website!

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

SublimeText3 Chinese version
Chinese version, very easy to use

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