【问题标题】:Navbar dropdown width resize导航栏下拉宽度调整大小
【发布时间】:2017-11-30 13:09:16
【问题描述】:

我有一个在桌面设备和移动设备上都能流畅运行的导航栏。但是,下拉项的宽度不会根据父项的宽度调整大小。我想将鼠标悬停在一个下拉项目上,它的宽度与我悬停的列表项目的宽度相同。目前它有一个固定的宽度,但我似乎无法通过尝试其他选项来让它工作。我也无法让整个导航栏居中,因为目前它被卡在左边。感谢您的帮助!

这是代码的codepen:https://codepen.io/Macast/pen/rYQPNe

HTML:

<nav>
        <div id="logo">
            <img src="images/J.-Freeman-&-Son-Landscape-Logo-White.png">
        </div>

        <label for="drop" class="toggle">Menu</label>
        <input type="checkbox" id="drop" />
        <ul class="menu">
            <li><a href="#">Home</a></li>
            <li>
                <!-- First Tier Drop Down -->
                <label for="drop-1" class="toggle">Short +</label>
                <a href="#">Short</a>
                <input type="checkbox" id="drop-1" />
                <ul>
                    <li><a href="#">History</a></li>
                    <li><a href="#">Our Services</a></li>
                    <li><a href="#">Our Aim</a></li>
                </ul>

            </li>
            <li>
                <!-- First Tier Drop Down -->
                <label for="drop-2" class="toggle">Dropdown Even Longer +</label>
                <a href="#">Dropdown Even Longer</a>
                <input type="checkbox" id="drop-2" />
                <ul>
                    <li><a href="#">Option</a></li>
                    <li><a href="#">Option</a></li>
                    <li><a href="#">Option</a></li>
                    <li>
                        <!-- Second Tier Drop Down -->
                        <label for="drop-3" class="toggle">More Options +</label>
                        <a href="#">More Options</a>
                        <input type="checkbox" id="drop-3" />

                        <ul>
                            <li><a href="#">Option</a></li>
                            <li><a href="#">Option</a></li>
                            <li><a href="#">Option</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li><a href="#">Option</a></li>
            <li><a href="#">Option</a></li>
            <li><a href="#">Option</a></li>
            <li><a href="#">Option</a></li>
        </ul>
    </nav>

CSS:

.toggle,
[id^=drop] {
    display: none;
}
/* Giving a background-color to the nav container. */

nav {
    margin: 0;
    padding: 0;
    background-color: #254441;
}
#logo {
    display: block;
    text-align: center;
    /*padding: 0 30px;*/
    /*float: left;*/
    /*font-size: 20px;*/
    /*line-height: 60px; */
}
#logo img {
    width: 30%;
}
/* Since we'll have the "ul li" "float:left"
 * we need to add a clear after the container. */

nav:after {
    content: "";
    display: table;
    clear: both;
}
/* Removing padding, margin and "list-style" from the "ul",
 * and adding "position:reltive" */

nav ul {
    /*float: right;*/
    padding: 0;
    margin: 0;
    list-style: none;
    position: relative;
}
/* Positioning the navigation items inline */

nav ul li {
    margin: 0px;
    display: inline-block;
    float: left;
    background-color: #254441;
    text-align: center;
}
/* Styling the links */

nav a {
    display: block;
    padding: 14px 20px;
    color: #FFF;
    font-size: 17px;
    text-decoration: none;
    text-align: center;
}
nav ul li ul li:hover {
    background: #000000;
}
/* Background color change on Hover */

nav a:hover {
    background-color: #000000;
}
/* Hide Dropdowns by Default
 * and giving it a position of absolute */

nav ul ul {
    display: none;
    position: absolute;
    /* has to be the same number as the "line-height" of "nav a" */
    top: 60px;
}
/* Display Dropdowns on Hover */

nav ul li:hover > ul {
    display: inherit;
}
/* Fisrt Tier Dropdown */

