もくじ
CSSのanimationを使って流れるテキストを実装する方法を紹介します。
テキストの長さやコンテンツ幅によって、animationのスピードを調整する必要があります。
記事後半では、JavaScriptを使用してanimationのスピードを調整する方法も紹介しています。
CSSで左から右にテキストを流す
サンプル
実装コード
テキストの長さやコンテンツ幅に合わせてanimation-duration: 10s;部分を調整して、アニメーションのスピードを調整してください。
<div class="horizontalFlowing"><div class="horizontalFlowing_content -left-to-right">テキスト</div></div>
.horizontalFlowing {padding: 15px 0;display: flex;align-items: center;overflow: hidden;background: #f9e1de;}.horizontalFlowing_content.-left-to-right {display: inline-block;padding-left: 100%;white-space: nowrap;animation-name: flows-from-left-to-right;animation-duration: 10s; /* アニメーションのスピード */animation-iteration-count: infinite;animation-timing-function: linear;animation-delay: 0s;animation-fill-mode: both;}@keyframes flows-from-left-to-right {from {transform: translateX(-100%);}to {transform: translateX(0);}}
CSSで右から左にテキストを流す
サンプル
実装コード
テキストの長さやコンテンツ幅に合わせてanimation-duration: 10s;部分を調整して、アニメーションのスピードを調整してください。
<div class="horizontalFlowing"><div class="horizontalFlowing_content -right-to-left">テキスト</div></div>
.horizontalFlowing {padding: 15px 0;display: flex;align-items: center;overflow: hidden;background: #f9e1de;}.horizontalFlowing_content.-right-to-left {display: inline-block;padding-left: 100%;white-space: nowrap;animation-name: flows-from-right-to-left;animation-duration: 10s; /* アニメーションのスピード */animation-iteration-count: infinite;animation-timing-function: linear;animation-delay: 0s;animation-fill-mode: both;}@keyframes flows-from-right-to-left {from {transform: translateX(0);}to {transform: translateX(-100%);}}
CSSで上から下にテキストを流す
サンプル
実装コード
.verticalFlowingと.verticalFlowing_contentのheightは、同じ値にする必要があります。
テキストの高さやコンテンツの高さに合わせてanimation-duration: 5s;部分を調整して、アニメーションのスピードを調整してください。
<div class="verticalFlowing"><div class="verticalFlowing_content -top-to-bottom">テキスト</div></div>
.verticalFlowing {height: 200px;text-align: center;overflow: hidden;background: #f9e1de;}.verticalFlowing_content.-top-to-bottom {display: inline-block;padding-top: 200px;white-space: nowrap;animation-name: flows-from-top-to-bottom;animation-duration: 5s; /* アニメーションのスピード */animation-iteration-count: infinite;animation-timing-function: linear;animation-delay: 0s;animation-fill-mode: both;}@keyframes flows-from-top-to-bottom {from {transform: translateY(-100%);}to {transform: translateY(0);}}
CSSで下から上にテキストを流す
サンプル
実装コード
.verticalFlowingと.verticalFlowing_contentのheightは、同じ値にする必要があります。
テキストの高さやコンテンツの高さに合わせてanimation-duration: 5s;部分を調整して、アニメーションのスピードを調整してください。
<div class="verticalFlowing"><div class="verticalFlowing_content -bottom-to-top">テキスト</div></div>
.verticalFlowing {height: 200px;text-align: center;overflow: hidden;background: #f9e1de;}.verticalFlowing_content.-bottom-to-top {display: inline-block;padding-top: 200px;white-space: nowrap;animation-name: flows-from-bottom-to-top;animation-duration: 5s; /* アニメーションのスピード */animation-iteration-count: infinite;animation-timing-function: linear;animation-delay: 0s;animation-fill-mode: both;}@keyframes flows-from-bottom-to-top {from {transform: translateY(0);}to {transform: translateY(-100%);}}
CSSで無限に流れるテキスト
サンプル
実装コード
コンテンツ幅に合わせて、テキストが途切れないように.horizontalFlowing_content-txtのdivタグの数を調整する必要があります。
要素が増えるとスピードが速くなるため、animation-duration: 10s;部分を修正して、アニメーションのスピードも調整が必要です。
<div class="horizontalFlowing"><div class="horizontalFlowing_content -loop-right-to-left"><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div></div><div class="horizontalFlowing_content -loop-right-to-left"><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div><div class="horizontalFlowing_content-txt">テキスト</div></div></div>
.horizontalFlowing {padding: 15px 0;display: flex;align-items: center;overflow: hidden;background: #f9e1de;}.horizontalFlowing_content.-loop-right-to-left {white-space: nowrap;animation-name: loop-right-to-left;animation-duration: 10s; /* アニメーションのスピード */animation-iteration-count: infinite;animation-timing-function: linear;animation-delay: 0s;animation-fill-mode: both;}.horizontalFlowing_content-txt {padding: 0 20px;display: inline-block;}@keyframes loop-right-to-left {from {transform: translateX(0);}to {transform: translateX(-100%);}}
CSSのanimationをホバー時に停止
サンプル
実装コード
<div class="horizontalFlowing -stop-hover"><div class="horizontalFlowing_content -left-to-right">テキスト</div></div>
.horizontalFlowing {padding: 15px 0;display: flex;align-items: center;overflow: hidden;background: #f9e1de;}.horizontalFlowing.-stop-hover:hover .horizontalFlowing_content {animation-play-state: paused;}.horizontalFlowing_content.-left-to-right {display: inline-block;padding-left: 100%;white-space: nowrap;animation-name: flows-from-left-to-right;animation-duration: 10s; /* アニメーションのスピード */animation-iteration-count: infinite;animation-timing-function: linear;animation-delay: 0s;animation-fill-mode: both;}@keyframes flows-from-left-to-right {from {transform: translateX(-100%);}to {transform: translateX(0);}}
JavaScriptでコンテンツサイズが変わってもanimationをスピードを一定にする
サンプル
実装コード
以下の実装コードでは、1秒で180px移動するようになっています。
必要に応じて180部分を適宜変更してください。
<div class="horizontalFlowing js-flowing"><div class="horizontalFlowing_content -left-to-right js-flowing-content">テキスト</div></div>
.horizontalFlowing {padding: 15px 0;display: flex;align-items: center;overflow: hidden;background: #f9e1de;}.horizontalFlowing.-stop-hover:hover .horizontalFlowing_content {animation-play-state: paused;}.horizontalFlowing_content.-left-to-right {display: inline-block;padding-left: 100%;white-space: nowrap;animation-name: flows-from-left-to-right;animation-iteration-count: infinite;animation-timing-function: linear;animation-delay: 0s;animation-fill-mode: both;}@keyframes flows-from-left-to-right {from {transform: translateX(-100%);}to {transform: translateX(0);}}
// animationのスピード調整function adjustAnimationSpeed() {let flowingContainer = document.querySelector('.js-flowing');let flowingContent = document.querySelector('.js-flowing-content');let flowingContainerWidth = flowingContainer.offsetWidth;let flowingContentWidth = flowingContent.offsetWidth;let totalWidth = flowingContainerWidth + flowingContentWidth;let speed = totalWidth / 180; // 例として1 秒で180px移動しますflowingContent.style.animationDuration = speed + 's';}// DOM読み込み後に実行document.addEventListener('DOMContentLoaded', adjustAnimationSpeed);// 画面サイズが変更された時に実行window.addEventListener('resize', adjustAnimationSpeed);
HTML・CSS入門におすすめの1冊

もみじ
Web製作未経験でも読みやすい構成で、初学者でも安心して学習することができます。
実践的な内容を学ぶことができ解説も丁寧で分かりやすく、基礎的な内容はこの1冊で学ぶことができます。