Magento – How to add dropdown menu

magento-2.1magento2menutoplinks

enter image description here

The above image is of top-links. I wanted to add a drop-down for My Account. When the user click or hover on it, My Account should show My account option For example orders.

Best Answer

Create phtml template file

<div class="customer-menu">
 <span class="action toggle"  data-mage-init='{"dropdown":{}}' data-toggle="dropdown" aria-haspopup="true">
     <span class="title"><?= __("Customer Menu"); ?></span>
 </span>
 <ul class="dropdown">
     <?= $block->getChildHtml() ?>
 </ul>
</div>

Call block out in XML where you want

 <block class="Magento\Framework\View\Element\Template" name="header-dropdown" template="Custom_Module::header-dropdown.phtml"/>

Add design into less file

.customer-menu {
    .lib-dropdown();
}

Move your links into dropdown with xml (example: my account link)

 <move element="my-account-link" destination="header-dropdown" />
Related Topic