16. hover효과

웹 페이지에서 마우스 커서를 요소 위에 올렸을 때 나타나는 시각적 반응을 호버(hover, 마우스 오버) 효과라고 부른다. 호버는 사용자의 조작 상태를 감지하여 스타일을 동적으로 변경하는 가상 클래스(pseudo-class)이며, 스위치에 손을 대면 불이 들어오는 감지 센서와 같은 역할을 한다. 이 글에서는 CSS의 트랜지션(transition, 전환)과 트랜스폼(transform, 변형), 그리고 가상 요소(pseudo-element, 의사 요소)를 결합하여 입체적인 버튼 및 이미지 마우스 오버 인터랙션을 단계별로 구현하는 방법을 다룬다.

마우스 호버 인터랙션 유형 비교 — 가상 요소와 전환 속성을 결합해 버튼과 이미지에 동적인 반응을 부여한다.

2.마우스 오버 효과

1.1. button-hover

버튼에 적용하는 호버 효과는 사용자가 클릭 가능한 영역임을 직관적으로 인지하도록 돕는다. 가상 요소와 지연 시간을 활용하면 단순한 색상 변화를 넘어 역동적인 모션 효과를 제작할 수 있다.

1.1.1. mouse-hover1

마우스를 올렸을 때 양옆에서 원형 점들이 날아와 중앙에서 합쳐진 후 배경이 사각형으로 확장되는 버튼 예제이다. HTML 구조는 링크 역할을 하는 <a> 태그 내부에 배경 확장을 담당할 빈 <span> 태그를 배치한다.

HTML

<a href="#"><span></span>hover me</a>

CSS에서는 가상 요소와 그림자 속성을 이용해 입자 애니메이션의 시간차를 구성한다. a:before와 a:after 가상 요소는 실제 HTML 태그를 추가하지 않고 CSS로 생성하는 보조 요소로, 버튼 좌우 외곽에 원형 점과 box-shadow를 배치한다. 마우스를 올리면 이 점들이 중앙으로 모여들고, transition-delay(전환 지연 시간)가 적용된 span이 0.4초 뒤에 transform: scale(1)로 커지며 버튼 전체를 채운다.

CSS

body {
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    font-family: verdana;
    background-color: #272727;
}

a {
    width: 220px;
    height: 80px;
    color: #ff0;
    background-color: transparent;
    font-size: 26px;
    text-decoration: none;
    text-transform: uppercase;
    text-align: center;
    line-height: 80px;
    transition: all 0.5s;
    position: relative;
}

a:before,
a:after {
    content: '';
    position: absolute;
    top: 50%;
    width: 20px;
    height: 20px;
    background-color: #ff0;
    border-radius: 50%;
    transform: translateY(-50%);
    transition: all 0.3s;
    z-index: -1;
    opacity: 0;
}

a:before {
    left: 0;
    box-shadow: -100px 0 0 #ff0;
}

a:after {
    right: 0;
    box-shadow: 100px 0 0 #ff0;
}

a:hover:before {
    left: 50%;
    box-shadow: 30px 0 0 #ff0;
    transform: translateX(-50%) translateY(-50%);
    opacity: 1;
}

a:hover:after {
    right: 50%;
    box-shadow: -30px 0 0 #ff0;
    transform: translateX(50%) translateY(-50%);
    opacity: 1;
}

span {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #ff0;
    border-radius: 8px;
    transform: scale(0);
    transition: all 0.3s;
    z-index: -1;
}

a:hover span {
    transform: scale(1);
    transition-delay: 0.4s;
}

a:hover {
    color: #262626;
    transition-delay: 0.4s;
}

가상 요소와 span에 z-index: -1을 지정하지 않으면 확장된 배경이 글자 위를 덮어 텍스트가 가려지므로 레이어 우선순위를 반드시 텍스트 아래로 설정해야 한다.

1.1.2. mouse-hover2

내비게이션 메뉴 항목에 마우스를 올렸을 때 배경이 4개의 가로 줄로 나뉘어 순차적으로 채워지는 효과이다. 각 메뉴 링크(<a>) 내부에 4개의 빈 <span> 태그를 배치하여 4분할 레이어를 준비한다.

HTML

<ul>
    <li>
        <a href="#">
            home
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </a>
    </li>

    <li>
        <a href="#">
            about
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </a>
    </li>

    <li>
        <a href="#">
            services
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </a>
    </li>

    <li>
        <a href="#">
            team
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </a>
    </li>

    <li>
        <a href="#">
            contact
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </a>
    </li>
</ul>

