옵션 1 : :focus-visible의사 클래스 사용
:focus-visible의사 클래스 (터치 또는 마우스 클릭을 통해, 즉)보기 흉한 윤곽을 죽이고 키보드를 통해 탐색되지 않은 사용자에 대한 버튼과 다양한 요소에 반지를 집중하는 데 사용할 수 있습니다.
/**
* The default focus style is likely provided by Bootstrap or the browser
* but here we override everything else with a visually appealing cross-
* browser solution that works well on all focusable page elements
* including buttons, links, inputs, textareas, and selects.
*/
*:focus {
outline: 0 !important;
box-shadow:
0 0 0 .2rem #fff, /* use site bg color to create whitespace for faux focus ring */
0 0 0 .35rem #069 !important; /* faux focus ring color */
}
/**
* Undo the above focused button styles when the element received focus
* via mouse click or touch, but not keyboard navigation.
*/
*:focus:not(:focus-visible) {
outline: 0 !important;
box-shadow: none !important;
}
경고 : 2020 년부터 :focus-visible의사 클래스는 브라우저에서 광범위하게 지원되지 않습니다 . 그러나 polyfill은 사용하기가 매우 쉽습니다. 아래 지침을 참조하십시오.
옵션 2 : .focus-visible폴리 필 사용
이 솔루션은 위에서 언급 한 의사 클래스 대신 일반 CSS 클래스를 사용 하며 공식 Javascript 기반 polyfill 이므로 광범위한 브라우저를 지원합니다 .
1 단계 : HTML 페이지에 Javascript 종속성 추가
참고 : focusvisible polyfill에는 classList를 지원 하지 않는 몇 가지 구형 브라우저에 대한 추가 polyfill이 필요합니다 .
<!-- place this code just before the closing </html> tag -->
<script src="https://cdn.polyfill.io/v2/polyfill.js?features=Element.prototype.classList"></script>
<script src="https://unpkg.com/focus-visible"></script>
2 단계 : 스타일 시트에 다음 CSS 추가
다음은 위에 자세히 설명 된 CSS 솔루션의 수정 된 버전입니다.
/**
* Custom cross-browser styles for keyboard :focus overrides defaults.
*/
*:focus {
outline: 0 !important;
box-shadow:
0 0 0 .2rem #fff,
0 0 0 .35rem #069 !important;
}
/**
* Remove focus styles for non-keyboard :focus.
*/
*:focus:not(.focus-visible) {
outline: 0 !important;
box-shadow: none !important;
}
3 단계 (선택 사항) : 필요한 경우 'focus-visible'클래스를 사용하십시오.
누군가가 터치를 클릭하거나 사용할 때 포커스 링을 실제로 표시하려는 항목이 있으면 focus-visible클래스를 DOM 요소에 추가하십시오 .
<!-- This example uses Bootstrap classes to theme a button to appear like
a regular link, and then add a focus ring when the link is clicked --->
<button type="button" class="btn btn-text focus-visible">
Clicking me shows a focus box
</button>
자원:
데모: