2. CSS

CSS Transition 트렌지션 사용하기

도피디 2024. 12. 3. 22:31
반응형

CSS에서 Transition(트렌지션)이란? 

어떤 상태에서 다른 상태로의 "변화"를 애니메이션으로 만드는 방법이다.

 

 

 

1. 기본 코딩

<!DOCTYPE html>
<html lang="kr">
<head>
<title>The Nick Times</title>
<style>
a {
color: wheat;
background-color: tomato;
text-decoration: none;
padding: 3px 5px;
border-radius: 5px;
}
</style>
</head>
<body>
<a href="#">Go home</a>
</body>
</html>

 

 

결과

버튼 완성!

 

 

 

 

 

2. hover 효과 적용해보기

<!DOCTYPE html>
<html lang="kr">
<head>
<title>The Nick Times</title>
<style>
a {
color: wheat;
background-color: tomato;
text-decoration: none;
padding: 3px 5px;
border-radius: 5px;
}
a:hover {
color: tomato;
background-color: wheat;
}
</style>
</head>
<body>
<a href="#">Go home</a>
</body>
</html>

 

 

결과

 

마우스 오버하면 색상이 바뀐다.

3. 이제 transtion을 써보자~!

 

transition은 state가 없는 요소(root) 에 적어야 한다.

 

(ex. 위 코딩에서 hover(state)가 있는 쪽이 아니라 없는 쪽에 써야함)

 

<!DOCTYPE html>
<html lang="kr">
<head>
<title>The Nick Times</title>
<style>
a {
color: wheat;
background-color: tomato;
text-decoration: none;
padding: 3px 5px;
border-radius: 5px;
font-size: 55px;
transition: background-color 10s ease-in-out;
}
a:hover {
color: tomato;
background-color: wheat;
}
</style>
</head>
<body>
<a href="#">Go home</a>
</body>
</html>

 

 

결과

 

아주 스무스하게 변하는구만 ㅎㅎ 👍

 

 

 

 

 

4. 살짝 다르게 transition 써보기

<!DOCTYPE html>
<html lang="kr">
<head>
<title>The Nick Times</title>
<style>
a {
color: wheat;
background-color: tomato;
text-decoration: none;
padding: 3px 5px;
border-radius: 5px;
font-size: 55px;
transition: background-color 10s ease-in-out, color 5s ease-in-out;
}
a:hover {
color: tomato;
background-color: wheat;
}
</style>
</head>
<body>
<a href="#">Go home</a>
</body>
</html>

 

결과

 

 

 


 

transition이 hover에 있는 background-color를 찾고

background-color가 hover 일 때 달라져 있으면

그 변화를 애니메이션으로 바꾸는 것이다.

 

 

https://nomadcoders.co/kokoa-clone/lectures/1732 

반응형