CSS에서는 각 span의 높이를 height: 25%로 지정하여 세로 영역을 4등분하고, transform: scaleX(0)과 transform-origin: left를 통해 가로축 왼쪽을 기준으로 압축해 둔다. :nth-child 선택자로 각 줄마다 transition-delay를 0.15초씩 차등 적용하여 위에서부터 아래로 순서대로 차오르는 계단식 애니메이션을 구현한다.

CSS

body {
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    font-family: sans-serif;
}

ul {
    margin: 0;
    padding: 0;
    display: flex;
    list-style-type: none;
}

ul li a {
    display: block;
    width: 120px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: red;
    text-transform: capitalize;
    text-decoration: none;
    position: relative;
    transition: all 0.5s;
    color: #262626;
}

a:hover {
    color: white;
}

ul li span {
    position: absolute;
    width: 100%;
    height: 25%;
    background-color: #262626;
    z-index: -1;
    left: 0;
    top: 0;
    transform: scaleX(0);
    transition: all 0.5s;
    transform-origin: left;
}

a:hover span {
    transform: scaleX(1);
}

span:nth-child(2) {
    top: 25%;
    transition-delay: 0.15s;
}

span:nth-child(3) {
    top: 50%;
    transition-delay: 0.3s;
}

span:nth-child(4) {
    top: 75%;
    transition-delay: 0.45s;
}

transform-origin 속성은 변형이 시작되는 기준점을 정의한다. 기본값은 중심(center)이므로, 왼쪽에서 오른쪽으로 채워지는 효과를 내려면 반드시 transform-origin: left를 명시해야 한다.

1.1.3. mouse-hover3

단일 버튼 내부에서 둥근 형태의 배경이 아래에서 위로 차오르는 효과이다. 추가 마크업 없이 <button> 태그 하나만으로 구성한다.

HTML

<button class="btn">button</button>

버튼에 overflow: hidden을 지정하여 경계 바깥으로 나가는 요소를 숨긴다. .btn:before 가상 요소에 border-radius: 50% 50% 0% 0%를 부여하여 윗부분이 볼록한 돔 형태로 만들고 초기 높이를 0%로 설정한다. 호버 시 높이를 190%로 급격히 늘려 액체가 차오르는 듯한 시각적 효과를 연출한다.

CSS

body {
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    font-family: sans-serif;
}

.btn {
    border: 2px solid tomato;
    background: none;
    color: tomato;
    padding: 20px 40px;
    font-size: 25px;
    text-transform: uppercase;
    cursor: pointer;
    transition: all 0.5s;
    position: relative;
    color: tomato;
    overflow: hidden;
}

.btn:hover {
    color: white;
}

.btn:before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: tomato;
    z-index: -1;
    border-radius: 50% 50% 0% 0%;
    height: 0%;
    transition: all 0.5s;
}

.btn:hover:before {
    height: 190%;
}

부모 요소인 .btn에 overflow: hidden이 누락되면 확장된 곡면 배경이 버튼 테두리를 벗어나 사각형 바깥으로 노출되므로 주의해야 한다.

1.1.4. mouse-hover4

원형 아이콘 버튼에 마우스를 올렸을 때 내부 배경이 채워짐과 동시에 테두리에 점선 회전 애니메이션이 동작하는 예제이다. 폰트어썸(FontAwesome) 아이콘 글꼴 CDN을 <head>에 연결하여 아이콘을 렌더링한다.

HTML

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CSS animatin, transitions and transforms</title>

    
    <link rel="stylesheet" href="style.css" type="text/css" media="all">

    
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" integrity="sha384-/rXc/GQVaYpyDdyxK+ecHPVYJSN9bmVFBvjA/9eOB+pb3F2w2N6fc5qB9Ew5yIns" crossorigin="anonymous">


</head>

<body>

    <ul>
        <li><i class="fas fa-heart"></i></li>
        <li><i class="fas fa-glass-martini"></i></li>
        <li><i class="fas fa-globe"></i></li>
        <li><i class="fas fa-gift"></i></li>
    </ul>

</body>
</html>

CSS에서는 li:before로 축소되며 나타나는 내부 배경을 제어하고, li:after로 점선 테두리(border: 2px dashed #2e86de)를 구성한다. 키프레임(keyframes, 애니메이션 구간 정의 규칙)인 @keyframes rotating을 선언하여 rotate(0deg)에서 rotate(360deg)까지 무한 반복 회전하도록 연결한다.

CSS

body {
 margin: 0;
 display: flex;
 align-items: center;
 justify-content: center;
 height: 100vh;
 font-family: sans-serif;
}

ul {
  padding: 0;
  margin: 0;
  display: flex;
}

ul li {
  list-style-type: none;
  width: 120px;
  height: 120px;
  margin: 0 20px;
  border:2px solid #0a3d62;
  border-radius: 50%;
  transition: all 0.5s;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

ul li i {
  font-size: 48px;
  color: #0a3d62;
  transition: 0.5s;
}

ul li:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #0a3d62;
  border-radius: 50%;
  transition: all 0.5s;
  opacity: 0;
  z-index: -1;
}

ul li:hover:before {
  opacity: 1;
  transform: scale(0.8);
}

ul li:after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: transparent;
  border-radius: 50%;
  border:2px dashed #2e86de;
  transition: all 0.5s;
  opacity: 0;
  z-index: -1;
  box-sizing: border-box;
}

ul li:hover:after {
  opacity: 1;
  animation: rotating 10s linear infinite;
}

@keyframes rotating {
  0% { transform:scale(0.92) rotate(0deg); }
  100% { transform:scale(0.92) rotate(360deg); }
}

ul li:hover i {
  color: white;
}

1.2. image-hover

이미지 호버 효과는 갤러리나 카드 UI에서 특정 항목에 시선을 집중시키는 데 활용된다. 이미지의 크기 확대, 회전과 함께 캡션 텍스트를 부드럽게 노출하는 기법을 실습한다.

1.2.1. image-hover1

파일

예제이미지다운로드

예제 이미지를 다운로드하여 image.png로 저장한 뒤 사용한다. 컨테이너 박스 안에 이미지와 설명 글 레이어(.caption)를 중첩 배치하는 구조이다.

HTML

  <div class="container">
      <img src="image.png">
      <div class="caption">
          <h1>amazing caption!</h1>
          <p>you can write anything here!</p>
      </div>
  </div>

CSS에서는 .container에 overflow: hidden을 부여하여 확대된 이미지가 영역 바깥으로 넘치지 않도록 제한한다. .caption은 position: absolute로 이미지 위에 겹쳐두고 기본 상태를 opacity: 0으로 숨긴다. 컨테이너에 호버하면 이미지가 1.3배 확대되면서 15도 회전(transform: scale(1.3) rotate(15deg))하고, 캡션 레이어는 반투명 검은색 배경과 함께 서서히 나타난다.

CSS

body {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Lato', sans-serif;
}

.container {
    width: 500px;
    height: 500px;
    position: relative;
    overflow: hidden;
}

.container img {
    width: 100%;
    transition: all 0.5s;
}

.caption {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    transition: 0.5s;
    opacity: 0;
    background-color: rgba(0, 0, 0, 0);
}

.caption h1 {
    text-transform: uppercase;
    margin: 0;
}

.caption p {
    font-size: 18px;
    text-transform: capitalize;
}

.container:hover .caption {
    opacity: 1;
    background-color: rgba(0, 0, 0, 0.5);
}

.container:hover img {
    transform: scale(1.3) rotate(15deg);
}

부모 컨테이너에 position: relative를 선언하지 않으면 position: absolute가 적용된 .caption이 부모 박스가 아닌 브라우저 전체 화면을 기준으로 정렬되므로 주의해야 한다.

1.2.2. image-hover2

파일

예제이미지다운로드

예제 이미지를 다운로드하여 girl.jpg로 저장한다. <div> 프레임 내부에 <img> 요소를 감싸는 기본 갤러리 구조이다.

HTML

  <div>
      <img src="girl.jpg">
  </div>

CSS에서는 div에 액자 형태의 테두리(border: 5px solid white)와 overflow: hidden을 설정한다. img:hover 시 1초 동안 1.2배 확대 및 9도 회전(transform: scale(1.2) rotate(9deg))이 발생하며, 프레임 바깥으로 벗어난 이미지는 잘려 깔끔한 액자 확대 효과를 연출한다.

CSS

body {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #4b4b4b;
}

div {
    width: 300px;
    height: 400px;
    border: 5px solid white;
    overflow: hidden;
}

img {
    width: 100%;
    height: 100%;
    transition: transform 1s;
}

img:hover {
    transform: scale(1.2) rotate(9deg);
}

이미지에 transition: transform 1s를 명시하면 호버가 해제되어 원래 상태로 돌아갈 때도 1초 동안 부드럽게 복원된다. 전환 속성을 :hover 선택자가 아닌 기본 img 선택자에 지정해야 진입과 복원 양방향 모두에서 애니메이션이 유지된다.

댓글 남기기