Overflow hidden hides dropdown, but taking overflow hidden off hide navigation background

守給你的承諾、 提交于 2021-02-09 02:45:17

问题


First off, here's a .js fiddle: http://jsfiddle.net/B6DSv/

The issue I am having is with my .css:

nav {
    overflow: hidden; /*THIS LINE*/
    background-color: #004b98;
    width: 100%;
    margin: 0;
    padding: 0;
}

and here:

<nav>
    <ul>
        <li><a href="index.html">Home</a>
            <ul>
                <li><a href="#">teadsfasdfadsst</a></li>
            </ul>
        </li>

        <li><a href="#">Gallery</a></li>
        <li><a href="#">Map</a></li>
    </ul>
</nav>

If I take off overflow: hidden;, the dropdown works... But my background is taken off.


回答1:


Since the children elements are floated (taken out of the document flow), the parent element, nav, collapses upon itself; thus, the background isn't shown because nav has a height of 0.

Rather than using overflow:hidden to fix this, just add a clearfix to the element instead:

Updated Example

nav:after {
    content:'';
    clear:both;
    display:table;
}



回答2:


Clearfix will help

http://nicolasgallagher.com/micro-clearfix-hack/

http://jsfiddle.net/B6DSv/1/

.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.cf {
    *zoom: 1;
}


来源:https://stackoverflow.com/questions/23553363/overflow-hidden-hides-dropdown-but-taking-overflow-hidden-off-hide-navigation-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!