但是,我无法弄清楚悬停状态下的项目是否比其父容器<ul>
标签高一点,如上图所示。
HTML示例:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<title>Navigation Menu</title>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Company</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
</html>
示例样式表:
nav ul {
padding:0;
margin:0;
list-style: none;
background-color:#404040;
overflow: hidden;
border-bottom: 5px solid #3696DD;
}
nav ul li {
float: left;
}
nav ul li a {
text-decoration: none;
display: block;
padding:15px 40px;
color:#FFFFFF;
font-size: 15px;
font-family: Calibri;
font-weight: bold;
}
nav ul li:hover a {
background-color: #3696DD;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
任何想法,请多多帮助。
答案 0 :(得分:1)
以下是使用伪元素创建额外高度的解决方案:
*为此,您需要移除overflow: hidden
上的ul
,并将其替换为固定高度。
nav ul {
padding: 0;
margin: 0;
list-style: none;
background-color: #404040;
height: 49px; /* plus 4px for the new border */
border-bottom: 5px solid #3696DD;
}
nav ul li {
float: left;
}
nav ul li a {
text-decoration: none;
display: block;
padding: 15px 40px 19px; /* plus 4px to bottom to keep text centred */
color: #FFFFFF;
font-size: 15px;
font-family: Calibri;
font-weight: bold;
position: relative;
}
nav ul li a::after {
content: '';
position: absolute;
top: -4px;
left: 0;
width: 100%;
height: 4px;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
}
nav ul li:hover a {
background-color: #3696DD;
}
nav ul li:hover a::after {
background-color: #3696DD;
}
&#13;
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Company</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
&#13;