Performance: Dropdown menu alignment
This affects how quickly and smoothly dropdown menus appear and align on the page, impacting user interaction speed and visual stability.
<div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown button </button> <ul class="dropdown-menu dropdown-menu-end"> <li><a class="dropdown-item" href="#">Action</a></li> <li><a class="dropdown-item" href="#">Another action</a></li> </ul> </div>
document.querySelectorAll('.dropdown-toggle').forEach(btn => { btn.addEventListener('click', () => { const menu = btn.nextElementSibling; menu.style.left = (btn.getBoundingClientRect().left + window.scrollX) + 'px'; menu.style.top = (btn.getBoundingClientRect().bottom + window.scrollY) + 'px'; }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual style setting on click | Multiple style writes | 2 reflows per open | High paint cost due to layout thrashing | [X] Bad |
| Bootstrap dropdown-menu-end class | Minimal DOM changes | 1 reflow triggered by Popper.js | Low paint cost with GPU compositing | [OK] Good |
dropdown-menu-start to align left, dropdown-menu-end to align right, and dropdown-menu-center to center the dropdown menu.dropdown-menu-end aligns the dropdown menu to the right side of the toggle button.dropdown-menu for styling and behavior.dropdown-menu-start must be combined with dropdown-menu in the class attribute, like dropdown-menu dropdown-menu-start.<div class='dropdown'>
<button class='btn btn-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>Menu</button>
<ul class='dropdown-menu dropdown-menu-center'>
<li><a class='dropdown-item' href='#'>Item 1</a></li>
<li><a class='dropdown-item' href='#'>Item 2</a></li>
</ul>
</div>dropdown-menu-center is used on the dropdown menu, which centers the menu horizontally below the toggle button.dropdown-menu-end, and center with dropdown-menu-center. So this menu will appear centered.<ul class='dropdown-menu-end'> <li><a class='dropdown-item' href='#'>Option</a></li> </ul>
dropdown-menu on the menu element for proper styling and alignment.dropdown-menu-end without dropdown-menu, so the alignment class alone won't work.dropdown-menu dropdown-menu-end for right alignment by default, then use CSS media queries to override alignment to center on small screens.Pass the test and this pattern moves unknown