← Back to CSS Course | Chapter 6: Selectors & Styling | Lesson 9 of 22

CSS Advanced Dropdowns

एक single-level hover dropdown आसान case है। असली navigation menus को अक्सर एक submenu के अंदर एक submenu चाहिए होता है, या एक ऐसा menu जो touch devices के लिए click पर खुले जहाँ hover बिल्कुल मौजूद ही नहीं होता।
Syntax
css
.dropdown .dropdown-content {
  display: none;
  position: absolute;
}
.dropdown:hover .dropdown-content {
  display: block;
}
.dropdown-content a:hover {
  background-color: color;
}

किसी Dropdown के अंदर एक Submenu Nest करना

एक multi-level dropdown किसी dropdown item के अंदर एक और absolutely positioned list रखकर बनाया जाता है, default रूप से hidden और सिर्फ तभी दिखाया जाता है जब वह specific item hover किया जाए।

हर nesting level को अपना खुद का position: relative parent चाहिए ताकि inner submenu सही item के relative position हो, outer menu के नहीं।

उदाहरण: Nesting a Submenu Inside a Dropdown

css
<style>
.item {
  position: relative;
  background: #eee;
  padding: 8px;
}
.submenu {
  display: none;
  position: absolute;
  background: lightyellow;
  border: 1px solid #ccc;
  padding: 8px;
}
.item:hover .submenu {
  display: block;
}
</style>
<div class="item">
  Tools
  <div class="submenu">Sub Item</div>
</div>

बिना JavaScript के Click-Triggered Dropdowns

Touch devices पर hover मौजूद नहीं होता, इसलिए mobile support के लिए click-triggered dropdowns चाहिए। एक pure-CSS trick एक hidden checkbox को एक label के साथ जोड़ता है: label को click करने से checkbox toggle होता है, और checkbox checked होने पर एक sibling selector menu दिखाता है।

उदाहरण: Click-Triggered Dropdowns Without JavaScript

css
<style>
.menu {
  display: none;
  background: lightyellow;
  border: 1px solid #ccc;
  padding: 8px;
}
#toggle:checked ~ .menu {
  display: block;
}
</style>
<input type="checkbox" id="toggle" hidden>
<label for="toggle">Menu</label>
<div class="menu">Shown when the checkbox is checked</div>

Viewport से Overflow होने वाले Submenus को Position करना

जो submenu हमेशा दाईं तरफ़ खुलता है वह narrow viewports पर screen के edge से बाहर जा सकता है। एक common fix एक modifier class है, जो media query या JavaScript detection के ज़रिए लागू होती है, जो submenu की position को इसके बजाय बाईं तरफ़ खोलने के लिए flip कर देती है।

उदाहरण: Positioning Submenus That Overflow the Viewport

css
<style>
.submenu {
  position: absolute;
  left: 100%;
  background: lightyellow;
  border: 1px solid #ccc;
  padding: 8px;
}
.submenu.flip {
  left: auto;
  right: 100%;
}
</style>
<div class="submenu flip">Opens leftward instead of running off-screen</div>

Submenu Reveal को Animate करना

एक instant display: none से display: block toggle के बजाय, submenu पर opacity और एक छोटे translateY transform को animate करना एक smoother, ज़्यादा polished fade-and-slide-down reveal बनाता है, जो production navigation menus में common है।

उदाहरण: Animating the Submenu Reveal

css
<style>
.item {
  position: relative;
  background: #eee;
  padding: 8px;
}
.submenu {
  background: lightyellow;
  border: 1px solid #ccc;
  padding: 8px;
  opacity: 0;
  transform: translateY(-10px);
  transition: opacity 0.2s, transform 0.2s;
}
.item:hover .submenu {
  opacity: 1;
  transform: translateY(0);
}
</style>
<div class="item">
  Menu
  <div class="submenu">Fades and slides down</div>
</div>

Nested Menus के लिए Keyboard और Screen Reader Considerations

सिर्फ :hover से बने Multi-level dropdowns keyboard users के लिए unreachable हैं, क्योंकि Tab hover states को trigger नहीं करता। हर nesting level पर :focus-within को :hover के साथ combine करना, और JavaScript से maintained aria-haspopup/aria-expanded attributes इस्तेमाल करना, nested menus को properly accessible बनाता है।

उदाहरण: Keyboard and Screen Reader Considerations for Nested Menus

css
<style>
.item {
  background: #eee;
  padding: 8px;
}
.submenu {
  display: none;
  background: lightyellow;
  border: 1px solid #ccc;
  padding: 8px;
}
.item:hover .submenu,
.item:focus-within .submenu {
  display: block;
}
</style>
<div class="item">
  <a href="#" aria-haspopup="true" aria-expanded="false">Menu</a>
  <div class="submenu"><a href="#">Item</a></div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. सिर्फ :hover इस्तेमाल करना, इसलिए touch और keyboard users menu नहीं खोल सकते।
  2. submenu को display: none से hide करना और फिर उसे animate करना, इसलिए कोई transition नहीं होता; opacity या visibility इस्तेमाल करें।
  3. submenu को उसके parent पर position: relative के बिना रखना।
ब्राउज़र सपोर्ट

हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.