07:user-invalid 자동 흔들림
JS 없이 input:user-invalid 의사 클래스만으로, 사용자가 값을 입력하고 포커스를 벗어난 뒤 유효성 검사에 실패하면 브라우저가 스스로 흔들림 애니메이션을 겁니다.
쓰임이메일·전화번호 형식 검증, 자바스크립트 없는 정적 폼
:user-invalidpattern500ms

<main class="stage">
<div class="ui is-demo">
<label class="ui__label" for="ui-email">이메일</label>
<input class="ui__input" id="ui-email" type="email" required pattern=".+@.+\..+" value="broken">
<p class="ui__msg">올바른 이메일 주소를 입력하세요</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
$duration: 500ms; // 흔들림 한 번의 길이
$easing: $ease-out;
$color: $error;
@include stage($stage-orange);
.ui { width: min(260px, 86vw); display: flex; flex-direction: column; gap: $sp-2; }
.ui__label { font-weight: 600; font-size: 12px; line-height: 1; font-family: inherit; color: $subject-ink; }
.ui__input {
box-sizing: border-box;
width: 100%;
padding: $sp-2 $sp-3;
border-radius: $r-sm;
border: 2px solid rgba(23, 20, 26, .2);
background: #fff;
color: $subject-ink;
font-weight: 400;
font-size: 14px;
line-height: 1;
font-family: inherit;
}
.ui__msg { margin: 0; color: $subject-ink; font-weight: 500; font-size: 11px; line-height: 1.3; font-family: inherit; }
@keyframes ui-shake {
0%, 22%, 100% { transform: translateX(0); }
2% { transform: translateX(-7px); }
4% { transform: translateX(7px); }
6% { transform: translateX(-7px); }
8% { transform: translateX(7px); }
10% { transform: translateX(-5px); }
12% { transform: translateX(5px); }
14% { transform: translateX(-3px); }
16% { transform: translateX(3px); }
}
// 실제 동작: JS 없이 :user-invalid 만으로 흔들린다. 값을 만지고 벗어난 뒤에만 켜진다
.ui__input:user-invalid { border-color: $color; animation: ui-shake $duration $easing; }
// 데모 루프: :user-invalid 활성화 타이밍이 브라우저마다 달라 .is-demo 로 강제한다
.ui.is-demo .ui__input { animation: ui-shake $duration $easing infinite; }
@media (prefers-reduced-motion: reduce) {
.ui__input:user-invalid,
.ui.is-demo .ui__input { animation: none !important; }
}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);
}
.ui {
width: min(260px, 86vw);
display: flex;
flex-direction: column;
gap: 8px;
}
.ui__label {
font-weight: 600;
font-size: 12px;
line-height: 1;
font-family: inherit;
color: #17141a;
}
.ui__input {
box-sizing: border-box;
width: 100%;
padding: 8px 12px;
border-radius: 8px;
border: 2px solid rgba(23, 20, 26, 0.2);
background: #fff;
color: #17141a;
font-weight: 400;
font-size: 14px;
line-height: 1;
font-family: inherit;
}
.ui__msg {
margin: 0;
color: #17141a;
font-weight: 500;
font-size: 11px;
line-height: 1.3;
font-family: inherit;
}
@keyframes ui-shake {
0%, 22%, 100% {
transform: translateX(0);
}
2% {
transform: translateX(-7px);
}
4% {
transform: translateX(7px);
}
6% {
transform: translateX(-7px);
}
8% {
transform: translateX(7px);
}
10% {
transform: translateX(-5px);
}
12% {
transform: translateX(5px);
}
14% {
transform: translateX(-3px);
}
16% {
transform: translateX(3px);
}
}
.ui__input:user-invalid {
border-color: #c2361a;
animation: ui-shake 500ms ease-out;
}
.ui.is-demo .ui__input {
animation: ui-shake 500ms ease-out infinite;
}
@media (prefers-reduced-motion: reduce) {
.ui__input:user-invalid,
.ui.is-demo .ui__input {
animation: none !important;
}
}// CSS 만으로는 :user-invalid 가 "사용자가 값을 만지고 벗어난 뒤" 에만 켜진다.
// 데모에서 그 상태를 보여주려고 포커스·입력·블러를 흉내만 낸다 (JS 4줄).
var el = document.getElementById('ui-email');
el.focus({ preventScroll: true });
el.dispatchEvent(new Event('input'));
el.blur();import { useState } from "react";
import type { CSSProperties, ChangeEvent } from "react";
import styles from "./UserInvalidShake.module.scss";
export type UserInvalidShakeProps = {
duration?: number;
easing?: string;
color?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
// React 는 :user-invalid 를 직접 읽지 못해, onBlur 에서 checkValidity() 로 같은 순간을 흉내 낸다.
// 실제 zip 의 vanilla 판은 JS 없이 CSS 의사 클래스 하나로 끝난다 — 이 파일은 React 이식일 뿐이다.
export default function UserInvalidShake({ duration = 500, easing = "ease-out", color = "#c2361a" }: UserInvalidShakeProps) {
const [touched, setTouched] = useState(true);
const [run, setRun] = useState(0);
const vars: Vars = { "--duration": `${duration}ms`, "--easing": easing, "--color": color };
const onBlur = (e: ChangeEvent<HTMLInputElement>) => {
setTouched(!e.target.checkValidity());
setRun((n) => n + 1);
};
return (
<div className={styles.ui} style={vars}>
<label className={styles.label} htmlFor="ui-email">이메일</label>
<input
key={run}
id="ui-email"
className={`${styles.input} ${touched ? styles.shake : ""}`}
type="email"
required
pattern=".+@.+\..+"
defaultValue="broken"
onBlur={onBlur}
/>
<p className={styles.msg}>올바른 이메일 주소를 입력하세요</p>
</div>
);
}07 :user-invalid 자동 흔들림 — 측정 누적면적 0.507% · 움직인 프레임 8/23GODRICH 원작 · zip 에 SCSS·React 포함 · CSS 흔들리는 애니메이션 9종 — 폼 에러 표시
이 데모가 속한 글
CSS 흔들리는 애니메이션 9종 — 폼 에러 표시비밀번호를 잘못 입력했을 때 입력창이 좌우로 흔들리는 걸 본 적 있으신가요? 그 동작이 바로 css 흔들리는 애니메이션입니다 — 자바스크립트 없이 CSS 코드 몇 줄만으로 폼이 틀렸다는 걸 눈에 띄게 알려줍니다. 아 이 글의 데모 전부 보기


