简体   繁体   English

下拉,适应父级的宽度?

[英]Drop down, adapt width from parent?

So I am trying to get my dropdown to work properly. 因此,我正在尝试使我的下拉菜单正常工作。 The dropdown is under a list-item in my header that changes depending on what username the user have. 下拉列表位于我标题的列表项下,该列表项会根据用户使用的用户名而变化。 That makes the size differ but I can't get the dropdown to follow in the width resize. 这使大小有所不同,但我无法按照下拉菜单调整宽度。

Example 1: http://jsfiddle.net/73tUx/ 示例1: http//jsfiddle.net/73tUx/

Here does it work properly but what if... 它在这里正常工作,但是如果...

Example 2: http://jsfiddle.net/73tUx/1/ 示例2: http//jsfiddle.net/73tUx/1/

As you can see in example 2 the dropdown is wider than the list-item. 如示例2所示,下拉列表比列表项宽。 How can I make it change width as well? 如何使它也改变宽度?

CSS: CSS:

header {
height: 72px;
width: 100%;
z-index: 999;
position: relative;
background-color: #2C3E50;
}

header ul {
margin: 0;
padding: 0;
list-style-type: none;
width: 600px;
height: 72px;
margin-left: auto;
margin-right: auto;
}

.navItem {
float: left;
padding-left: 35px;
padding-right: 35px;
padding-top: 26px;
padding-bottom: 26px;
text-decoration: none;
color: #FFF;
}

.navItem:hover {
background-color: #34495e;
}

header ul li a.right {
float: right;
}

.dropdown {
list-style-type: none;
}

.drop {
position: absolute;
float: right;
background: #2C3E50;
margin-top: 72px;
right: 103px;
width: 150px;
height: 140px;
display: none;
z-index: -10;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;

}

.drop li {
list-style-type: none;
height: 25px;
padding-top: 10px;
padding-left: 20px;
width: 123px;
}

.drop li a {
color: #FFF;
text-decoration: none;
}

.drop li:hover {
background: #34495e;
}
header ul{width:100%;position:relative;}

DEMO 演示

I made few changes here and there in css. 我在CSS中到处都没有做任何更改。 Major one was moving padding and float from anchor to the list. 主要的是将填充和浮动从锚点移动到列表。

Note: That the dropdown div is so small that i had to add overflow auto to see the hidden content. 注意:下拉div非常小,我不得不添加自动溢出功能以查看隐藏的内容。

CSS: CSS:

header {
    height: 72px;
    width: 100%;
    z-index: 999;
    position: relative;
    background-color: #2C3E50;
}

header ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    width: 600px;
    height: 72px;
    margin-left: auto;
    margin-right: auto;
}
ul > li{
    float: left;
    padding-left: 35px;
    padding-right: 35px;
    padding-top: 26px;
    padding-bottom: 26px;
    position:relative;
}
.navItem {  
    text-decoration: none;
    color: #FFF;
}

.navItem:hover {
    background-color: #34495e;
}

header ul li a.right {
    float: right;
}

.dropdown {
    list-style-type: none;
}

.drop {
    background: none repeat scroll 0 0 #2C3E50;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    display: none;
    float: right;
    height: 140px;
    margin-top: 46px;
    overflow: auto;
    position: absolute;
    right: 0;
    width: 100%;
    z-index: -10;
}

.drop li {
    list-style-type: none;
    height: 25px;
    padding-top: 10px;
    padding-left: 20px;
    width: 123px;
}

.drop li a {
    color: #FFF;
    text-decoration: none;
}

.drop li:hover {
    background: #34495e;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM