애니메이션 중에 변화를 지정하는 데 사용된다. 즉, 각 단계에서 요소의 스타일을 어떻게 바꿀지를 정의한다.
@keyframes example {
0% {background-color: red;}
50% {background-color: yellow;}
100% {background-color: blue;}
}
@keyframes moveToRight {
from{
color: red;
}
to {
color: yellow;
transform: translateX(300px);
}
여기서, from은 0%와 같고, to는 100%와 같다.
요소에 적용된 @keyframes 애니메이션의 동작과 지속 시간, 반복 횟수 등을 지정한다.
애니메이션의 하위 속성은 다음과 같다.
div {
animation-name: example;
animation-duration: 4s;
animation-iteration-count: infinite;
}
위 예제에서는 example이라는 이름의 @keyframes 애니메이션이 div 요소에 적용되어 있다. 애니메이션의 지속 시간은 4초이며, 무한히 반복된다.
CSS 애니메이션을 사용하면 JavaScript 없이도 복잡한 애니메이션 효과를 만들 수 있다. 그러나 CSS 애니메이션은 JavaScript로 제어할 수 없으므로(예: 일시 중지, 취소, 반복), 보다 복잡한 상호 작용이 필요한 경우 JavaScript를 사용하는 것이 더 나을 수 있다.
animation-fill-mode 속성은 애니메이션이 시작하기 전이나 끝나고 난 후에 어떻게 요소를 스타일링할지 결정하는 속성이다. 이 속성은 아래의 값 중 하나를 가질 수 있다.

CSS 애니메이션에서 translate, scale, rotate, skew는 모두 transform의 함수들이다. 이들은 웹 페이지 요소의 모양과 위치를 변환하는데 사용된다. 각각의 기능은 다음과 같다.
이들 transform 함수는 다양한 방식으로 조합되어 복잡한 변환 효과를 생성할 수 있다. 또한, 이들 함수는 CSS 애니메이션에서 매우 중요한 역할을 한다. 애니메이션의 키 프레임에서 이들 변환 함수를 다양하게 적용하여 동적인 시각 효과를 만들 수 있다.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./index.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>Test</button>
</body>
</html>
@keyframes moveToRight {
from{
color: red;
}
to {
color: yellow;
transform: translateX(300px);
}
}
button {
animation: moveToRight 2s;
animation-delay: 2s;
color: black;
}

이 CSS 코드는 버튼에 애니메이션을 적용하고 있다. 애니메이션의 이름은 "moveToRight"이며, 이 애니메이션은 버튼의 색상을 빨간색에서 노란색으로 변경하고, 동시에 버튼을 오른쪽으로 300px 만큼 이동시키는 역할을 한다.
@keyframes moveToRight: 이 부분에서 moveToRight라는 애니메이션을 정의하고 있다. from 키워드로 시작하여 이동을 시작하는 시점의 스타일을 정의하고, to 키워드로 이동이 끝나는 시점의 스타일을 정의한다. 이 경우, 애니메이션은 빨간색에서 시작해서 노란색으로 변하면서 오른쪽으로 300px 만큼 이동하게 된다.
button: 이 부분에서 버튼에 대한 CSS 스타일을 설정한다.
@keyframes moveToRight {
from{
color: red;
}
to {
color: yellow;
transform: translateX(300px);
}
}
button {
animation: moveToRight 2s;
animation-direction: reverse;
animation-delay: 2s;
color: black;
}

animation-direction에 reverse를 적용한 예시이다. 애니메이션이 예제1과는 다르게 역순으로 재생되는 것을 볼 수 있다.
@keyframes moveToRight {
from{
color: red;
}
to {
color: yellow;
transform: translateX(300px);
}
}
button {
animation: moveToRight 2s;
animation-iteration-count: 2;
animation-direction: alternate-reverse;
animation-delay: 2s;
color: black;
}
애니메이션 횟수를 2로 하고, 방향을 alternate-reverse로 했다.
이 경우, 역순으로 진행된 다음, 정순으로 진행되는 애니메이션이 재생된다.
역순과 정순은 각각 1회로 카운트된다.

@keyframes moveToRight {
from{
color: red;
}
to {
color: yellow;
transform: translateX(300px);
}
}
button {
animation: moveToRight 2s;
animation-duration: 1s;
animation-direction: alternate-reverse;
animation-iteration-count: infinite;
animation-play-state: running;
color: black;
}
이 CSS 코드는 HTML 문서의 <button> 요소에 애니메이션을 적용하고 있습니다. 해당 애니메이션의 이름은 "moveToRight"이며, 이 애니메이션은 버튼의 색상을 빨간색에서 노란색으로 변경하고, 동시에 버튼을 오른쪽으로 300px 만큼 이동시키는 역할을 한다.

button {
animation: moveToRight;
animation-duration: 1s;
animation-delay: 2s;
animation-fill-mode: none;
color: black;
}

button {
animation: moveToRight;
animation-duration: 1s;
animation-delay: 2s;
animation-fill-mode: forwards;
color: black;
}
animation-fill-mode: forwards; 애니메이션이 완료된 이후에도, 애니메이션의 마지막 keyframe에 정의된 스타일이 유저된다. 따라서, 오른쪽으로 이동한 노란색 버튼이 된다.

@keyframes moveToRight {
from{
color: red;
}
to {
color: yellow;
transform: translateX(300px);
}
}
button {
animation: moveToRight;
animation-duration: 1s;
animation-delay: 2s;
animation-fill-mode: both;
color: black;
}
animation-fill-mode: both 가 적용되었으므로, 시작에선 빨간색이 되고, 끝에선 노란색이 된다.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./index.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>Test</button>
</body>
</html>
button {
color: black;
transform: rotate(90deg);
}
transform에서 90도 회전이므로, 회전한다.

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./index.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>Test</button>
</body>
</html>
button {
color: black;
transform: skew(15deg, 30deg);
}
X축으로 15도, Y축으로 30도 비튼다.
