How do I force nested list items to be the same wi

2019-04-06 11:55发布

I have a horizontal parent list. Some of the list items display a nested vertical list when clicked. How do I force the items in the vertical sub list to be the same width as the parent list item?

See jsFiddle.

enter image description here

HTML:

<ul class="mainMenu horizontalMenu bulletless fullWidth bold">
    <li class="showSubMenu">
        <div>Resumes &amp; Cover Letters &#x25BE; </div>
        <ul class="mainSubMenu bulletless">
            <li><a>Resumes</a></li>
            <li><a>Cover Letters</a></li>
            <li><a>Interviews</a></li>
        </ul>
    </li><li><a>Other Link</a>
    </li><li><a>Other Link</a></li>
 </ul>​

CSS:

.horizontalMenu li{    
    display: inline-block;
}
.mainMenu > li{
    border: 1px solid black;
}
.mainMenu a, .mainMenu div{
    display: block;
    padding: 10px 20px;

}
.mainSubMenu{
    position: absolute;
}

3条回答
别忘想泡老子
2楼-- · 2019-04-06 12:19

I did the change on your fiddle. http://jsfiddle.net/BXnxc/2/

The parent li needs to have position:relative; and the nested submenu has to have width:100%; and position:absolute;

查看更多
欢心
3楼-- · 2019-04-06 12:38

You can also do this by inheriting the width of the containing structures. I set your mainMenu div to width of 200px and then width:inherit for the mainSubMenu.

http://jsfiddle.net/BXnxc/4/

.mainMenu a, .mainMenu div{
    display: block;
    padding: 10px 20px; 
    width:200px;
}
.mainSubMenu{
    position: absolute;
    width:inherit;
}

.mainSubMenu li
{
    display: block;
    border: 1px solid grey; 
    width:inherit; 
}
查看更多
\"骚年 ilove
4楼-- · 2019-04-06 12:44

You can do this by specifying the parent LI as relative and child UL with width: 100%

Demo: http://jsfiddle.net/BXnxc/3/

.horizontalMenu li {    
    position: relative;
}
.horizontalMenu li ul {    
    width: 100%;   
}
查看更多
登录 后发表回答