html - 光标悬停时如何阻止主菜单扩展

标签 html css

我正在玩这个导航栏,不知何故,当您将鼠标悬停在它的顶部时,“关于和投资组合”菜单会一直向侧面延伸。 我想阻止它扩展。 您可以从 JSFiddle 检查代码。提前致谢。

https://jsfiddle.net/JustCraze/gdc56899/1/#

HTML

 <ul id="main-nav">
                    <li>
                        <a href="#">Home</a>
                    </li>
                    <li>
                        <a href="#">About ↓</a>
                        <ul class="hidden">
                            <li>
                                <a href="#">Who We Are</a>
                            </li>
                            <li>
                                <a href="#">What We Do</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Portfolio ↓</a>
                        <ul class="hidden">
                            <li>
                                <a href="#">Photography</a>
                            </li>
                            <li>
                                <a href="#">Web & User Interface Design</a>
                            </li>
                            <li>
                                <a href="#">Illustration</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">News</a>
                    </li>
                    <li>
                        <a href="#">Contact</a>
                    </li>
                </ul>

CSS

#wrap-nav {
  display: block;
  margin:2rem;
  z-index: 900;
  padding: 5rem;
}
#main-nav {
  display: block;
  z-index: 950;
  padding: 1rem;
  float: right;
}
/*Strip the ul of padding and list styling*/
ul {
    list-style-type:none;
    margin:0;
    padding:.1rem;
}

/*Create a horizontal list with spacing*/
li {
    display:inline-block;
    float: left;
    margin-right: 1px;
}

/*Style for menu links*/
li a {
    display:block;
    min-width:140px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #fff;
    background: #2f3036;
    text-decoration: none;
}

/*Hover state for top level links*/
li:hover a {
    background: #19c589;
  z-index: 980;
}

/*Style for dropdown links*/
li:hover ul a {
    background: #f3f3f3;
    color: #2f3036;
    height: 40px;
    line-height: 40px;
  z-index: 980;
}

/*Hover state for dropdown links*/
li:hover ul a:hover {
    background: #19c589;
    color: #fff;
  z-index: 980;
}

/*Hide dropdown links until they are needed*/
li ul {
    display: none;
}

/*Make dropdown links vertical*/
li ul li {
    display: block;
    float: none;
}

/*Prevent text wrapping*/
li ul li a {
    width: auto;
    min-width: 100px;
    padding: 0 20px;
}

/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
    display: block;
}

/*Responsive Styles*/

@media screen and (max-width : 250px){
    /*Make dropdown links appear inline*/
    ul {
        position: static;
        display: none;
    }
    /*Create vertical spacing*/
    li {
        margin-bottom: 1px;
    }
    /*Make all menu links full width*/
    ul li, li a {
        width: 100%;
    }

最佳答案

诀窍是使 li ul 位置绝对化,这样它们就不会占用空间。

#wrap-nav {
  display: block;
  margin:2rem;
  z-index: 900;
  padding: 5rem;
}
#main-nav {
  display: block;
  z-index: 950;
  padding: 1rem;
  float: right;
}
/*Strip the ul of padding and list styling*/
ul {
	list-style-type:none;
	margin:0;
	padding:.1rem;
}

/*Create a horizontal list with spacing*/
li {
	display:inline-block;
	float: left;
	margin-right: 1px;
}

/*Style for menu links*/
li a {
	display:block;
	min-width:140px;
	height: 50px;
	text-align: center;
	line-height: 50px;
	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	color: #fff;
	background: #2f3036;
	text-decoration: none;
}

/*Hover state for top level links*/
li:hover a {
	background: #19c589;
  z-index: 980;
}

/*Style for dropdown links*/
li:hover ul a {
	background: #f3f3f3;
	color: #2f3036;
	height: 40px;
	line-height: 40px;
  z-index: 980;
}

/*Hover state for dropdown links*/
li:hover ul a:hover {
	background: #19c589;
	color: #fff;
  z-index: 980;
}

/*Hide dropdown links until they are needed*/
li ul {
	display: none;
    /*this is the trick*/
    position:absolute;
}

/*Make dropdown links vertical*/
li ul li {
	display: block;
	float: none;
}

/*Prevent text wrapping*/
li ul li a {
	width: auto;
	min-width: 100px;
	padding: 0 20px;
}

/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
	display: block;
}

/*Responsive Styles*/

@media screen and (max-width : 250px){
	/*Make dropdown links appear inline*/
	ul {
		position: static;
		display: none;
	}
	/*Create vertical spacing*/
	li {
		margin-bottom: 1px;
	}
	/*Make all menu links full width*/
	ul li, li a {
		width: 100%;
	}
<ul id="main-nav">
                    <li>
                        <a href="#">Home</a>
                    </li>
                    <li>
                        <a href="#">About ↓</a>
                        <ul class="hidden">
                            <li>
                                <a href="#">Who We Are</a>
                            </li>
                            <li>
                                <a href="#">What We Do</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Portfolio ↓</a>
                        <ul class="hidden">
                            <li>
                                <a href="#">Photography</a>
                            </li>
                            <li>
                                <a href="#">Web & User Interface Design</a>
                            </li>
                            <li>
                                <a href="#">Illustration</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">News</a>
                    </li>
                    <li>
                        <a href="#">Contact</a>
                    </li>
                </ul>

关于html - 光标悬停时如何阻止主菜单扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38798485/

相关文章:

html - 如何为子元素设置 html5 网页过滤器?

javascript - 提交请求后如何查看完整的 URL?

css - wordpress 样式表中的 "textwidget"类在哪里?

html - 在 CSS 中 float 一个 DIV

html - 是否有特定于 tr 行的 CSS 选择器的语法?

html - Bootstrap/CSS 中固定宽度标签中的自动换行

html - Bootstrap 导航栏折叠

html - 将 css 应用于 html 而不是 iframe html

html - 宽度为零的按钮仍会影响 Safari 中父级的宽度

javascript - HTML CSS 用 ul 包装 li