【CSS】@starting-style の使い方!display: none やDOM追加時に transition を効かせる方法
CSSで要素をふわっと表示させたい時、display: none; から display: block; へクラスを切り替えただけでは transition が効かず、パッと一瞬で表示されてしまって困った経験はありませんか? また、JavaScriptで後からHTML要素を生成して画面に追加(append)した際にも、同様にそのままでは transition による登場アニメーションは動きません。
これまでこうした場面では、CSSの @keyframes(animation)を使うか、JavaScriptの setTimeout で数ミリ秒遅らせてクラスを付与するといった少し回りくどい工夫が必要でした。
しかし、モダンCSSの新機能である @starting-style を使えば、要素が画面に初めて描画される瞬間の「開始スタイル(初期値)」を定義できるため、普段通りのシンプルな transition だけで登場アニメーションが実装できるようになります! 今回は、@starting-style の基本的な仕組みと、3つの実践パターンを体験できるデモをご紹介します!
デモ動作
See the Pen @starting-style!display:noneやDOM追加時にtransitionを効かせる使い方 by mikanbako (@mikanbako) on CodePen.
@starting-styleの使い方!demo: display:noneやDOM追加時にtransitionを効かせる方法
実装コード紹介
【HTML】
<div class="l-wrap">
<h2>CSS @starting-style 活用パターン体験デモ</h2>
<p class="p-desc">従来は transition が効かなかった3つの場面で、<br>@starting-style の効果を確認しよう!</p>
<div class="p-demo-box">
<!-- 💡 パターン1:display: none からの表示切り替え比較 -->
<section class="p-section">
<h3 class="c-sec-title">① display: none ➔ block の表示切り替え比較</h3>
<p class="c-sec-desc">どちらも同じ <code>transition: 0.5s</code> を指定していますが、<code>@starting-style</code> の有無で挙動が変わります。</p>
<button class="c-btn c-btn--primary js-toggle-btn" type="button">
🔄 表示 / 非表示 を切り替える
</button>
<div class="c-compare-grid">
<!-- ❌ @starting-style なし -->
<div class="c-compare-col">
<div class="c-col-label">❌ @starting-style なし</div>
<div class="c-box c-box--no-start is-shown js-toggle-box">
パッと瞬間的に<br>表示されます
</div>
</div>
<!-- ⭕ @starting-style あり -->
<div class="c-compare-col">
<div class="c-col-label">⭕ @starting-style あり</div>
<div class="c-box c-box--with-start is-shown js-toggle-box">
初期値からふわっと<br>transitionします✨
</div>
</div>
</div>
</section>
<!-- 💡 パターン2:JSで動的にDOM要素を追加(append)する場合 -->
<section class="p-section">
<h3 class="c-sec-title">② JSで後からDOM要素を追加(createElement)する場合</h3>
<p class="c-sec-desc">DOMツリーに追加された瞬間、<code>@starting-style</code> の初期値から自動でアニメーションします。</p>
<div class="c-btn-row">
<button class="c-btn c-btn--accent js-add-item" type="button">➕ 要素を新しく追加する</button>
<button class="c-btn c-btn--sub js-clear-items" type="button">🗑️ リセット</button>
</div>
<div class="c-item-list js-item-list">
<div class="c-added-item">最初からある要素 #1</div>
</div>
</section>
<!-- 💡 パターン3:<dialog> 要素への活用 -->
<section class="p-section">
<h3 class="c-sec-title">③ 応用:<dialog> モーダルを transition だけで開く</h3>
<p class="c-sec-desc"><code>@keyframes</code> を使わず、本体と <code>::backdrop</code> に <code>@starting-style</code> を設定しています。</p>
<button class="c-btn c-btn--primary js-open-dialog" type="button">
dialogモーダルを開く
</button>
<dialog class="c-dialog js-dialog">
<div class="c-dialog__inner">
<h4>✨ transition だけで開きました!</h4>
<p><code>dialog[open]</code> と <code>dialog[open]::backdrop</code> の中に <code>@starting-style</code> を書くだけで、キーフレームアニメーション無しで滑らかに表示できます。</p>
<button class="c-btn c-btn--sub js-close-dialog" type="button">閉じる</button>
</div>
</dialog>
</section>
</div>
</div>
【CSS】
/* ベース装飾 */
.l-wrap {
padding: 30px 15px;
max-width: 650px;
margin: 0 auto;
color: #3e3e3e;
text-align: center;
}
h2 {
margin-bottom: 10px;
font-size: 16px;
color: #1bb4d4;
}
.p-desc {
margin-bottom: 25px;
font-size: 16px;
font-weight: bold;
color: #666;
line-height: 1.6;
}
.p-demo-box {
display: flex;
flex-direction: column;
gap: 30px;
padding: 30px;
text-align: left;
background: #fff;
border-radius: 12px;
box-shadow: 0 4px 15px rgb(0 0 0 / 5%);
}
.p-section {
padding: 20px;
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 8px;
}
.c-sec-title {
padding-left: 10px;
margin-bottom: 10px;
font-size: 16px;
color: #1bb4d4;
border-left: 4px solid #1bb4d4;
}
.c-sec-desc {
margin-bottom: 15px;
font-size: 14px;
color: #666;
line-height: 1.5;
}
.c-sec-desc code {
padding: 2px 6px;
font-weight: bold;
color: #e83e8c;
background: #e9ecef;
border-radius: 4px;
}
/* ボタン共通 */
.c-btn {
padding: 10px 18px;
font-family: inherit;
font-size: 14px;
font-weight: bold;
color: #fff;
cursor: pointer;
border: none;
border-radius: 6px;
transition: 0.2s;
}
.c-btn:hover {
opacity: 0.85;
transform: translateY(-1px);
}
.c-btn--primary {
background: #1bb4d4;
}
.c-btn--accent {
background: #e83e8c;
}
.c-btn--sub {
background: #6c757d;
}
.c-btn-row {
display: flex;
gap: 10px;
margin-bottom: 15px;
}
/* =========================================
💡 パターン1:display: none からの表示比較
========================================= */
.c-compare-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
min-height: 115px;
margin-top: 15px;
}
.c-compare-col {
padding: 12px;
text-align: center;
background: #fff;
border: 1px solid #dee2e6;
border-radius: 8px;
}
.c-col-label {
margin-bottom: 10px;
font-size: 14px;
font-weight: bold;
color: #555;
}
.c-box {
display: none; /* 初期状態は非表示 */
padding: 15px 10px;
font-size: 14px;
font-weight: bold;
color: #fff;
border-radius: 8px;
}
.c-box.is-shown {
display: block;
/* ゴール地点(表示完了時)のスタイル */
opacity: 1;
transform: translateY(0) scale(1);
transition:
opacity 0.5s ease,
transform 0.5s ease;
}
/* ❌ @starting-style なし(開始スタイルが分からないため、いきなり opacity: 1 で描画される) */
.c-box--no-start {
background: #6c757d;
}
/* ⭕ @starting-style あり(display: block になった瞬間のスタート地点を定義!) */
.c-box--with-start {
background: #1bb4d4;
}
.c-box--with-start.is-shown {
@starting-style {
opacity: 0;
transform: translateY(15px) scale(0.9);
}
}
/* =========================================
💡 パターン2:JSで動的に追加される要素
========================================= */
.c-item-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.c-added-item {
padding: 8px 14px;
font-size: 14px;
font-weight: bold;
color: #d6336c;
background: #fff0f6;
border: 2px solid #e83e8c;
border-radius: 20px;
/* ゴール地点(通常時)のスタイル */
opacity: 1;
transform: scale(1);
transition:
opacity 0.3s ease,
transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
/* 💡 DOMに追加された瞬間のスタート地点! */
@starting-style {
opacity: 0;
transform: scale(0.5);
}
}
/* =========================================
💡 パターン3:<dialog> 要素への適用
========================================= */
.c-dialog {
width: 90%;
max-width: 420px;
padding: 0;
border: none;
border-radius: 12px;
box-shadow: 0 10px 30px rgb(0 0 0 / 20%);
}
.c-dialog[open] {
/* 開いた状態のゴール地点 */
opacity: 1;
transform: translateY(0);
transition:
opacity 0.3s ease,
transform 0.3s ease;
/* モーダルが開いた瞬間のスタート地点 */
@starting-style {
opacity: 0;
transform: translateY(20px);
}
}
/* 背景(::backdrop)も transition と @starting-style でふわっと表示! */
.c-dialog::backdrop {
background: rgb(0 0 0 / 60%);
}
.c-dialog[open]::backdrop {
opacity: 1;
transition: opacity 0.3s ease;
@starting-style {
opacity: 0;
}
}
.c-dialog__inner {
padding: 25px;
text-align: center;
}
.c-dialog__inner h4 {
margin-bottom: 10px;
font-size: 18px;
color: #1bb4d4;
}
.c-dialog__inner p {
margin-bottom: 20px;
font-size: 14px;
line-height: 1.5;
color: #555;
text-align: left;
}
.c-dialog__inner code {
padding: 2px 6px;
font-weight: bold;
color: #e83e8c;
background: #f1f3f5;
border-radius: 4px;
}
【JavaScript】
// =========================================
// 💡 パターン1:display: none ⇄ block の切り替え
// =========================================
const toggleBtn = document.querySelector('.js-toggle-btn');
const toggleBoxes = document.querySelectorAll('.js-toggle-box');
toggleBtn.addEventListener('click', () => {
toggleBoxes.forEach(box => {
box.classList.toggle('is-shown');
});
});
// =========================================
// 💡 パターン2:DOM要素の動的追加
// =========================================
const itemList = document.querySelector('.js-item-list');
const addBtn = document.querySelector('.js-add-item');
const clearBtn = document.querySelector('.js-clear-items');
let itemCount = 1;
addBtn.addEventListener('click', () => {
itemCount++;
// 新しい div 要素を生成してクラスを付与し、リストに追加するだけ!
const newItem = document.createElement('div');
newItem.className = 'c-added-item';
newItem.textContent = `追加された要素 #${itemCount}`;
itemList.appendChild(newItem);
});
clearBtn.addEventListener('click', () => {
itemCount = 1;
itemList.innerHTML = '<div class="c-added-item">最初からある要素 #1</div>';
});
// =========================================
// 💡 パターン3:<dialog> の開閉
// =========================================
const dialog = document.querySelector('.js-dialog');
const openBtn = document.querySelector('.js-open-dialog');
const closeBtn = document.querySelector('.js-close-dialog');
openBtn.addEventListener('click', () => dialog.showModal());
closeBtn.addEventListener('click', () => dialog.close());
dialog.addEventListener('click', (e) => {
if (e.target === dialog) dialog.close();
});
@starting-styleの仕組みと書き方
display: none からの表示で transition が効かない理由
CSSの transition は、「変更前の状態(A)」から「変更後の状態(B)」への差分を計算して滑らかに補間する仕組みです。
しかし、要素が display: none;(画面上に存在しない状態)だった場合や、JavaScriptの appendChild() などで新しくDOMツリーに追加された瞬間は、ブラウザにとって「変更前の状態(A)」の計算データが存在しません。そのため、いきなり「変更後の状態(B)」として描画されてしまい、transition が発火しなかったのです。
そこで @starting-style を使うと、「要素が画面に初めて表示される1フレーム目のスタイル(変更前の状態A)」を明示的に指定できるため、そこから通常スタイル(状態B)へ向かって綺麗に transition が動くようになります!
@starting-style の基本的な書き方(2通りの記述方法)
@starting-style は、CSSのネスト(入れ子)を使って対象のセレクタの中に直接書く方法と、外側に独立して書く方法の2通りがあります。コードがまとまって見やすい「① ネスト記法」がおすすめです!
/* 書き方①:セレクタの中にネスト(入れ子)して書く方法(推奨!) */
.box.is-open {
display: block;
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
/* 表示された瞬間の「スタート地点」を指定 */
@starting-style {
opacity: 0;
transform: translateY(20px);
}
}
/* 書き方②:外側に @starting-style ブロックを独立して書く方法 */
@starting-style {
.box.is-open {
opacity: 0;
transform: translateY(20px);
}
}
【注意点】@starting-style のルールは通常のスタイルと同じ詳細度を持つため、外側に独立して書く場合は必ず「通常のスタイル定義よりも下(後ろ)」に記述してください(上に書くと通常のスタイルに上書きされてしまい動きません)。
どんな場面で使うと便利?
display: none➔display: blockで表示する要素
タブパネルやアコーディオン、ドロップダウンメニューなど、visibilityではなくdisplayで表示・非表示を切り替えたいUI。- JSで動的に追加される要素
ToDoリストの追加アイテム、チャットメッセージ、画面端に出るトースト通知など、createElement()で追加された瞬間にふわっと表示させたい要素。 <dialog>要素や[popover]要素
標準ではdisplay: noneから切り替わるモーダルやポップオーバー、さらにその背景である::backdrop疑似要素のオープン演出。
補足:消える時(block ➔ none)もアニメーションさせたい場合は?
@starting-style はあくまで「要素が表示される時(入場時)」の初期値を決めるための機能です。
もし逆に、クラスを外して display: block から display: none へ非表示にする時(退場時) にも opacity: 0 のフェードアウトが終わるまで display: none になるのを待たせたい場合は、もうひとつのモダンCSS機能である transition-behavior: allow-discrete(または transition プロパティ内での allow-discrete 指定)を併用します!
/* 登場時も退場時も display: none ⇄ block のままフェードさせる完全形 */
.box {
display: none;
opacity: 0;
/* 💡 display プロパティにも allow-discrete をつけてトランジションさせる! */
transition:
opacity 0.4s ease,
display 0.4s ease allow-discrete;
}
.box.is-open {
display: block;
opacity: 1;
@starting-style {
opacity: 0;
}
}
「登場時だけでよければ @starting-style のみ」「消える時のフェードアウトまでこだわるなら allow-discrete を足す」と覚えておくと、用途に合わせてスマートに使い分けられます!
補足:公式ドキュメント
まとめ
今回はモダンCSSの新機能である @starting-style について解説しました。
@starting-styleは、要素が最初に描画される瞬間の「開始スタイル(初期値)」を定義するルール- これまで
transitionが効かなかった「display: noneからの表示」や「JSでのDOM追加時」に滑らかなアニメーションが付けられる - 消える時(
block➔none)もアニメーションさせたい場合はtransition-behavior: allow-discreteを組み合わせる
これまで @keyframes を書いたりJSのタイマーで工夫していた処理が、CSSの数行だけで書けるようになる非常に強力な機能です。@starting-styleは主要ブラウザすべてでサポートされていますので、ぜひ日々のUI制作に取り入れてみてくださいね!