Search code examples
htmlcssmaterial-design-lite

mdl-menu not working properly with max-width


If I set the max-width property for mdl-layout__header-row class, the dropdowns are not working properly, they are misaligned. Remove the max-width property and everything works fine.

Code:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <title>Test Page</title>
        <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
        <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.red-light_blue.min.css" />
        <script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
    </head>
    <body>
        <header class="mdl-layout__header">
            <div class="mdl-layout__header-row" style="max-width: 1200px;width: 100%;margin: auto;">
                <a href="/">
                    <img style="height: 60px;" src="http://www.vwshops.com/dummy-store-1/img/my-shop-logo-1452186152.jpg" />
                </a>
                <span>Logo</span>
                <button id="DROPDOWN_RIGHT" class="mdl-button mdl-js-button mdl-button--icon">
                    <i class="material-icons">arrow_drop_down</i>
                </button>
                <ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect language-menu" for="DROPDOWN_RIGHT">
                    <li class="mdl-menu__item">One</li>
                    <li class="mdl-menu__item">Two</li>
                    <li class="mdl-menu__item">Three</li>
                </ul>
                <button style="margin-left: auto;" id="DROPDOWN_LEFT" class="mdl-button mdl-js-button mdl-button--icon">
                    <i class="material-icons">arrow_drop_down</i>
                </button>
                <ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect language-menu" for="DROPDOWN_LEFT">
                    <li class="mdl-menu__item">One</li>
                    <li class="mdl-menu__item">Two</li>
                    <li class="mdl-menu__item">Three</li>
                </ul>
            </div>
        </header>
    </body>
</html>

Solution

  • Add position:relative. This stops the positioned elements straying out of line in .mdl-layout__header-row div. When elements are positioned using absolute positioning you need to setup a parent container to position them off and do that by setting a position:relative for them - otherwise it will be to the outermost html element / document they position off.

    <!doctype html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
            <title>Test Page</title>
            <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
            <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.red-light_blue.min.css" />
            <script src="https://code.getmdl.io/1.3.0/material.min.js"></script>
        </head>
        <body>
            <header class="mdl-layout__header">
                <div class="mdl-layout__header-row" style="max-width: 1200px;width: 100%;margin: auto; position:relative;">
                    <a href="/">
                        <img style="height: 60px;" src="http://www.vwshops.com/dummy-store-1/img/my-shop-logo-1452186152.jpg" />
                    </a>
                    <span>Logo</span>
                    <button id="DROPDOWN_RIGHT" class="mdl-button mdl-js-button mdl-button--icon">
                        <i class="material-icons">arrow_drop_down</i>
                    </button>
                    <ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect language-menu" for="DROPDOWN_RIGHT">
                        <li class="mdl-menu__item">One</li>
                        <li class="mdl-menu__item">Two</li>
                        <li class="mdl-menu__item">Three</li>
                    </ul>
                    <button style="margin-left: auto;" id="DROPDOWN_LEFT" class="mdl-button mdl-js-button mdl-button--icon">
                        <i class="material-icons">arrow_drop_down</i>
                    </button>
                    <ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect language-menu" for="DROPDOWN_LEFT">
                        <li class="mdl-menu__item">One</li>
                        <li class="mdl-menu__item">Two</li>
                        <li class="mdl-menu__item">Three</li>
                    </ul>
                </div>
            </header>
        </body>
    </html>