⟨/⟩
SnipBox
← Voltar ao catálogo
Catálogo
›
Tipografia
›
CodePen Home Animating Text Justification
CodePen Home Animating Text Justification
Tipografia
HTML
CSS
JS
📅 07/04/2026
👁 10 visualizações
Visualização ao Vivo
🔄 Recarregar
HTML
📋 Copiar
<div class="scene"> <article class="board"> <h1 class="headline js-headline" aria-label="Building a better community for the creative industry"> BUILDING A <span data-accent>BETTER</span> <span data-accent>COMMUNITY</span> FOR THE CREATIVE INDUSTRY </h1> </article> </div> <!-- OKAY DEV - Branding --> <div class="okay-container"> <img id="okaydev-logo" src="https://files.okaydev.co/production/images/ui/logos/logo-okaydev-light.svg?dm=1729245907" alt="OKAY DEV logo"> <a href="https://okaydev.co/?ref=codepen-animating-text-justification" target="_blank" class="okay-description"> A design + development community </a> </div>
CSS
📋 Copiar
:root { --bg: #42ed91; --panel: #42ed91; --text: #ccffe4; --accent: #ffffff; } *, *::before, *::after { box-sizing: border-box; } html, body { min-height: 100svh; } body { margin: 0; display: grid; place-items: center; background: var(--bg); } .scene { padding: clamp(0.5rem, 1vw, 1rem); } .board { display: flex; align-items: center; width: min(1200px, 90vw); aspect-ratio: 2.5 / 1; border-radius: 1.5rem; padding: clamp(1.5rem, 4vw, 4rem); background: var(--panel); } .headline { width: 100%; margin: 0; color: var(--text); text-transform: uppercase; font-family: Impact, "Arial Narrow Bold", sans-serif; font-size: clamp(3rem, 9vw, 8rem); line-height: 0.9; letter-spacing: 0.01em; visibility: hidden; } .line { display: block; width: 100%; } .line + .line { margin-top: 0.02em; } .line-mask { display: block; width: 100%; overflow: hidden; } .word { display: inline-block; white-space: nowrap; color: var(--text); will-change: transform, color; backface-visibility: hidden; transform: translateZ(0); } .word--accent { color: var(--text); } @media (prefers-reduced-motion: reduce) { .word, .line, .line-mask, .headline { transition: none !important; animation: none !important; } } @media (max-width: 767px) { .board { aspect-ratio: auto; min-height: 280px; } .headline { font-size: clamp(2.2rem, 9vw, 3.5rem); line-height: 0.95; } } /* OKAY DEV - Branding & Link */ .okay-container { position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 9999; max-width: 200px; } #okaydev-logo { width: 100%; margin-bottom: 3px; user-select: none; } .okay-description { color: #000; text-decoration: none; text-transform: uppercase; background: rgba(#ccffe4, 0.75); padding: 3px 5px; border-radius: 2px; font-size: 9.3px; line-height: 1.75em; box-decoration-break: clone; -webkit-box-decoration-break: clone; } .okay-description:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
JavaScript
📋 Copiar
gsap.registerPlugin(SplitText); const headline = document.querySelector(".js-headline"); const originalHeadlineHTML = headline.innerHTML.trim(); const prefersReducedMotion = window.matchMedia( "(prefers-reduced-motion: reduce)" ); const mobileBreakpoint = window.matchMedia("(max-width: 767px)"); const COLORS = { text: "#ccffe4", accent: "#ffffff" }; const OPTIONS = { centerSingleWordLines: false, enableMobileJustifyAnimation: true, enableMobileLineAnimation: true }; let timeline; let splitInstance; let hasAnimated = false; function getAccentTexts() { const temp = document.createElement("div"); temp.innerHTML = originalHeadlineHTML; return [...temp.querySelectorAll("[data-accent]")] .map((el) => el.textContent.trim()) .filter(Boolean); } const accentTexts = getAccentTexts(); function shouldAnimateJustify() { return !mobileBreakpoint.matches || OPTIONS.enableMobileJustifyAnimation; } function shouldAnimateLines() { return !mobileBreakpoint.matches || OPTIONS.enableMobileLineAnimation; } function splitHeadline() { headline.innerHTML = originalHeadlineHTML; if (splitInstance) { splitInstance.revert(); } splitInstance = SplitText.create(headline, { type: "lines,words", linesClass: "line", wordsClass: "word", mask: "lines", autoSplit: true, onSplit(self) { return createAnimation(self); } }); } function applyAccents(self) { if (!accentTexts.length) return; const remaining = [...accentTexts]; self.words.forEach((word) => { const text = word.textContent.trim(); const matchIndex = remaining.indexOf(text); if (matchIndex !== -1) { word.classList.add("word--accent"); remaining.splice(matchIndex, 1); } }); } function measureJustify(self) { self.lines.forEach((line) => { const words = [...line.querySelectorAll(".word")]; gsap.set(words, { x: 0 }); if (!words.length) return; const lineRect = line.getBoundingClientRect(); const rects = words.map((word) => word.getBoundingClientRect()); if (words.length === 1) { const word = words[0]; const rect = rects[0]; const currentLeft = rect.left - lineRect.left; word.dataset.x = OPTIONS.centerSingleWordLines ? (lineRect.width - rect.width) / 2 - currentLeft : 0; return; } const totalWidth = rects.reduce((sum, rect) => sum + rect.width, 0); const availableSpace = Math.max(0, lineRect.width - totalWidth); const gap = availableSpace / (words.length - 1); let runningX = 0; words.forEach((word, index) => { const currentLeft = rects[index].left - lineRect.left; word.dataset.x = runningX - currentLeft; runningX += rects[index].width + gap; }); }); } function setEndState(self) { const accents = [...headline.querySelectorAll(".word--accent")]; gsap.set(self.lines, { yPercent: 0 }); gsap.set(self.words, { x: (_, el) => Number(el.dataset.x || 0), color: COLORS.text }); if (accents.length) { gsap.set(accents, { color: COLORS.accent }); } gsap.set(headline, { visibility: "visible" }); } function setupState(self) { applyAccents(self); measureJustify(self); } function setIntroStartState(self) { const animateLines = shouldAnimateLines(); const animateJustify = shouldAnimateJustify(); gsap.set(self.lines, { yPercent: animateLines ? 110 : 0 }); gsap.set(self.words, { x: animateJustify ? 0 : (_, el) => Number(el.dataset.x || 0), color: COLORS.text }); } function animateIn(self) { if (timeline) timeline.kill(); const accents = [...headline.querySelectorAll(".word--accent")]; const animateLines = shouldAnimateLines(); const animateJustify = shouldAnimateJustify(); setIntroStartState(self); gsap.set(headline, { visibility: "visible" }); if (!animateLines && !animateJustify) { setEndState(self); hasAnimated = true; return; } timeline = gsap.timeline({ defaults: { ease: "expo" } }); if (animateLines) { timeline.to(self.lines, { yPercent: 0, duration: 0.8, stagger: 0.15 }); } if (animateJustify) { timeline.to( self.words, { x: (_, el) => Number(el.dataset.x || 0), duration: 0.4, stagger: 0.02, ease: "power4" }, animateLines ? "-=0.5" : 0 ); } if (accents.length) { let accentPosition = 0; if (animateLines && animateJustify) accentPosition = "-=0.1"; else if (animateLines && !animateJustify) accentPosition = "-=0.35"; else if (!animateLines && animateJustify) accentPosition = "-=0.1"; timeline.to( accents, { color: COLORS.accent, duration: 0.8, ease: "power2", stagger: 0.04 }, accentPosition ); } hasAnimated = true; return timeline; } function createAnimation(self) { setupState(self); if (prefersReducedMotion.matches || hasAnimated) { setEndState(self); hasAnimated = true; return; } gsap.set(headline, { visibility: "hidden" }); requestAnimationFrame(() => { requestAnimationFrame(() => { if (!splitInstance) return; animateIn(self); }); }); } function build() { if (timeline) { timeline.kill(); timeline = null; } gsap.set(headline, { visibility: "hidden" }); if (splitInstance) { splitInstance.revert(); splitInstance = null; } splitHeadline(); } prefersReducedMotion.addEventListener("change", build); mobileBreakpoint.addEventListener("change", build); build();