【CSS】span要素を使ったアニメーション実装
span要素はインライン要素ですが、CSSアニメーションと組み合わせることで、テキストの一部に動的な効果を追加できます。この記事では、display: inline-blockと@keyframesを活用して、span要素に様々なアニメーション効果を実装する方法を解説します。
このテキストがアニメーションしています
spanアニメーションの特徴
- 部分的なテキスト装飾:段落中の特定の単語だけに効果を適用可能
- 軽量な実装:JavaScript不要でCSSのみで実現
- 高い汎用性:ボタン、ラベル、警告表示など多様な場面で活用可能
🔹 基本コード
<span class="highlight">アニメーションされるテキスト</span>
<style>
.highlight {
animation: highlight 2s infinite;
display: inline-block; /* アニメーション可能にする */
}
@keyframes highlight {
0% { background-color: yellow; }
50% { background-color: orange; transform: scale(1.05); }
100% { background-color: yellow; }
}
</style>
🧩 アニメーションのカスタマイズ
@keyframesルールを変更することで、様々なアニメーション効果を作成できます。span要素は通常インライン要素ですが、display: inline-blockを設定することで幅・高さ・余白などのプロパティもアニメーション可能になります。
代表的なアニメーションパターン
/* 回転アニメーション - アイコンなどに最適 */
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* フェードイン - 注目させたい要素に */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
/* バウンス - クリックを促すボタンなど */
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
アニメーション設定のポイント
animation-iteration-count:infiniteで無限ループ、数値指定で繰り返し回数を設定animation-timing-function:ease, linear, ease-in-outなどで動きの質感を変更animation-delay:アニメーション開始を遅らせる
🎯 実践的な活用例と注意点
効果的な使用場面
1. 重要なキーワードの強調
契約書の重要な条項や金額表示など、ユーザーに確実に読んでほしい部分
2. 操作ガイド
フォームの必須項目や入力例を示す場合
3. ステータス表示
「新着」「限定」などのバッジ表現
注意すべきポイント
✗ アニメーションの過剰使用
多くの要素に同時にアニメーションを適用すると、ユーザーが混乱したり、重要な内容が目立たなくなる可能性があります。
✓ アクセシビリティ配慮
モーションに敏感なユーザーのために、@media (prefers-reduced-motion: reduce)でアニメーションを無効にするオプションを提供しましょう。
✗ パフォーマンス影響
box-shadowやfilterなど重いプロパティのアニメーションは、モバイルデバイスでパフォーマンス低下を引き起こす可能性があります。
💻 完全動作コード
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>spanアニメーションデモ</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
text-align: center;
}
.highlight {
animation: highlight 2s infinite;
padding: 0.2rem 0.4rem;
border-radius: 4px;
display: inline-block;
}
@keyframes highlight {
0% { background-color: #ffeb3b; }
50% { background-color: #ff9800; transform: scale(1.05); }
100% { background-color: #ffeb3b; }
}
</style>
</head>
<body>
<p>この<span class="highlight">テキスト</span>がアニメーションしています</p>
</body>
</html>
💯 完全動作コード(全アニメーション+インタラクティブ効果)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>spanアニメーション完全デモ</title>
<style>
body {
font-family: 'Arial', 'Meiryo', sans-serif;
line-height: 1.6;
padding: 20px;
max-width: 800px;
margin: 0 auto;
background: #f5f5f5;
}
.demo-section {
background: white;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
h2 {
color: #333;
border-bottom: 2px solid #eee;
padding-bottom: 10px;
}
/* 基本ハイライトアニメーション */
.highlight {
animation: highlight 2s infinite;
padding: 0.2rem 0.4rem;
border-radius: 4px;
display: inline-block;
}
@keyframes highlight {
0% { background-color: #ffeb3b; }
50% { background-color: #ff9800; transform: scale(1.05); }
100% { background-color: #ffeb3b; }
}
/* 回転アニメーション */
.spin {
animation: spin 3s linear infinite;
display: inline-block;
font-weight: bold;
color: #2196F3;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* フェードインアニメーション */
.fade-in {
animation: fadeIn 2s ease-in-out infinite alternate;
}
@keyframes fadeIn {
from { opacity: 0.3; }
to { opacity: 1; }
}
/* バウンスアニメーション */
.bounce {
animation: bounce 1.5s ease infinite;
display: inline-block;
color: #4CAF50;
font-weight: bold;
}
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
/* パルスアニメーション(実践例) */
.pulse {
animation: pulse 1.5s infinite;
color: #e91e63;
font-weight: bold;
padding: 0 5px;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
/* 警告表示 */
.warning {
animation: warning 1s infinite alternate;
background: #ff5722;
color: white;
padding: 2px 5px;
border-radius: 3px;
}
@keyframes warning {
from { background: #ff5722; }
to { background: #ff9800; }
}
/* インタラクティブホバー効果 */
.hover-effect {
display: inline-block;
padding: 5px 15px;
background: #4285f4;
color: white;
border-radius: 20px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
margin-left: 10px;
}
.hover-effect:hover {
background: #3367d6;
transform: translateY(-3px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.hover-effect:hover::after {
content: "クリックしてください!";
position: absolute;
top: -40px;
left: 50%;
transform: translateX(-50%);
background: #333;
color: white;
padding: 5px 10px;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
animation: fadeIn 0.3s ease-out;
}
.hover-effect:active {
transform: translateY(-1px) scale(0.98);
}
</style>
</head>
<body>
<div class="demo-section">
<h2>基本ハイライトアニメーション</h2>
<p>この<span class="highlight">テキスト</span>が背景色変化と拡大縮小のアニメーションをしています</p>
</div>
<div class="demo-section">
<h2>カスタムアニメーション例</h2>
<h3>回転アニメーション</h3>
<p><span class="spin">⚙️ 設定</span> アイコンが回転しています</p>
<h3>フェードインアニメーション</h3>
<p>重要な<span class="fade-in">メッセージ</span>をゆっくり表示</p>
<h3>バウンスアニメーション</h3>
<p>こちらを<span class="bounce">クリック</span>してください!</p>
</div>
<div class="demo-section">
<h2>実践的な活用例</h2>
<h3>限定特典の強調</h3>
<p>今すぐ<span class="pulse">限定特典</span>をご利用ください!</p>
<h3>警告表示</h3>
<p><span class="warning">⚠️ 重要</span>: 本日が申込期限です!</p>
<h3>インタラクティブ要素(改良版)</h3>
<p>マウスを乗せてみてください <span class="hover-effect">👉 ここ</span></p>
<ul>
<li>ホバーでボタンが浮き上がりツールチップ表示</li>
<li>クリックで押し込み効果</li>
<li>スムーズなトランジション</li>
</ul>
</div>
</body>
</html>
コピーしました!