简体   繁体   English

如何使用js使下拉菜单的宽度等于父div

[英]how to make dropdown sub menu width equal to the parent div with js

I have a CSS and HTML drop-down menu and I want to keep the width of my sub menus equal to their parent. 我有一个CSS和HTML下拉菜单,我想使子菜单的宽度等于其父菜单的宽度。 I tried several ways but nothing works for me. 我尝试了几种方法,但对我没有用。 If its possible to do it whether with CSS or with JS its fine for me here is my code 如果可能的话,无论是使用CSS还是使用JS,对我来说这都是我的代码

HTML 的HTML

<nav>
    <ul class="navi_main">
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a>
            <ul class="sub_navi_main">
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
            </ul>
        </li>
        <li><a href="#">Products</a>
            <ul class="sub_navi_main">
                <li><a href="#">Prod 1</a></li>
                <li><a href="#">Prod 2</a></li>
            </ul>                        
                        </li>
        <li><a href="#">About Us</a></li>
        <li><a href="#">Contact Us</a></li>
    </ul>
</nav>

CSS 的CSS

.navi_main{
    display:block;
}

.navi_main li{
    display: table-cell;
    z-index: 100;
}

.navi_main li:first-child{
margin-left:0;
}

.navi_main li a {
    text-decoration: none;
    display: block;
    font-size: 16px;
    color: #8098b1;
    padding: 0 14px;
    height: 64px;
    line-height: 64px;
    border-right: 1px solid #e6e6e6;
    transition: all 0.3s;
    -moz-transition: all 0.3s;
    -webkit-transition: all 0.3s;
    text-shadow: 0 1px 1px rgba(255,255,255,.64);
    text-transform: uppercase;
    position: relative;
}

.navi_main li a:hover, .navi_main li:hover > a{
    color:#ffffff;
    background:#9CA3DA;
}


.navi_main ul {
    display: none;
    margin: 0;
    padding: 0;
    width: 100%;
    position:relative;
    background: #ffffff;
    z-index:999999;
}

.navi_main ul li {
    display:block;
    float: none;
    background:none;
    margin:0;
    padding:0;
}
.navi_main ul li a {
    font-size: 14px;
    font-weight: normal;
    display: block;
    color: #797979;
    border-left: 3px solid #ffffff;
    background: #ffffff;
    height:40px;
    line-height:40px;
}

.navi_main ul li a:hover, .navi_main ul li:hover > a{
    background:#f0f0f0;
    border-left:3px solid #9CA3DA;
    color:#797979;
}

.navi_main li > ul.visible{
    display: block;
    position:absolute;
}
.navi_main ul ul {
    left: 149px;
    top: 0px;
}

OK...I've simplified the CSS a little as you had some reduntant rules in there and made the CSS selectors somewhat more specific and I think this is what you are looking for. 好的...我已经简化了CSS,因为那里有一些多余的规则,并使CSS选择器更加具体 ,我想这就是您想要的。

Note: If you want multiline text you really can't set a line-height equal to the height . 注意:如果要使用多行文字,则实际上不能将line-height设置为height I took all the height statements and let the link sizing (with padding) determine the actual heigts. 我记下了所有的高度声明,然后让链接大小(带有填充)确定实际的高度。

I've made one of the sub-menus visible so you don't have to hover to see it. 我已经使子菜单之一可见,因此您不必将鼠标悬停即可看到它。

 * { box-sizing: border-box; } ul { list-style-type: none; margin: 0; padding: 0; } a { text-decoration: none; } .navi_main { /* display: block;*/ /* not required */ } .navi_main > li { display: table-cell; position: relative; /* positioning context */ } .navi_main > li > a { text-decoration: none; display: block; font-size: 16px; color: #8098b1; padding: 16px; border-right: 1px solid #e6e6e6; transition: all 0.3s; -moz-transition: all 0.3s; -webkit-transition: all 0.3s; text-shadow: 0 1px 1px rgba(255, 255, 255, .64); text-transform: uppercase; } .navi_main li a:hover, .navi_main li:hover > a { color: #ffffff; background: #9CA3DA; } .navi_main li > ul { position: absolute; top: 100%; left: 0; display: none; width: 100%; background: #ff0000; } .navi_main li:hover > ul { display:block; } .navi_main ul > li { display: block; background: #bada55; } .navi_main ul > li > a { font-size: 14px; font-weight: normal; display: block; color: #797979; background: #ffffff; padding:8px 16px; border-left:2px solid white; } .navi_main ul li a:hover { background: #f0f0f0; color: #797979; border-left:2px solid #9CA3DA; } .navi_main li > ul.visible { display: block; } 
 <nav> <ul class="navi_main"> <li><a href="#">Home</a></li> <li><a href="#">About</a> <ul class="sub_navi_main"> <li><a href="#">Page 1</a></li> <li><a href="#">Page 2</a></li> </ul> </li> <li><a href="#">Products</a> <ul class="sub_navi_main visible"> <li><a href="#">Lorem ipsum dolor sit amet.</a></li> <li><a href="#">Prod 2</a></li> </ul> </li> <li><a href="#">About Us</a></li> <li><a href="#">Contact Us</a></li> </ul> </nav> 

Codepen Demo Codepen演示

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

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