【问题标题】:stop list dropdown going off-screen停止列表下拉菜单离开屏幕
【发布时间】:2016-11-29 20:21:43
【问题描述】:

我在我的导航栏中使用 HTML 列表作为下拉菜单。我遇到的问题是,当浏览器窗口很小时,下拉菜单会离开屏幕到左侧。

我想知道将其简单居中并以小屏幕尺寸将内容拉伸 100% 的最佳方法。

问题在于通知菜单(将鼠标悬停在小信封上)。

我已经提供了一些我从网站上撕下来的代码,这给了你一个想法:http://codepen.io/anon/pen/eBeYXd

将鼠标悬停在最右侧的项目上,将出现菜单,将浏览器设置为手机屏幕大小,然后看到下拉菜单移到左侧被切断。

代码如下:

    <div class="navigation-main">
        <div class="container group">
            <ul class="header-navbar">
                <!-- notes test -->
                <li class="dropdown pull-right alerts-box">
<a href="#" onclick="return false;"><img src="/templates/default/images/comments/envelope.png" alt=""/> <span class="badge badge-important">1</span> <b class="caret"></b></a>
<ul class="dropdown-menu">
    <li><a href="/index.php?module=articles_full&aid=3&clear_note=9"><strong>liamdawe</strong> replied to "test a test test a test test a test test a test te&hellip;</a>"</li>
    <li class="divider"></li><li><a href="/usercp.php?module=notifications&go=clear">Clear all</a></li>
    <li class="divider"></li>
    <li><a href="/usercp.php?module=notifications">See all</a></li>
</ul>
</li>
<!-- notes test -->
            </ul>
        </div>
    </div>

CSS 代码:

*, *:before, *:after {
    box-sizing: inherit;
}
.navigation-main {
    position: fixed;
    top: 0;
    width: 100%;
    height: 49px;
    z-index: 10001;
    background-color: #222;
}
.header-navbar {
    list-style: none;
    padding: 0;
    margin: 0;
}
.header-navbar>li {
    float: left;
}
.header-navbar>.pull-right {
    float: right;
}
.header-navbar>li>a {
    display: block;
    color: #999;
    padding: 14px;
}
.header-navbar>.active>a {
    color: #FFF;
    background-color: #000;
}
.header-navbar>li>a:hover {
    color: #FFF;
    text-decoration: none;
}
.header-navbar>.header-brand>a {
    line-height: 0;
    padding: 9px 14px;
    margin-left: -14px;
}
.header-search {
    padding: 9px 0;
}
.header-search .search-field {
    width: auto;
    background-color: #333;
    border: 1px solid #5c5c5c;
    outline: none;
    line-height: 19px;
    height: 30px;
    color: #fff;
    margin: 0;
}
.dropdown:hover .dropdown-menu {
    display: block;
}
.header .dropdown-menu {
    border-top-width: 1px;
}
.header .dropdown:hover .caret {
    border-top-color: #FFF;
}
.header-avatar {
    padding: 9px 0 9px 14px;
}
.dropdown {
    position: relative;
}
.caret {
    display: inline-block;
    width: 0;
    height: 0;
    vertical-align: middle;
    border-top: 4px solid #999;
    border-right: 4px solid transparent;
    border-left: 4px solid transparent;
    content: ""
}
.dropdown-menu {
    position: absolute;
    top: 49px;
    left: 0;
    z-index: 999;
    float: left;
    min-width: 160px;
    padding: 5px 0;
    margin: 0;
    list-style: none;
    text-align: left;
    background-color: #222;
    border: 1px solid #000;
    box-shadow: 0 3px 12px rgba(0, 0, 0, .15);
    display: none;
}
.dropdown-menu li {padding: 0 15px 2px;}
.dropdown-menu .divider {
    *width: 100%;
    height: 1px;
    padding: 0px;
    margin: 9px 0;
    *margin: -5px 0 5px;
    overflow: hidden;
    background-color: #383838;
}
.dropdown-menu a {
    display: block;
    clear: both;
    line-height: 26px;
    color: #999;
    white-space: nowrap;
    padding: 2px;
}
.dropdown-menu li>a:hover, .dropdown-menu li>a:focus {
    text-decoration: none;
    color: #fff;
}
.pull-right .dropdown-menu {
    right: 0;
    left: auto;
}
.pull-right {
    float: right;
}
.nav-avatar {
    vertical-align: middle;
    background-color: #333;
}

【问题讨论】:

    标签: html css list navbar dropdown


    【解决方案1】:

    使用 min-width: 或 max-width: 并可能允许您的文本换行

    类似的东西

    .dropdown-menu a {
        display: block;
        clear: both;
        line-height: 26px;
        color: #999;
        min-width: 200px;
        white-space: wrap;
        padding: 2px;
    }
    

    【讨论】:

    • 嗯,这肯定比我拥有的更好,让我可以控制更多。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多