nav ul ul li {
    width: 170px;
    float: none;
    display: list-item;
    position: relative;
    text-align: center;
}
/* Second, Third and more Tiers 
 * We move the 2nd and 3rd etc tier dropdowns to the left
 * by the amount of the width of the first tier.
*/

nav ul ul ul li {
    position: relative;
    top: -60px;
    /* has to be the same number as the "width" of "nav ul ul li" */

    left: 170px;
}
/* Change ' +' in order to change the Dropdown symbol */

li > a:after {
    content: ' +';
}
li > a:only-child:after {
    content: '';
}
/* Media Queries
--------------------------------------------- */

@media all and (max-width: 768px) {
    #logo {
        display: block;
        padding: 0;
        width: 100%;
        text-align: center;
        float: none;
    }
    #logo img {
        width: 100%;
        box-sizing: border-box;
        padding: 20px;
    }
    nav {
        margin: 0;
    }
    /* Hide the navigation menu by default */
    /* Also hide the  */

    .toggle + a,
    .menu {
        display: none;
    }
    /* Stylinf the toggle lable */

    .toggle {
        display: block;
        background-color: #254441;
        padding: 14px 20px;
        color: #FFF;
        font-size: 17px;
        text-decoration: none;
        border: none;
        text-align: center;
    }
    .toggle:hover {
        background-color: #000000;
    }
    /* Display Dropdown when clicked on Parent Lable */

    [id^=drop]:checked + ul {
        display: block;
    }
    /* Change menu item's width to 100% */

    nav ul li {
        display: block;
        width: 100%;
    }
    nav ul ul .toggle,
    nav ul ul a {
        padding: 0 40px;
    }
    nav ul ul ul a {
        padding: 0 80px;
    }
    nav a:hover,
    nav ul ul ul a {
        background-color: #000000;
    }
    nav ul li ul li .toggle,
    nav ul ul a,
    nav ul ul ul a {
        padding: 14px 20px;
        color: #FFF;
        font-size: 17px;
    }
    nav ul li ul li .toggle,
    nav ul ul a {
        background-color: #212121;
    }
    /* Hide Dropdowns by Default */

    nav ul ul {
        float: none;
        position: static;
        color: #ffffff;
        /* has to be the same number as the "line-height" of "nav a" */
    }
    /* Hide menus on hover */

    nav ul ul li:hover > ul,
    nav ul li:hover > ul {
        display: none;
    }
    /* Fisrt Tier Dropdown */

    nav ul ul li {
        display: block;
        width: 100%;
    }
    nav ul ul ul li {
        position: static;
        /* has to be the same number as the "width" of "nav ul ul li" */
    }
}
@media all and (max-width: 330px) {
    nav ul li {
        display: block;
        width: 94%;
    }
}

【问题讨论】:

  • 告诉我,如果你在nav ul ul 上声明了以下样式position: absolute; left: 0; right: 0;,那么删除nav ul ul li 上声明的显式width,你得到预期的行为?此外,请确保在包含元素 nav ul li 上声明 position: relative
  • 删除 nav ul ul li 上的宽度只是将宽度缩小到每个列表项中文本的大小,而不是下拉父级的宽度。我已经根据您的调整更新了代码,但问题仍然存在:codepen.io/Macast/pen/rYQPNe
  • 您是否在nav ul ul 上声明了left: 0; right: 0;(按照我的建议)- 还是您只是从nav ul ul li 中删除了宽度?在此处查看实际演示:codepen.io/UncaughtTypeError/pen/yPQZja(您的 CodePen 分叉)
  • 我没有看到我在您的“更新的 codepen”中推荐的任何调整。甚至下拉列表项的width 仍然是明确定义的。你确定你已经更新了吗?也许你错过了它,但最好参考我之前评论中包含的分叉笔来观察调整后的行为。 (codepen.io/UncaughtTypeError/pen/yPQZja)
  • 抱歉,我更新了它,但它一定没有正确保存,或者我忘了做一些简单的事情(我以前只使用过 CodePen 一次)。现在确实有效,非常感谢!

