mdl-menu无法正常使用max-width

时间:2017-02-26 11:51:13

标签: html css material-design-lite

如果我为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>

1 个答案:

答案 0 :(得分:1)

添加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>