Looping templates¶
This guide explains how to create content blocks that automatically switch at a fixed time interval. Each block displays for the same duration, then the cycle loops back to the first block.
The rotation is pure CSS: every block runs the same keyframe animation, offset by a delay, so no JavaScript, no timers and nothing to clean up on a template switch. Use it for sponsor slides, rotating notices, or alternating between a schedule and a map. For blocks that must change at a clock time rather than on an interval, see Time-based layout override.
Markup¶
Inside the base template's .your-content, one block per slide, each with its own id;
blocks that share a layout share a class:
<div id="your-content" class="your-content">
<div id="div1" class="slide">
<h2>First block</h2>
</div>
<div id="div2" class="slide">
<h2>Second block</h2>
</div>
<div id="div3" class="slide slide-alt">
<h2>Third block</h2>
</div>
</div>
A block may contain anything a template can — data-field bindings, images, a video.
CSS¶
With three blocks each visible for a third of the cycle:
@keyframes divSwitch {
0%, 33.33% { opacity: 1; z-index: 1; }
33.34%, 100% { opacity: 0; z-index: -1; }
}
.slide {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2em;
opacity: 0; /* hidden until its turn */
animation: divSwitch 45s infinite; /* 15 s per block × 3 blocks */
}
#div1 { animation-delay: 0s; }
#div2 { animation-delay: 15s; } /* 15 s × (block number − 1) */
#div3 { animation-delay: 30s; }
Every block needs opacity: 0 and the same animation with the same total duration;
the tiny gap between 33.33% and 33.34% keeps two blocks from overlapping for a frame.
Put the rules after the base template's styles (an @font-face block still goes first).
For two blocks the visible range is 0%, 50% and the hidden one 50.01%, 100%; for four,
0%, 25% and 25.01%, 100%.
Changing the timing¶
The three numbers follow from the display time per block and the number of blocks:
- Keyframe percentage per block:
100% / number_of_blocks - Total animation duration:
display_time_per_block × number_of_blocks - Animation delay for block N:
display_time_per_block × (N − 1)
To change the timing:
- Change the total duration in the
animationof every block. - Adjust the
animation-delayfor each block. - Update the keyframe percentages in
@keyframes.
To add a block, add a <div> with a new id, give it the shared class, recompute the
total duration and the percentages for the new count, and set its delay.
Related¶
- Time-based layout override
- Template JavaScript best practices — the Rotating Announcements Banner example does the same with JavaScript, for lists that come from data
- Getting started with templates