效果:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 清空所有边距 */
*{
padding: 0%;
margin: 0%;
}
.box{
width: 300px;
/* 中间显示 */
margin: 0 auto;
}
ul{
/* 去除前边的黑点 */
list-style: none;
}
.item{
float: left;
width: 148px;
text-align: center;
border: 1px solid blue;
background-color: blue;
line-height: 40px;
}
.item:hover{
background: lightblue;
}
.item ul{
display: none;
background-color: white;
color: black;
}
.item:hover ul{
display: block;
}
/* 注意此处hover位置 */
.item li:hover{
color: blue;
}
</style>
</head>
<body>
<ul class="box">
<li class="item">视频教程
<ul>
<li>全部视频教程</li>
<li>html5视频教程</li>
<li>css视频教程</li>
<li>java视频教程</li>
</ul>
</li>
<li class="item">考试认证
<ul>
<li>pmp</li>
<li>红帽</li>
</li>
</ul>
</body>
</html>
利用子代选择器实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 清空所有边距 */
*{
padding: 0%;
margin: 0%;
}
.box{
width: 300px;
/* 中间显示 */
margin: 0 auto;
}
ul{
/* 去除前边的黑点 */
list-style: none;
}
.box>li{
float: left;
width: 148px;
text-align: center;
border: 1px solid blue;
background-color: blue;
line-height: 40px;
}
.box>li:hover{
background: lightblue;
}
.box>li>ul{
display: none;
background-color: white;
color: black;
}
.box>li:hover ul{
display: block;
}
/* 注意此处hover位置 */
.box>li li:hover{
color: blue;
}
</style>
</head>
<body>
<ul class="box">
<li>视频教程
<ul>
<li>全部视频教程</li>
<li>html5视频教程</li>
<li>css视频教程</li>
<li>java视频教程</li>
</ul>
</li>
<li>考试认证
<ul>
<li>pmp</li>
<li>红帽</li>
</li>
</ul>
</body>
</html>