标签: html css navigation width navbar


【解决方案1】:

要将下拉菜单大小调整为与包含列表项相同的大小,请在相关下拉元素 (nav ul ul) 上声明 leftright 属性值 0,然后删除在下拉菜单的嵌套列表项上显式声明 width (nav ul ul li)。

示例:

nav ul ul {
    display: none;
    position: absolute;
    top: 60px;
    /* additional property values declared */
    left: 0;
    right: 0;
}

之所以有效,是因为下拉菜单已经定位在absolute 并且包含的父元素列表项 (nav ul li)已经定位在 relative .

如果他们的父母是relative,你可以将absolute元素定位到他们的父母相对

所以我们在这里所做的只是将下拉菜单宽度的范围定义为从父级宽度的“左到右”“拉伸”

代码片段演示:

.toggle,
[id^=drop] {
    display: none;
}
/* Giving a background-color to the nav container. */

nav {
    margin: 0;
    padding: 0;
    background-color: #254441;
}
#logo {
    display: block;
    text-align: center;
    /*padding: 0 30px;*/
    /*float: left;*/
    /*font-size: 20px;*/
    /*line-height: 60px; */
}
#logo img {
    width: 30%;
}
/* Since we'll have the "ul li" "float:left"
 * we need to add a clear after the container. */

nav:after {
    content: "";
    display: table;
    clear: both;
}
/* Removing padding, margin and "list-style" from the "ul",
 * and adding "position:reltive" */

nav ul {
    text-align: center;
    padding: 0;
    margin: 0;
    list-style: none;
    position: relative;
}
/* Positioning the navigation items inline */

nav ul li {
    margin: 0px;
    display: inline-block;
    background-color: #254441;
    text-align: center;
    position: relative;
}
/* Styling the links */

nav a {
    display: block;
    padding: 14px 20px;
    color: #FFF;
    font-size: 17px;
    text-decoration: none;
    text-align: center;
}
nav ul li ul li:hover {
    background: #000000;
}
/* Background color change on Hover */

nav a:hover {
    background-color: #000000;
}
/* Hide Dropdowns by Default
 * and giving it a position of absolute */

nav ul ul {
    display: none;
    position: absolute;
    /* has to be the same number as the "line-height" of "nav a" */
    top: 60px;
    left: 0;
    right: 0;
}
/* Display Dropdowns on Hover */

nav ul li:hover > ul {
    display: inherit;
}
/* Fisrt Tier Dropdown */

nav ul ul li {
    /*width: 170px;*/
    float: none;
    display: list-item;
    position: relative;
    text-align: center;
}
/* Second, Third and more Tiers	
 * We move the 2nd and 3rd etc tier dropdowns to the left
 * by the amount of the width of the first tier.
*/

nav ul ul ul li {
    position: relative;
    top: -60px;
    /* has to be the same number as the "width" of "nav ul ul li" */
    
    left: 170px;
}
/* Change ' +' in order to change the Dropdown symbol */

li > a:after {
    content: ' +';
}
li > a:only-child:after {
    content: '';
}
/* Media Queries
--------------------------------------------- */

