html - CSS:如何删除子菜单的顶部间隙?

标签 html css

我有一个 code snippet 垂直弹出菜单,并希望避免子菜单的顶部空白

我在 floatpositiontop 方面取得了一些进展,但在一些肮脏的代码中迷失了方向。

这就是想要的结果:

enter image description here

解决问题的最佳/最佳/常见做法是什么?提前致谢!

/* Define the body style */
body {
  font-family:Arial;
  font-size:12px;
}

/* We remove the margin, padding, and list style of UL and LI components */
#menuwrapper ul, #menuwrapper ul li{
  margin:0;
  padding:0;
  list-style:none;
}

/* We apply background color and border bottom white and width to 150px */
#menuwrapper ul li{
  background-color:#7f95db;
  border-bottom:solid 1px white;
  width:150px;
  cursor:pointer;
}

/* We apply the background hover color when user hover the mouse over of the li component */
#menuwrapper ul li:hover{
  background-color:#6679e9;
  position:relative;
}

/* We apply the link style */
#menuwrapper ul li a{
  padding:5px 15px;
  color:#ffffff;
  display:inline-block;
  text-decoration:none;
}

/**** SECOND LEVEL MENU ****/
/* We make the position to absolute for flyout menu and hidden the ul until the user hover the parent li item */
#menuwrapper ul li ul{
  position:absolute;
  display:none;
}

/* When user has hovered the li item, we show the ul list by applying display:block, note: 150px is the individual menu width.  */
#menuwrapper ul li:hover ul{
  left:150px;
  top:0px;
  display:block;
}

/* we apply different background color to 2nd level menu items*/
#menuwrapper ul li ul li{
  background-color:#cae25a;
}

/* We change the background color for the level 2 submenu when hovering the menu */
#menuwrapper ul li:hover ul li:hover{
  background-color:#b1b536;
}

/* We style the color of level 2 links */
#menuwrapper ul li ul li a{
  color:#454444;
  display:inline-block;
  width:120px;
}

/**** THIRD LEVEL MENU ****/
/* We need to hide the 3rd menu, when hovering the first level menu */
#menuwrapper ul li:hover ul li ul{
  position:absolute;
  display:none;
}

/* We show the third level menu only when they hover the second level menu parent */
#menuwrapper ul li:hover ul li:hover ul{
  display:block;
  left:150px;
  top:0;
}

/* We change the background color for the level 3 submenu*/
#menuwrapper ul li:hover ul li:hover ul li{
  background:#86d3fa;
}

/* We change the background color for the level 3 submenu when hovering the menu */

#menuwrapper ul li:hover ul li:hover ul li:hover{
  background:#358ebc;
}

/* We change the level 3 link color */
#menuwrapper ul li:hover ul li:hover ul li a{
  color:#ffffff;
}

/* Clear float */
.clear{
  clear:both;
}
<div id="menuwrapper">
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Products</a>
            <ul>
                <li><a href="#">Product 1</a>
                    <ul>
                        <li><a href="#">Sub Product 1</a></li>
                        <li><a href="#">Sub Product 2</a></li>
                        <li><a href="#">Sub Product 3</a></li>
                    </ul>
                </li>
                <li><a href="#">Product 2</a></li>
                <li><a href="#">Product 3</a></li>
            </ul>
        </li>
        <li><a href="#">About Us</a>
            <ul>
                <li><a href="#">Faqs</a></li>
                <li><a href="#">Contact Us</a></li>
                <li><a href="#">Where are we?</a></li>
            </ul>
        </li>
        <li><a href="#">Help</a>
    </ul>
</div>

最佳答案

这看起来如何:

https://jsfiddle.net/tobyl/um02Loxc/1/

相关代码:

#menuwrapper ul {
  position: relative;
}

和:

/* We apply the background hover color when user hover the mouse over of the li component */
#menuwrapper ul li:hover{
    background-color:#6679e9;
}

关于html - CSS:如何删除子菜单的顶部间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38516410/

相关文章:

html - 将鼠标悬停在图像上时不显示文本

html - 如何水平居中 iframe 的内容(谷歌地图)?常规方法无效

javascript - 可扩展列表折叠隐藏 div 元素 (jQuery)

php - 这个 Twig 代码性能明智,足够快吗?

html - 使跨度元素可编辑?

javascript - 如何使用文本增加Popup的宽度

html - 如何使点击功能不仅在按住鼠标时持续?

html - 聊天网络应用 : flex-boxes inside div position:fixed

CSS 不适用于空格

html - 导航下的图像使链接不可点击。