03Dot nav via :target
Clicking a dot changes the address bar's #fragment like a real link and the browser scrolls that slide into view inside the snap track, while the :target selector is used only to color the matching dot.
Best forGalleries where the back button should return to the previous slide, shareable slide links
:targetscroll-margin앵커 링크

<main class="stage">
<div class="dt is-demo">
<div class="dt__viewport">
<div class="dt__track">
<section id="dt-s1" class="dt__slide dt__slide--1"></section>
<section id="dt-s2" class="dt__slide dt__slide--2"></section>
<section id="dt-s3" class="dt__slide dt__slide--3"></section>
<section class="dt__slide dt__slide--1" aria-hidden="true"></section>
</div>
</div>
<nav class="dt__dots">
<a href="#dt-s1" class="dt__dot dt__dot--1" aria-label="Slide 1"></a>
<a href="#dt-s2" class="dt__dot dt__dot--2" aria-label="Slide 2"></a>
<a href="#dt-s3" class="dt__dot dt__dot--3" aria-label="Slide 3"></a>
</nav>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// ── 변수 3개 ──────────────────────────────────────────
$duration: $dur-base;
$easing: $ease-spring;
$color: $stage-yellow;
@include stage($stage-orange);
.dt { width: min(300px, 80vw); }
// ── 실제 동작: <a href="#id"> 로 진짜 앵커 이동 + :target 으로 슬라이드가 스크롤 상자 안에서 이동 (0줄 JS) ──
.dt__viewport {
overflow-x: auto;
scroll-snap-type: x mandatory;
border-radius: $r-card;
scrollbar-width: none;
&::-webkit-scrollbar { display: none; }
}
.dt__track { display: flex; width: max-content; }
.dt__slide {
flex: 0 0 min(300px, 80vw);
aspect-ratio: 16 / 9;
scroll-snap-align: start;
scroll-margin-left: 0;
}
.dt__slide--1 { background: linear-gradient(135deg, $subject-ink, #4a3f5c); }
.dt__slide--2 { background: linear-gradient(135deg, $subject-blue, #d6ddeb); }
.dt__slide--3 { background: linear-gradient(135deg, $color, #ebe7d6); }
.dt__dots {
display: flex;
justify-content: center;
gap: $sp-2;
margin-top: $sp-3;
}
.dt__dot {
width: $sp-2; height: $sp-2;
border-radius: 50%;
background: rgba(23, 20, 26, .28);
transition: transform $duration $easing, background $duration $easing;
}
// :target 은 링크(dot)가 아니라 가리킨 슬라이드(section)에 붙는다 — :has() 로 같은 편의 점을 역으로 찾아 칠한다
.dt:has(#dt-s1:target) .dt__dot--1,
.dt:has(#dt-s2:target) .dt__dot--2,
.dt:has(#dt-s3:target) .dt__dot--3 {
background: $color;
transform: scale(1.4);
}
// ── 데모 루프: 진짜 :target 상태는 그대로 두고, 트랙·점만 미리보기로 스스로 순환한다 ──
// 트랙 끝에는 1번의 복제본(aria-hidden)이 한 장 더 — 그곳까지 정상 속도로 가고 100%→0% 점프는
// 복제본=1번이라 안 보인다 (끝→처음 텔레포트 금지). % 는 트랙(max-content=슬라이드 4장) 기준이라
// 한 칸은 정확히 -25% — 슬라이드 폭이 vw 단위라 창이 좁아져도 어긋나지 않는다
.dt.is-demo .dt__track { animation: dt-track $dur-loop $easing infinite; }
.dt.is-demo .dt__dot--1 { animation: dt-dot-1 $dur-loop $easing infinite; }
.dt.is-demo .dt__dot--2 { animation: dt-dot-2 $dur-loop $easing infinite; }
.dt.is-demo .dt__dot--3 { animation: dt-dot-3 $dur-loop $easing infinite; }
@keyframes dt-track {
0%, 8% { transform: translateX(0); }
31%, 39% { transform: translateX(-25%); }
62%, 70% { transform: translateX(-50%); }
92%, 100% { transform: translateX(-75%); }
}
// 점 1번은 루프 끝(92~100%, 복제본=1번이 보이는 구간)에 다시 켜진다 — 100% 와 0% 의 상태가 같아야
// 루프 경계에서 점이 순간이동으로 바뀌지 않는다
@keyframes dt-dot-1 { 0%, 4% { background: $color; transform: scale(1.4); } 29%, 88% { background: rgba(23,20,26,.28); transform: scale(1); } 92%, 100% { background: $color; transform: scale(1.4); } }
@keyframes dt-dot-2 { 0%, 29% { background: rgba(23,20,26,.28); transform: scale(1); } 33%, 62% { background: $color; transform: scale(1.4); } 66%, 100% { background: rgba(23,20,26,.28); transform: scale(1); } }
@keyframes dt-dot-3 { 0%, 62% { background: rgba(23,20,26,.28); transform: scale(1); } 66%, 96% { background: $color; transform: scale(1.4); } 100% { background: rgba(23,20,26,.28); transform: scale(1); } }
// ── 접근성 ──────────────────────────────────────────
@media (prefers-reduced-motion: reduce) {
.dt.is-demo .dt__track,
.dt.is-demo .dt__dot--1,
.dt.is-demo .dt__dot--2,
.dt.is-demo .dt__dot--3 { animation: none !important; }
.dt__viewport { scroll-behavior: auto; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ff4d1f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.dt {
width: min(300px, 80vw);
}
.dt__viewport {
overflow-x: auto;
scroll-snap-type: x mandatory;
border-radius: 16px;
scrollbar-width: none;
}
.dt__viewport::-webkit-scrollbar {
display: none;
}
.dt__track {
display: flex;
width: max-content;
}
.dt__slide {
flex: 0 0 min(300px, 80vw);
aspect-ratio: 16/9;
scroll-snap-align: start;
scroll-margin-left: 0;
}
.dt__slide--1 {
background: linear-gradient(135deg, #17141a, #4a3f5c);
}
.dt__slide--2 {
background: linear-gradient(135deg, #2f6df6, #d6ddeb);
}
.dt__slide--3 {
background: linear-gradient(135deg, #ffd23f, #ebe7d6);
}
.dt__dots {
display: flex;
justify-content: center;
gap: 8px;
margin-top: 12px;
}
.dt__dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: rgba(23, 20, 26, 0.28);
transition: transform 300ms cubic-bezier(0.2, 0.8, 0.2, 1), background 300ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.dt:has(#dt-s1:target) .dt__dot--1,
.dt:has(#dt-s2:target) .dt__dot--2,
.dt:has(#dt-s3:target) .dt__dot--3 {
background: #ffd23f;
transform: scale(1.4);
}
.dt.is-demo .dt__track {
animation: dt-track 2s cubic-bezier(0.2, 0.8, 0.2, 1) infinite;
}
.dt.is-demo .dt__dot--1 {
animation: dt-dot-1 2s cubic-bezier(0.2, 0.8, 0.2, 1) infinite;
}
.dt.is-demo .dt__dot--2 {
animation: dt-dot-2 2s cubic-bezier(0.2, 0.8, 0.2, 1) infinite;
}
.dt.is-demo .dt__dot--3 {
animation: dt-dot-3 2s cubic-bezier(0.2, 0.8, 0.2, 1) infinite;
}
@keyframes dt-track {
0%, 8% {
transform: translateX(0);
}
31%, 39% {
transform: translateX(-25%);
}
62%, 70% {
transform: translateX(-50%);
}
92%, 100% {
transform: translateX(-75%);
}
}
@keyframes dt-dot-1 {
0%, 4% {
background: #ffd23f;
transform: scale(1.4);
}
29%, 88% {
background: rgba(23, 20, 26, 0.28);
transform: scale(1);
}
92%, 100% {
background: #ffd23f;
transform: scale(1.4);
}
}
@keyframes dt-dot-2 {
0%, 29% {
background: rgba(23, 20, 26, 0.28);
transform: scale(1);
}
33%, 62% {
background: #ffd23f;
transform: scale(1.4);
}
66%, 100% {
background: rgba(23, 20, 26, 0.28);
transform: scale(1);
}
}
@keyframes dt-dot-3 {
0%, 62% {
background: rgba(23, 20, 26, 0.28);
transform: scale(1);
}
66%, 96% {
background: #ffd23f;
transform: scale(1.4);
}
100% {
background: rgba(23, 20, 26, 0.28);
transform: scale(1);
}
}
@media (prefers-reduced-motion: reduce) {
.dt.is-demo .dt__track,
.dt.is-demo .dt__dot--1,
.dt.is-demo .dt__dot--2,
.dt.is-demo .dt__dot--3 {
animation: none !important;
}
.dt__viewport {
scroll-behavior: auto;
}
}// 실제 동작은 <a href="#id"> + :target 만으로 끝난다(0줄) — 점을 누르면 주소창 #이 바뀌고
// 브라우저가 그 슬라이드를 스크롤 상자 안에서 화면에 맞춰 옮긴다.
// 이 3줄은 미리보기 자동 재생만 멈춘다.
document.querySelector('.dt__viewport').addEventListener('pointerdown', function () {
document.querySelector('.dt').classList.remove('is-demo');
});import { useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./DotsTarget.module.scss";
export type DotsTargetProps = {
duration?: number;
easing?: string;
color?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type TrackEl = { children: { item(i: number): { scrollIntoView(opts: { behavior: string; inline: string }): void } | null } } | null;
export default function DotsTarget({ duration = 300, easing = "cubic-bezier(.2,.8,.2,1)", color = "#ffd23f" }: DotsTargetProps) {
const [active, setActive] = useState(0);
const track = useRef<TrackEl>(null);
const vars: Vars = { "--duration": `${duration}ms`, "--easing": easing, "--color": color };
const go = (i: number) => {
setActive(i);
track.current?.children.item(i)?.scrollIntoView({ behavior: "smooth", inline: "start" });
};
return (
<div className={styles.dt} style={vars}>
{/* 원본은 <a href="#id"> + :target 만으로 끝난다(0줄 JS). 컴포넌트는 아이디가 겹치지 않도록
scrollIntoView 로 같은 동작을 낸다 — 실제로 슬라이드가 스크롤 상자 안에서 움직인다 */}
<div className={styles.viewport} ref={track as never}>
<div className={styles.track}>
{[1, 2, 3].map((n) => (
<div key={n} className={`${styles.slide} ${styles["slide" + n]}`} />
))}
</div>
</div>
<nav className={styles.dots}>
{[0, 1, 2].map((i) => (
<button
key={i}
type="button"
aria-label={`슬라이드 ${i + 1}`}
className={`${styles.dot} ${i === active ? styles.isActive : ""}`}
onClick={() => go(i)}
/>
))}
</nav>
</div>
);
}03 Dot nav via :target — Measured area 35.13% · moving frames 20/23Original by GODRICH · SCSS and React ship in the zip · 9 CSS Only Carousel Slider Recipes to Copy-Paste
The post this demo lives in
9 CSS Only Carousel Slider Recipes to Copy-PasteA CSS only carousel slider is a row of photos or cards that steps through one at a time in the same frame, built nine wa See all demos in this post


