परिचय
यदि हम किसी फोकस्ड एलिमेंट के पैरेंट को स्टाइल करना चाहते हैं, तो हम CSS :focus-within स्यूडो-क्लास का उपयोग करते हैं। यह फॉर्म और इंटरैक्टिव कंपोनेंट्स के लिए अत्यंत उपयोगी है।
उदाहरण 1: फॉर्म फोकस स्टाइलिंग
<!DOCTYPE html>
<html>
<head>
<style>
form {
margin: 2%;
padding: 2%;
display: flex;
flex-direction: column;
background: thistle;
}
input {
margin: 2%;
}
form:focus-within {
background-color: burlywood;
box-shadow: 0 0 12px rgba(0,0,0,0.6)
}
input:focus {
box-shadow: 0 0 12px rgba(0,0,0,0.6)
}
</style>
</head>
<body>
<form>
<input type="text" placeholder="name"/>
<input type="email" placeholder="email"/>
</form>
</body>
</html>
उदाहरण 2: मिक्स्ड कंटेंट के साथ डिव फोकस
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 40%;
margin: 2%;
padding: 2%;
display: flex;
flex-direction: column;
background: lavenderblush;
}
div > * {
margin: 2%;
}
div:focus-within {
background-color: aliceblue;
box-shadow: 0 0 12px rgba(0,0,0,0.6)
}
</style>
</head>
<body>
<div>
<textarea></textarea>
<button>Click</button>
</div>
</body>
</html>
यह कैसे काम करता है
:focus-withinएक एलिमेंट से मेल खाता है यदि **वह स्वयं** फोकस्ड है **या** उसका कोई वंशज फोकस्ड है:focusके विपरीत, जो केवल सीधे फोकस्ड एलिमेंट को टार्गेट करता है- फॉर्म वैलिडेशन फीडबैक, ड्रॉपडाउन मेन्यू, और कार्ड हाइलाइटिंग के लिए आदर्श
व्यावहारिक उपयोग के मामले
1. फॉर्म फील्ड हाइलाइटिंग
<!DOCTYPE html>
<html>
<head>
<style>
.form-field {
margin: 1rem 0;
padding: 1rem;
border: 2px solid #e0e0e0;
border-radius: 8px;
transition: border-color 0.2s, box-shadow 0.2s;
}
.form-field:focus-within {
border-color: #0066cc;
box-shadow: 0 0 0 3px rgba(0,102,204,0.15);
}
.form-field label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
}
.form-field input, .form-field textarea {
width: 100%;
border: none;
outline: none;
font-size: 1rem;
}
</style>
</head>
<body>
<div class="form-field">
<label for="email">ईमेल</label>
<input type="email" id="email" placeholder="आपका ईमेल दर्ज करें" />
</div>
<div class="form-field">
<label for="message">संदेश</label>
<textarea id="message" placeholder="अपना संदेश लिखें..."></textarea>
</div>
</body>
</html>
2. ड्रॉपडाउन मेन्यू विजिबिलिटी
<!DOCTYPE html>
<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
min-width: 200px;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
opacity: 0;
visibility: hidden;
transform: translateY(-8px);
transition: all 0.2s;
z-index: 100;
}
.dropdown:focus-within .dropdown-menu {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.dropdown-toggle {
padding: 0.75rem 1.5rem;
background: #0066cc;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="dropdown">
<button class="dropdown-toggle" tabindex="0">मेन्यू ▼</button>
<div class="dropdown-menu">
<a href="#" style="display:block; padding:0.75rem; text-decoration:none; color:#333;">प्रोफाइल</a>
<a href="#" style="display:block; padding:0.75rem; text-decoration:none; color:#333;">सेटिंग्स</a>
<a href="#" style="display:block; padding:0.75rem; text-decoration:none; color:#333;">लॉगआउट</a>
</div>
</div>
</body>
</html>
ब्राउज़र सपोर्ट
सभी मॉडर्न ब्राउज़र में सपोर्टेड (Chrome 60+, Firefox 52+, Safari 10.1+, Edge 79+)। IE में सपोर्ट नहीं है।