विकल्प 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छद्म वर्ग ब्राउज़रों में व्यापक रूप से समर्थित नहीं है । हालांकि पॉलीफिल का उपयोग करना बहुत आसान है; नीचे निर्देश देखें।
विकल्प 2: .focus-visibleपॉलीफिल का उपयोग करें
यह समाधान ऊपर उल्लेखित छद्म वर्ग के बजाय एक सामान्य सीएसएस वर्ग का उपयोग करता है, और इसमें व्यापक ब्राउज़र समर्थन है क्योंकि यह एक आधिकारिक जावास्क्रिप्ट-आधारित पॉलीफ़िल है ।
चरण 1: अपने HTML पृष्ठ पर जावास्क्रिप्ट निर्भरता जोड़ें
ध्यान दें: फ़ोकस-दृश्यमान पॉलीफ़िल को कई पुराने ब्राउज़रों के लिए एक अतिरिक्त पॉलीफ़िल की आवश्यकता होती है जो क्लासिस्ट का समर्थन नहीं करते हैं :
<!-- 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: अपनी स्टाइलशीट में निम्नलिखित सीएसएस जोड़ें
निम्नलिखित सीएसएस समाधान का एक संशोधित संस्करण है जो ऊपर और अधिक अच्छी तरह से प्रलेखित है।
/**
* 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क्लास को 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>
संसाधन:
डेमो: