Firefox显示主页和下拉菜单之间的差距增加了行高

时间:2017-06-16 09:19:05

标签: html css firefox drop-down-menu flexbox

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解决方案?

1 个答案:

答案 0 :(得分:0)

使用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 */
}

&#13;
&#13;
/* 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;
}
&#13;
<!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>
&#13;
&#13;
&#13;

还包括工作小提琴

<强> fiddle link