@media all and (max-width: 768px) {
    #logo {
        display: block;
        padding: 0;
        width: 100%;
        text-align: center;
        float: none;
    }
    #logo img {
        width: 100%;
        box-sizing: border-box;
        padding: 20px;
    }
    nav {
        margin: 0;
    }
    /* Hide the navigation menu by default */
    /* Also hide the  */
    
    .toggle + a,
    .menu {
        display: none;
    }
    /* Stylinf the toggle lable */
    
    .toggle {
        display: block;
        background-color: #254441;
        padding: 14px 20px;
        color: #FFF;
        font-size: 17px;
        text-decoration: none;
        border: none;
        text-align: center;
    }
    .toggle:hover {
        background-color: #000000;
    }
    /* Display Dropdown when clicked on Parent Lable */
    
    [id^=drop]:checked + ul {
        display: block;
    }
    /* Change menu item's width to 100% */
    
    nav ul li {
        display: block;
        width: 100%;
    }
    nav ul ul .toggle,
    nav ul ul a {
        padding: 0 40px;
    }
    nav ul ul ul a {
        padding: 0 80px;
    }
    nav a:hover,
    nav ul ul ul a {
        background-color: #000000;
    }
    nav ul li ul li .toggle,
    nav ul ul a,
    nav ul ul ul a {
        padding: 14px 20px;
        color: #FFF;
        font-size: 17px;
    }
    nav ul li ul li .toggle,
    nav ul ul a {
        background-color: #212121;
    }
    /* Hide Dropdowns by Default */
    
    nav ul ul {
        float: none;
        position: static;
        color: #ffffff;
        /* has to be the same number as the "line-height" of "nav a" */
    }
    /* Hide menus on hover */
    
    nav ul ul li:hover > ul,
    nav ul li:hover > ul {
        display: none;
    }
    /* Fisrt Tier Dropdown */
    
    nav ul ul li {
        display: block;
        width: 100%;
    }
    nav ul ul ul li {
        position: static;
        /* has to be the same number as the "width" of "nav ul ul li" */
    }
}
@media all and (max-width: 330px) {
    nav ul li {
        display: block;
        width: 94%;
    }
}
<nav>
            <div id="logo">
                <img src="images/J.-Freeman-&-Son-Landscape-Logo-White.png">
            </div>

            <label for="drop" class="toggle">Menu</label>
            <input type="checkbox" id="drop" />
            <ul class="menu">
                <li><a href="#">Home</a></li>
                <li>
                    <!-- First Tier Drop Down -->
                    <label for="drop-1" class="toggle">Short +</label>
                    <a href="#">Short</a>
                    <input type="checkbox" id="drop-1" />
                    <ul>
                        <li><a href="#">History</a></li>
                        <li><a href="#">Our Services</a></li>
                        <li><a href="#">Our Aim</a></li>
                    </ul>

                </li>
                <li>
                    <!-- First Tier Drop Down -->
                    <label for="drop-2" class="toggle">Dropdown Even Longer +</label>
                    <a href="#">Dropdown Even Longer</a>
                    <input type="checkbox" id="drop-2" />
                    <ul>
                        <li><a href="#">Option</a></li>
                        <li><a href="#">Option</a></li>
                        <li><a href="#">Option</a></li>
                        <li>
                            <!-- Second Tier Drop Down -->
                            <label for="drop-3" class="toggle">More Options +</label>
                            <a href="#">More Options</a>
                            <input type="checkbox" id="drop-3" />

                            <ul>
                                <li><a href="#">Option</a></li>
                                <li><a href="#">Option</a></li>
                                <li><a href="#">Option</a></li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Option</a></li>
                <li><a href="#">Option</a></li>
                <li><a href="#">Option</a></li>
                <li><a href="#">Option</a></li>
            </ul>
        </nav>

另外:
您可以通过删除嵌套列表项 (nav ul li) 上声明的 float 规则将导航菜单水平居中,因为这将否定任何对齐内容的尝试(除非您使用 flex-box),然后声明 text-align: center on包含无序列表(.menu),如下所示:

nav ul {
    text-align: center;
    padding: 0;
    margin: 0;
    list-style: none;
    position: relative;
}

【讨论】:

  • 感谢您的回答和解释,而不是像其他贡献者那样只显示代码而不解释。再次感谢!
  • @Macast 我很欣赏一个花里胡哨的家伙,他很欣赏骨头上的肉需要时间来充实的答案。
猜你喜欢
  • 2016-08-23
  • 2013-06-02
  • 2019-08-19
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多