如果我为mdl-layout__header-row类设置了max-width属性,则下拉列表将无法正常工作,它们将无法对齐。删除max-width属性,一切正常。

码:

<!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>

最佳答案

添加position:relative。这将停止定位的元素在.mdl-layout__header-row div中偏离直线。当使用绝对定位来定位元素时,您需要设置一个父容器来定位它们,并通过为它们设置position:relative来做到这一点-否则它将位于它们所定位的最外层html元素/文档中。

<!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>

关于html - MDL菜单不能与最大宽度正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42468109/

10-13 01:37