gpt4 book ai didi

html - Firefox 显示主菜单和下拉菜单之间的差距,行高增加

转载 作者:行者123 更新时间:2023-11-28 03:33:56 25 4
gpt4 key购买 nike

In this jsfiddle我确实有一个菜单,它显示某些元素的下拉菜单。主菜单和子菜单项确实增加了高度。为此,我使用了 line-height 属性。

/* body ---------------------------------------------------------- */

body {
font-family: Calibri, sans-serif;
min-height: 100vh;
display: flex;
flex-direction: column;
margin: 0;
}


/* header ---------------------------------------------------------- */

header {
width: 100%;
}

.header-div {
width: 920px;
margin-left: auto;
margin-right: auto;
}


/* main-menu ---------------------------------------------------------- */

div.float-left-menu {
float: left;
clear: both;
}

ul#main-menu {
display: inline;
padding: 0;
position: relative;
font-size: 1.1em;
text-align: center;
}

ul#main-menu li {
display: inline-block;
line-height: 200%;
list-style: none;
vertical-align: middle;
width: 120px;
}

ul#main-menu li:hover {
background-color: #4f569d;
}

ul#main-menu li a {
background: none;
color: #4f569d;
text-decoration: none;
text-transform: uppercase;
}

ul#main-menu li span {
background: none;
color: #4f569d;
text-transform: uppercase;
}

ul#main-menu li:hover>a {
color: #fff;
text-decoration: none;
}

ul#main-menu li:hover>span {
color: #fff;
}

ul#main-menu li:hover>ul {
display: block;
position: absolute;
/*top: 138%; hack for FF, otherwise there is a gap between the main menu and the dropdown menu*/
}


/* header-submenu ---------------------------------------------------------- */

ul#header-submenu {
display: none;
padding: 0;
text-align: left;
z-index: 1;
}

ul#header-submenu li {
display: block;
line-height: 200%;
padding-left: 5%;
background-color: #bbb;
list-style: none;
vertical-align: middle;
width: 240px;
}

ul#header-submenu li:hover {
background-color: #4f569d;
}

ul #header-submenu li a {
background: none;
color: #4f569d;
text-decoration: none;
}

ul#header-submenu li:hover>a {
color: #fff;
text-decoration: none;
}
<header>
<div class="header-div">
<div class="float-left-menu">
<nav>
<ul id="main-menu">
<li><a href="~/">Item 1</a></li>
<li>
<span>Sub 1</span>
<ul id="header-submenu">
<li><a href="#">SItem 1</a></li>
<li><a href="#">SItem 2</a></li>
</ul>
</li>
<li>
<span>Sub 2</span>
<ul id="header-submenu">
<li><a href="#">SItem 1</a></li>
<li><a href="#">SItem 2</a></li>
</ul>
</li>
<li><a href="#">Item 2</a></li>
</ul>
</nav>
</div>
</div>
</header>

在Chrome、IE、Edge中,下拉菜单在其父项下方无缝显示,而Firefox则显示了一个间隙,不仅看起来不好看,而且当鼠标光标移动到那里进行选择时,下拉菜单也会消失。 “正常”高度不会出现此问题。
对于 line-height: 200%; 我能够通过将 top: 138%; 添加到下拉列表的 ul 来解决问题,但坦率地说,这种方法的尝试和错误太多了。
Firefox 是否有更清洁的解决方案?

最佳答案

使用position得到一样的

更新css部分

    ul#main-menu li {
display: inline-block;
line-height: 200%;
list-style: none;
vertical-align: middle;
width: 120px;
position:relative; /*add this*/
}

ul#header-submenu {
display: none;
padding: 0;
text-align: left;
z-index: 1;
position:absolute; /*add this*/
top:auto; /*add this you can change as your need */
left:0px; /*add this you can change as your need */
}

/* body ---------------------------------------------------------- */

body {
font-family: Calibri, sans-serif;
min-height: 100vh;
display: flex;
flex-direction: column;
margin: 0;
/* put the top margin into the header, otherwise there will always be a vertical scrollbar */
}


/* header ---------------------------------------------------------- */

header {
width: 100%;
}

.header-div {
width: 920px;
margin-left: auto;
margin-right: auto;
}


/* main-menu ---------------------------------------------------------- */

div.float-left-menu {
float: left;
clear: both;
}

ul#main-menu {
display: inline;
padding: 0;
position: relative;
font-size: 1.1em;
text-align: center;
}

ul#main-menu li {
display: inline-block;
line-height: 200%;
list-style: none;
vertical-align: middle;
width: 120px;
position: relative;
}

ul#main-menu li:hover {
background-color: #4f569d;
}

ul#main-menu li a {
background: none;
color: #4f569d;
text-decoration: none;
text-transform: uppercase;
}

ul#main-menu li span {
background: none;
color: #4f569d;
text-transform: uppercase;
}

ul#main-menu li:hover>a {
color: #fff;
text-decoration: none;
}

ul#main-menu li:hover>span {
color: #fff;
}

ul#main-menu li:hover>ul {
display: block;
position: absolute;
/*top: 138%; hack for FF, otherwise there is a gap between the main menu and the dropdown menu*/
}


/* header-submenu ---------------------------------------------------------- */

ul#header-submenu {
display: none;
padding: 0;
text-align: left;
z-index: 1;
position: absolute;
top: auto;
left: 0px;
}

ul#header-submenu li {
display: block;
line-height: 200%;
padding-left: 5%;
background-color: #eee;
list-style: none;
vertical-align: middle;
width: 240px;
}

ul#header-submenu li:hover {
background-color: #4f569d;
}

ul #header-submenu li a {
background: none;
color: #4f569d;
text-decoration: none;
}

ul#header-submenu li:hover>a {
color: #fff;
text-decoration: none;
}
<!DOCTYPE html>

<body>
<header>
<div class="header-div">
<div class="float-left-menu">
<nav>
<ul id="main-menu">
<li><a href="~/">Item 1</a></li>
<li>
<span>Sub 1</span>
<ul id="header-submenu">
<li><a href="#">SItem 1</a></li>
<li><a href="#">SItem 2</a></li>
</ul>
</li>
<li>
<span>Sub 2</span>
<ul id="header-submenu">
<li><a href="#">SItem 1</a></li>
<li><a href="#">SItem 2</a></li>
</ul>
</li>
<li><a href="#">Item 2</a></li>
</ul>
</nav>
</div>
</div>
</header>
</body>

</html>

还包括工作 fiddle

fiddle link

关于html - Firefox 显示主菜单和下拉菜单之间的差距,行高增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44585494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com