【问题标题】:Drop down width same as parent下拉宽度与父级相同
【发布时间】:2013-08-10 03:53:54
【问题描述】:

我正在创建一个下拉菜单,顶级列表项将显示一个用户名。

我有一点问题,因为我希望下拉菜单与悬停在顶级列表项上的宽度相同。

但是,如果我将 ul li ul li 设置为宽度 100%,它将占用整个容器的 100%,而不是其上方的 ul。我想是因为 ul ul 的位置是绝对的?

任何想法如何使下拉菜单的宽度与父列表项的宽度相同?

The page can be seen here.

这是 HTML

<ul>
    <li><a href="#"><span aria-hidden="true" data-icon="a"> A long name here</a>
    <ul>
        <li><a href="#">View Profile</a></li>
        <li><a href="#">Edit Profile</a></li>
        <li><a href="#">Settings</a></li>
        <li><a href="#">My Payments</a></li>
    </ul>
    </li>

    <li><a href=""> <span aria-hidden="true" data-icon="c"> Online</a></li>
    <li><a href="#"> Log Out</a></li>

</ul>

CSS

.head-link ul ul {
    display: none;
}

.head-link ul li:hover > ul {
    display: block;
}

.head-link ul {  
    list-style: none;
    position: relative;
    display: inline-table;
}

    .head-link ul li {
    float: left;
    border-top-left-radius: 0.5em;
    border-top-right-radius: 0.5em;
    height: 2em;
    width: auto;
    padding: 0.5em 0.98em;
}

.head-link ul li:hover {
    background: #ffffff;
}

.head-link ul li:hover a {
    color: #434343;
    border-bottom: none;
}

.head-link ul li a {
    display: block; 
    color: #ffffff; 
    text-decoration: none;
}

.head-link ul ul {
    background: #ffffff; 
    border-radius: 0px; 
    padding: 0;
    position: absolute; 
    top: 99%;
    left: 0;
    text-align: left;
    border-bottom-left-radius: 0.5em;
    border-bottom-right-radius: 0.5em;
    padding-bottom: 0.8em;
    padding-top: 0.5em;
    box-shadow: 0px 0.3em 0.4em rgba(0,0,0,0.3);
}

.head-link ul ul li {
    float: none; 
    position: relative;
    padding: 0em;
}

.head-link ul ul li a {
    padding: 0.5em 1.24em;
    color: #434343;
    border-bottom-left-radius: 0.5em;
    border-bottom-right-radius: 0.5em;
}   

.head-link ul ul li a:hover {
    background: #ffffff;
    text-decoration: underline;
}

.head-link ul ul ul {
    position: absolute; 
    left: 100%; 
    top:0;
}

【问题讨论】:

    标签: css drop-down-menu width css-position


    【解决方案1】:

    我是这样弄的:

    只需修改这些规则:

    .head-link ul li {
        position: relative;
        ... /* Keep all current rules */
    }
    .head-link ul li ul {
        width: 100%;
        ... /* Keep all current rules */
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-02
      • 2015-07-29
      • 2021-02-09
      • 2015-03-20
      • 1970-01-01
      • 2023-02-19
      • 2013-12-07
      • 2017-08-15
      相关资源
      最近更新 更多