tabindex와 accesskey
키보드 탐색 순서와 단축키를 제어합니다.
tabindex와 accesskey은(는) CoddyKit의 무료 HTML Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 HTML Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. HTML Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
키보드 탐색
마우스를 사용하지 않는 사용자는 대화형 요소를 탐색할 때 Tab 키에 의존합니다:
- 자연스럽게 포커스할 수 있는 요소:
<a href>,<button>,<input>,<select>,<textarea> - 대화형이 아닌 요소(
<div>,<span>)는 Tab 순서에 포함되지 않습니다 tabindex는 요소가 Tab 순서에 포함되는지와 그 위치를 제어합니다
tabindex="0"
tabindex="0"은 대화형이 아닌 요소를 자연스러운 Tab 순서에 추가합니다:
<div tabindex="0" role="button" onclick="handleClick()">
Custom Button
</div>
<!-- Now Tab navigates to this div -->
<!-- It receives focus at the natural position in the DOM -->
<!-- Better: use a real button instead! -->
<button onclick="handleClick()">Real Button</button>tabindex="-1"
tabindex="-1"은 요소를 프로그래밍 방식으로 포커스할 수 있게 하지만 Tab 순서에서는 제거합니다:
<div id="modal" tabindex="-1" role="dialog">
<!-- modal content -->
</div>
<script>
// Open modal and send focus to it:
document.getElementById('modal').focus();
// Modal is now focused but not in the normal tab sequence
</script>양수 tabindex 값은 피해야 합니다
양수 tabindex 값(예: tabindex="3")은 사용자 지정 Tab 순서를 설정하므로 피해야 합니다:
<!-- BAD: positive tabindex creates unpredictable order -->
<input tabindex="3">
<input tabindex="1">
<input tabindex="2">
<!-- The tab order becomes: 1, 2, 3, then all tabindex=0 elements -->
<!-- This almost always confuses users and fails WCAG 2.4.3 -->
<!-- Fix the DOM order instead of using positive tabindex -->SPA에서 포커스 관리하기
단일 페이지 앱은 포커스를 수동으로 관리해야 합니다:
// When navigating to a new route:
document.querySelector('#main-content').focus();
// When opening a modal:
const modal = document.getElementById('modal');
modal.removeAttribute('hidden');
modal.querySelector('[autofocus], button, [tabindex]').focus();
// When closing a modal: return focus to the trigger
document.getElementById('open-btn').focus();모달에서 포커스 가두기
열린 모달 안에 포커스를 가두어 Tab 키가 모달 안에서만 이동하도록 합니다:
function trapFocus(element) {
const focusable = element.querySelectorAll(
'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'
);
const first = focusable[0];
const last = focusable[focusable.length - 1];
element.addEventListener('keydown', e => {
if (e.key === 'Tab') {
if (e.shiftKey && document.activeElement === first) {
e.preventDefault(); last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault(); first.focus();
}
}
});
}accesskey 속성
accesskey 속성은 요소에 키보드 단축키를 할당합니다:
<button accesskey="s">Submit</button>
<!-- On Windows: Alt+S focuses/activates the button -->
<!-- On Mac: Control+Option+S -->
<!-- On Firefox: Shift+Alt+S -->
<a href="/" accesskey="h">Home</a>accesskey의 한계
accesskey에는 다음과 같은 중요한 문제가 있습니다:
- 활성화 키가 브라우저와 운영 체제마다 다릅니다
- 브라우저 단축키와 충돌합니다(예: Alt+F = 파일 메뉴)
- 기본적으로 화면 낭독기가 이를 안내하지 않습니다
- 실제로는 거의 사용되지 않으므로 사용한다면 문서화해야 합니다
포커스 표시기는 항상 표시하기
포커스 윤곽선을 절대 숨기지 마십시오. 키보드 사용자를 위해 꼭 필요합니다:
/* BAD: removes focus outline for everyone */
* { outline: none; }
/* Better: style focus outlines, don't remove them */
:focus-visible {
outline: 2px solid #0070f3;
outline-offset: 2px;
border-radius: 3px;
}
/* :focus-visible only shows outline for keyboard navigation -->
/* Mouse clicks do not trigger :focus-visible in modern browsers */건너뛰기 링크 패턴
건너뛰기 링크를 사용하면 키보드 사용자가 반복되는 탐색 영역을 건너뛸 수 있습니다:
<a href="#main-content" class="skip-link">Skip to main content</a>
<nav>... long navigation ...</nav>
<main id="main-content" tabindex="-1">... content ...</main>
<!-- CSS: -->
<!--
.skip-link {
position: absolute;
top: -100%;
}
.skip-link:focus {
top: 0;
}
-->autofocus 속성
autofocus 속성은 페이지가 로드될 때 요소로 포커스를 이동합니다:
<input type="search" autofocus placeholder="Search...">
<!-- Useful on search pages or forms with a single primary field -->
<!-- Use sparingly: it can disrupt users who rely on the page top for orientation -->
<!-- Only one element per page should have autofocus -->빠른 확인
JavaScript로 요소에 포커스할 수 있지만 일반적인 Tab 탐색에서는 건너뛰게 하는 tabindex 값은 무엇인가요?
복습: tabindex와 accesskey
키보드 접근성의 핵심 내용입니다:
tabindex="0"— 자연스러운 Tab 순서에 추가합니다tabindex="-1"— 프로그래밍 방식으로 포커스할 수 있지만 Tab 순서에는 포함되지 않습니다- 양수 tabindex 값은 피하십시오
:focus-visible윤곽선을 절대 제거하지 마십시오- 키보드 사용자를 위한 건너뛰기 링크를 구현하십시오
accesskey를 사용할 수 있지만 충돌 때문에 실제로는 거의 유용하지 않습니다
자주 묻는 질문
“tabindex와 accesskey” 강의는 무료인가요?
네 — “tabindex와 accesskey” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 HTML Academy 강의 전체를 잠금 해제할 수 있습니다. HTML Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“tabindex와 accesskey”에서 뭘 배우나요?
키보드 탐색 순서와 단축키를 제어합니다. 브라우저에서 직접 실행하는 실습 코드로 HTML Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
HTML Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 HTML Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“tabindex와 accesskey” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 HTML Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 HTML Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- id, class, style 및 title
- hidden 속성
- tabindex와 accesskey
- 사용자 정의 데이터 속성 data-*