HTML/CSS Horizontal navigation submenu hover wrong displayed

一笑奈何 提交于 2019-12-11 03:52:48

问题


I am creating an HTML page with a horizontal navigation and vertical submenu. Everything is working fine, except the fact, that the hover on the submenu is displayed to the left of the actual menu item.

See my jsfiddle: https://jsfiddle.net/qmcte349/

/* Navigation */
nav ul {
	list-style: none;
	background-color: #444;
	text-align: center;
	padding: 0;
	margin: 0;
}

nav li {
	line-height: 40px;
	text-align: left;
	width: 13%;
	border-bottom: none;
	height: 50px;
	display: inline-block;
	margin-right: -4px;
}

nav a {
	font-size: .8em;
	text-decoration: none;
	color: #fff;
	display: block;
	padding-left: 15px;
	border-bottom: none;
}

nav a:hover {
	background-color: #8e2323;
}

nav a.active {
	background-color: #aaa;
	color: #444;
	cursor: default;
}

nav > ul > li {
	text-align: center;
}

nav > ul > li > a {
	padding-left: 0;
}

/* Sub Menus */

nav li ul {
	position: absolute;
	display: none; 
	width: inherit;
}

nav li:hover ul {
	display: block; 
}

nav li ul li {
	display: block; 
}
			<nav>
				<ul>
					<li><a href="index.html">Home</a></li>
					<li><a href="#">Verein</a></li>
					<li><a href="#">Chronik</a></li>
					<li><a href="#">Termine</a>
						<ul>
							<li><a href="#">Veranstaltungen</a></li>
							<li><a href="#">Übungen</a></li>
						</ul>
					</li>
					<li><a href="#">Bilder und Videos</a></li>
					<li><a href="#">Links</a></li>
				</ul>
			</nav>

Thank you for your suggestions!


回答1:


Your problem goes from setting width to 13%. This way the li in the submenu also is 13% of its parrent width. Another issue you will see with margin-right: -4px; I suggest this code:

nav li {
    line-height: 40px;
    text-align: left;
    border-bottom: none;
    height: 50px;
    display: inline-block;
}


nav > ul > li {
    width: 13%;
  margin-right: -4px;
}

Set the things that are importaint for main menu (menu bar) only to it and not to its children.



来源:https://stackoverflow.com/questions/44175196/html-css-horizontal-navigation-submenu-hover-wrong-displayed

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