फ़ोकस और फ़ोकस-दृश्य रूपांतर
कीबोर्ड नेविगेशन के लिए focus:* शैलियाँ लागू करें और केवल कीबोर्ड उपयोगकर्ताओं के लिए, माउस क्लिक के लिए नहीं, फ़ोकस रिंग दिखाने हेतु focus-visible:* का उपयोग करें।
फ़ोकस और फ़ोकस-दृश्य रूपांतर, CoddyKit पर Tailwind CSS Academy का एक निःशुल्क पाठ है। यह 4 में से 2वाँ पाठ है। आप नीचे पूरा पाठ निःशुल्क पढ़ सकते हैं—फिर अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर के साथ ब्राउज़र में इसका व्यावहारिक अभ्यास कर सकते हैं। यह Tailwind CSS Academy सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। Tailwind CSS Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
फ़ोकस शैलियों का महत्व
कीबोर्ड नेविगेशन का उपयोग मोटर अक्षमता वाले लोग, दक्ष उपयोगकर्ता और लंबा फ़ॉर्म भरने वाला कोई भी व्यक्ति कर सकता है। जब किसी तत्व पर कीबोर्ड से फ़ोकस आता है, तो ब्राउज़र उसे फ़ोकस संकेतक से हाइलाइट करता है। CSS की :focus छद्म-क्लास किसी भी फ़ोकस घटना पर सक्रिय होती है, जबकि :focus-visible तभी सक्रिय होती है जब ब्राउज़र यह निर्धारित करे कि दृश्य फ़ोकस संकेतक उपयोगी होगा — आमतौर पर कीबोर्ड नेविगेशन के दौरान। Tailwind दोनों को focus: और focus-visible: वेरिएंट उपसर्ग के रूप में उपलब्ध कराता है।
<!-- focus-visible is the accessible modern choice -->
<div class="flex gap-4">
<button class="bg-blue-500 text-white px-4 py-2 rounded focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2">
Tab to me
</button>
</div>focus: वेरिएंट की मूल बातें
focus: वेरिएंट तब शैली लागू करता है जब तत्व पर किसी भी माध्यम से फ़ोकस आता है — माउस क्लिक, स्पर्श या कीबोर्ड। इसका सबसे सामान्य उपयोग ब्राउज़र की डिफ़ॉल्ट फ़ोकस आउटलाइन हटाकर उसकी जगह कस्टम रिंग लगाना है: focus:outline-none focus:ring-2 focus:ring-blue-500। यह सुविधाजनक है, लेकिन इसकी एक कमी है: माउस उपयोगकर्ताओं को क्लिक करने पर भी रिंग दिखाई देती है, जिसे कुछ डिज़ाइनर दृश्य रूप से बाधक मानते हैं।
<!-- focus: fires on both mouse click and keyboard tab -->
<div class="space-y-3 max-w-sm">
<input
type="text"
placeholder="Click or tab to focus"
class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
/>
<button class="w-full bg-gray-200 py-2 rounded-lg focus:outline-none focus:ring-2 focus:ring-gray-500">
Focus-on-click-too button
</button>
</div>focus-visible: आधुनिक तरीका
focus-visible: CSS की :focus-visible छद्म-क्लास से जुड़ा है। ब्राउज़र इसे तभी लागू करते हैं जब वे निर्धारित करते हैं कि फ़ोकस संकेतक दिखाना उचित है — यानी कीबोर्ड नेविगेशन के दौरान, लेकिन आमतौर पर बटनों पर माउस क्लिक के दौरान नहीं। इससे दोनों लाभ मिलते हैं: माउस उपयोगकर्ताओं को रिंग दिखाई नहीं देती, लेकिन कीबोर्ड उपयोगकर्ताओं को हमेशा स्पष्ट प्रतिक्रिया मिलती है। सभी आधुनिक Tailwind प्रोजेक्टों में यही अनुशंसित तरीका है।
<!-- focus-visible: only shows ring for keyboard, not mouse -->
<div class="space-y-3 max-w-sm">
<!-- Try: click with mouse (no ring) vs Tab key (ring appears) -->
<button class="w-full bg-blue-500 text-white py-2 rounded-lg focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2">
Smart focus (use Tab key)
</button>
<input
type="text"
placeholder="Input: ring on keyboard focus"
class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500"
/>
</div>कस्टम फ़ोकस रंग संबंधी परंपराएँ
ऐसे फ़ोकस रिंग रंग चुनें जो तत्व और पृष्ठभूमि, दोनों के साथ पर्याप्त कंट्रास्ट बनाएँ। ब्रांड रंग वाली रिंग अच्छी रहती हैं: इंडिगो ब्रांड वाले उत्पाद के लिए focus-visible:ring-indigo-500। अमान्य इनपुट पर त्रुटि अवस्था के फ़ोकस के लिए focus:ring-red-500 उपयोग किया जा सकता है। डार्क मोड में यह भी देखें कि आपकी रिंग का रंग गहरी पृष्ठभूमि पर पर्याप्त कंट्रास्ट देता है या नहीं — गहरे सतहों पर हल्की रिंग (ring-white या ring-blue-300) बेहतर काम करती हैं।
<!-- Focus colors for different contexts -->
<div class="space-y-4 max-w-sm">
<!-- Normal: brand color ring -->
<input class="w-full border rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500" placeholder="Brand focus" />
<!-- Success: green ring -->
<input class="w-full border border-green-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-green-500 bg-green-50" placeholder="Valid input" />
<!-- Error: red ring -->
<input class="w-full border border-red-300 rounded px-3 py-2 focus:outline-none focus:ring-2 focus:ring-red-500 bg-red-50" placeholder="Invalid input" />
</div>लिंक के लिए फ़ोकस शैलियाँ
लिंक कीबोर्ड से नेविगेट किए जाने वाले सबसे महत्वपूर्ण तत्वों में से हैं। डिफ़ॉल्ट रूप से ब्राउज़र लिंक पर फ़ोकस आउटलाइन लगाते हैं, लेकिन आप इसे अपने डिज़ाइन के अनुरूप बदलना चाह सकते हैं। focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:rounded पैटर्न इनलाइन टेक्स्ट लिंक के चारों ओर सटीक रूप से फिट होने वाली साफ़ रिंग बनाता है, जिससे कीबोर्ड नेविगेशन के दौरान वे स्पष्ट रूप से पहचाने जा सकते हैं।
<!-- Accessible link focus styles -->
<p class="text-gray-700 leading-relaxed max-w-prose">
Visit our
<a href="#" class="text-blue-600 underline focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:rounded">
documentation
</a>
to learn more about
<a href="#" class="text-blue-600 underline focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:rounded">
getting started
</a>
with our product.
</p>Focus Within — चाइल्ड फ़ोकस पर प्रतिक्रिया देता पैरेंट
focus-within: तब पैरेंट पर लागू होता है जब उसके किसी भी वंशज पर फ़ोकस हो। यह पूरे फ़ॉर्म फ़ील्ड कंटेनर (लेबल + इनपुट + सहायक टेक्स्ट) को हाइलाइट करने के लिए उपयोगी है, जब उसके अंदर के इनपुट पर फ़ोकस आए — केवल इनपुट को नहीं। दृश्य समूह फैलकर बताता है कि कौन-सा फ़ॉर्म फ़ील्ड समूह सक्रिय है, जिससे जटिल फ़ॉर्म में नेविगेशन आसान हो जाता है।
<!-- Entire form group highlights when input is focused -->
<div class="space-y-3 max-w-sm">
<!-- Container gets ring when child input is focused -->
<div class="border border-gray-200 rounded-xl p-3 focus-within:border-blue-500 focus-within:ring-2 focus-within:ring-blue-500/20 transition-all">
<label class="block text-xs font-medium text-gray-500 mb-1">Email Address</label>
<input
type="email"
class="w-full focus:outline-none text-gray-800"
placeholder="you@example.com"
/>
</div>
<div class="border border-gray-200 rounded-xl p-3 focus-within:border-blue-500 focus-within:ring-2 focus-within:ring-blue-500/20 transition-all">
<label class="block text-xs font-medium text-gray-500 mb-1">Password</label>
<input
type="password"
class="w-full focus:outline-none text-gray-800"
placeholder="••••••••"
/>
</div>
</div>सुगम रिंग के लिए फ़ोकस और ट्रांज़िशन
फ़ोकस पर रिंग और बॉर्डर में होने वाले बदलावों को Tailwind की ट्रांज़िशन उपयोगिताओं से ऐनिमेट किया जा सकता है। transition-shadow शैडो को ऐनिमेट करता है (रिंग आंतरिक रूप से इसी का उपयोग करती हैं), और transition-colors बॉर्डर के रंग में बदलाव को ऐनिमेट करता है। त्वरित 150ms ट्रांज़िशन के लिए duration-150 सेट करने से फ़ोकस शैलियाँ तुरंत और प्रतिक्रियाशील महसूस होती हैं, लेकिन झटकेदार नहीं लगतीं — इंटरैक्टिव फ़ॉर्म तत्वों के लिए यह सही संतुलन है।
<!-- Smoothly animated focus rings -->
<div class="space-y-4 max-w-sm p-4">
<input
type="text"
placeholder="Focus with smooth transition"
class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-150"
/>
<textarea
placeholder="Textarea with focus transition"
rows="3"
class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-purple-500 transition-all duration-150 resize-none"
></textarea>
</div>फ़ोकस शैलियाँ ज़िम्मेदारी से बंद करना
आपको फ़ोकस आउटलाइन पूरी तरह हटाने के डिज़ाइन अनुरोध मिल सकते हैं — जब तक उसका कोई विकल्प न दें, ऐसा करने से बचें। केवल focus:outline-none का उपयोग WCAG 2.1 सफलता मानदंड 2.4.7 (फ़ोकस दृश्य) का उल्लंघन करता है। इसे हमेशा कस्टम रिंग, बॉर्डर में बदलाव, पृष्ठभूमि में बदलाव या किसी अन्य दृश्य संकेतक के साथ जोड़ें। डिफ़ॉल्ट आउटलाइन हटाते हुए सुगम्यता बनाए रखने का ज़िम्मेदार तरीका focus:outline-none focus-visible:ring-2 पैटर्न है।
<!-- ❌ Bad: removes focus with no replacement -->
<button class="bg-blue-500 text-white px-4 py-2 rounded focus:outline-none">
Inaccessible (no focus indicator)
</button>
<!-- ✅ Good: replaces with custom ring -->
<button class="bg-blue-500 text-white px-4 py-2 rounded focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2">
Accessible (custom ring)
</button>Select और Checkbox तत्वों में फ़ोकस
select, checkbox और radio जैसे मूल फ़ॉर्म नियंत्रण भी फ़ोकस वेरिएंट पर प्रतिक्रिया देते हैं। सेलेक्ट के लिए पैटर्न इनपुट जैसा ही है: focus:outline-none focus:ring-2 focus:ring-blue-500। चेकबॉक्स में रिंग चेकबॉक्स के वर्गाकार हिस्से को घेरती है, जबकि रेडियो में यह वृत्त को घेरती है। @tailwindcss/forms प्लगइन अलग-अलग ब्राउज़र में इन तत्वों के लिए बेहतर डिफ़ॉल्ट प्रदान करता है।
<!-- Focus styles on different form controls -->
<div class="space-y-4 max-w-sm">
<!-- Select -->
<select class="w-full border border-gray-300 rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<!-- Checkboxes -->
<label class="flex items-center gap-3 cursor-pointer">
<input type="checkbox" class="w-4 h-4 rounded border-gray-300 text-blue-600 focus:ring-2 focus:ring-blue-500" />
<span>Agree to terms</span>
</label>
<!-- Radio -->
<label class="flex items-center gap-3 cursor-pointer">
<input type="radio" name="plan" class="w-4 h-4 border-gray-300 text-blue-600 focus:ring-2 focus:ring-blue-500" />
<span>Monthly plan</span>
</label>
</div>नेविगेशन छोड़ने वाले लिंक
नेविगेशन छोड़ने वाले लिंक पेज पर फ़ोकस किए जा सकने वाले पहले तत्व होते हैं। इनके ज़रिए कीबोर्ड उपयोगकर्ता सीधे मुख्य सामग्री पर जा सकते हैं और नेविगेशन को छोड़ सकते हैं। ये डिफ़ॉल्ट रूप से दिखाई नहीं देते, लेकिन फ़ोकस मिलने पर दिखाई देने लगते हैं। इस पैटर्न में sr-only को focus:not-sr-only focus:absolute के साथ इस्तेमाल किया जाता है, ताकि कीबोर्ड उपयोगकर्ता के टैब करके वहाँ पहुँचने पर ही लिंक दिखाई दे। यह WCAG 2.4.1 का अनुपालन करने की आवश्यकता है।
<!-- Skip navigation link: visible only on keyboard focus -->
<a
href="#main-content"
class="sr-only focus:not-sr-only focus:absolute focus:top-2 focus:left-2 bg-blue-500 text-white px-4 py-2 rounded font-medium z-50"
>
Skip to main content
</a>
<nav class="bg-gray-800 text-white px-6 py-4">
<!-- Navigation items -->
</nav>
<main id="main-content" class="p-6">
Main content here
</main>डार्क मोड में फ़ोकस शैलियाँ
लाइट मोड के लिए बनाई गई फ़ोकस रिंग डार्क मोड में गायब हो सकती है या उसका कंट्रास्ट कम हो सकता है। रिंग के रंग बदलने के लिए टेलविंड के dark: वेरिएंट का इस्तेमाल करें: focus-visible:ring-blue-500 dark:focus-visible:ring-blue-400 डार्क मोड में गहरे बैकग्राउंड पर बेहतर कंट्रास्ट के लिए थोड़ा हल्का नीला रंग इस्तेमाल करता है। वैकल्पिक रूप से, गहरे सतहों पर सफ़ेद रिंग इस्तेमाल करें: dark:focus-visible:ring-white।
<!-- Focus rings for both light and dark mode -->
<div class="p-6 bg-white dark:bg-gray-900 space-y-3">
<input
type="text"
placeholder="Adaptive focus ring"
class="w-full bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 text-gray-900 dark:text-white rounded-lg px-4 py-2 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 dark:focus-visible:ring-blue-400 focus-visible:border-blue-500 max-w-sm"
/>
</div>त्वरित जाँच
इस पाठ में सिखाई गई टेलविंड CSS महारत की अवधारणाओं की अपनी समझ जाँचें।
पाठ का पुनरावलोकन
इस पाठ में आपने सीखा: focus: किसी भी फ़ोकस घटना पर सक्रिय होता है, जबकि focus-visible: केवल कीबोर्ड नेविगेशन के दौरान सक्रिय होता है, focus-within: किसी भी संतति तत्व पर फ़ोकस होने पर उसके अभिभावक को हाइलाइट करता है — फ़ॉर्म फ़ील्ड समूहों के लिए बहुत उपयोगी, और WCAG की सुलभता बनाए रखने के लिए focus:outline-none को हमेशा कस्टम रिंग के साथ इस्तेमाल करें। आगे हम फ़ॉर्म तत्वों की स्टाइलिंग के लिए निष्क्रिय और प्लेसहोल्डर वेरिएंट देखेंगे।
एआई शिक्षक के साथ HTML सीखें — निःशुल्क
अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।
- पाठ्यक्रम
- 30
- पाठ
- 120
अक्सर पूछे जाने वाले प्रश्न
क्या “फ़ोकस और फ़ोकस-दृश्य रूपांतर” पाठ निःशुल्क है?
हाँ—“फ़ोकस और फ़ोकस-दृश्य रूपांतर” का पूरा पाठ यहाँ वेब पर निःशुल्क पढ़ा जा सकता है। इंटरैक्टिव अभ्यास (अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर) करने और Tailwind CSS Academy पाठ्यक्रम का बाकी हिस्सा अनलॉक करने के लिए CoddyKit PRO लें। Tailwind CSS Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
“फ़ोकस और फ़ोकस-दृश्य रूपांतर” में मैं क्या सीखूँगा?
कीबोर्ड नेविगेशन के लिए focus:* शैलियाँ लागू करें और केवल कीबोर्ड उपयोगकर्ताओं के लिए, माउस क्लिक के लिए नहीं, फ़ोकस रिंग दिखाने हेतु focus-visible:* का उपयोग करें। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ Tailwind CSS Academy का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।
क्या Tailwind CSS Academy शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?
पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर Tailwind CSS Academy शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 2वाँ पाठ है।
“फ़ोकस और फ़ोकस-दृश्य रूपांतर” पाठ पूरा करने में कितना समय लगता है?
CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।
क्या मैं इस Tailwind CSS Academy पाठ में कोड लिख और चला सकता हूँ?
हाँ। हर Tailwind CSS Academy पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।
इस पाठ्यक्रम के सभी पाठ
- Hover और सक्रिय रूपांतर
- फ़ोकस और फ़ोकस-दृश्य रूपांतर
- अक्षम और प्लेसहोल्डर रूपांतर
- Group और peer रूपांतर