본문 바로가기
명함 만들기 캐릭터 모음 16가지 디자인 · PNG / SVG / PDF

좋아요 토글

누르면 그 자리에서 켜지고 꺼지는 동작 — 화면을 옮기지도, 저장 버튼을 누르지도 않고 상태만 바뀔 때 쓴다.

버튼 · 액션

실제 미리보기

출처

<div class="dc-likebox">
  <input class="dc-like__in" type="checkbox" id="dc-like-post" />
  <label class="dc-like" for="dc-like-post">
    <svg class="dc-like__ico" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 20.3 10.6 19C6.5 15.3 4 13 4 10.2A4.2 4.2 0 0 1 8.2 6c1.5 0 2.9.7 3.8 1.9A4.7 4.7 0 0 1 15.8 6 4.2 4.2 0 0 1 20 10.2c0 2.8-2.5 5.1-6.6 8.8z" /></svg>
    <span class="dc-like__txt">좋아요</span>
  </label>
</div>
.dc-likebox {
  display: flex;
  align-items: center;
  padding: var(--dc-4);
  background: var(--dc-surface);
  font-family: var(--dc-font);
}
.dc-like__in {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.dc-like {
  display: inline-flex;
  align-items: center;
  gap: var(--dc-2);
  min-height: 44px;
  padding: 0 var(--dc-3);
  border-radius: var(--dc-r-full);
  color: var(--dc-text-sub);
  font-family: var(--dc-font);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--dc-fast), color var(--dc-fast);
}
.dc-like:hover { background: var(--dc-sunken); }
.dc-like__ico {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linejoin: round;
  transition: transform var(--dc-fast), fill var(--dc-fast);
}
.dc-like__txt { line-height: 1.2; }
.dc-like__n {
  min-width: 2ch;
  font-variant-numeric: tabular-nums;
  color: var(--dc-text-sub);
}
.dc-like__in:checked + .dc-like {
  color: var(--dc-danger);
  background: var(--dc-danger-soft);
}
.dc-like__in:checked + .dc-like .dc-like__ico {
  fill: currentColor;
  stroke: currentColor;
  transform: scale(1.12);
}
.dc-like__in:checked + .dc-like .dc-like__n { color: var(--dc-danger); }
.dc-like__in:focus-visible + .dc-like {
  outline: 2px solid var(--dc-brand);
  outline-offset: 2px;
}
.dc-like--round {
  justify-content: center;
  min-width: 44px;
  padding: 0;
}

이렇게 씁니다

  • 누른 즉시 모양이 바뀌게 한다. 서버 응답을 기다리고 나서 바꾸면 먹통처럼 느껴진다
  • 숫자를 함께 보여줄 때는 내 한 번이 숫자에 바로 반영되게 한다
  • 같은 자리에서 다시 누르면 취소된다는 것이 모양만 보고 이해되어야 한다

주의할 점

  • 색만으로 켜짐을 나타내지 않는다. 채움·굵기가 같이 달라져야 색을 못 가리는 사람도 안다
  • 체크박스로 만들면 스크린리더가 켜짐·꺼짐을 스스로 읽는다. div 에 클릭만 붙이지 않는다
  • 아이콘만 남길 때는 dc-sr 이름을 넣고 누르는 영역을 44×44px 로 지킨다

다음에 볼 것