问题描述
下面的示例应该在 CSS main-nav 中使用鼠标悬停,这样当鼠标指向菜单项时,项目就会在菜单项周围形成一个块。不知何故,这只适用于右侧的 ABOUT,而不适用于主导航中的其他菜单项。有什么遗漏吗?这可能是孩子父母的问题吗?
HTML index.html
<!DOCTYPE html>
<head>
<title>TITLE</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<header>
<div class="row">
<div class="logo">
<img src="logo1.png">
</div>
<ul class="main-nav">
<li class="active"><a href=""> HOME </a></li>
<li><a href=""> DOCUMENTS </a></li>
<li><a href=""> LOGIN </a></li>
<li><a href=""> ABOUT </a></li>
</ul>
</div>
<div class="hero">
<h1>Welcome</h1>
<div class="button">
<a href="" class="btn btn-one"> Watch Video </a>
<a href="" class="btn btn-two"> Explore More </a>
</div>
</div>
</header>
</body>
</html>
CSS style.css
{
margin: 0;
padding: 0;
}
header
{
background-image: linear-gradient(rgba(0,0.05),rgba(0,0.05)),url(pic1.jpg);
height: 100vh;
background-size: cover;
background-position: center;
}
.main-nav
{
float: right;
list-style: none;
margin-top: 30px;
}
.main-nav li
{
display: inline-block;
}
.main-nav li a
{
color: white;
text-decoration: none;
padding: 5px 20px;
font-family: "Roboto",sans-serif;
font-size: 15px;
}
.main-nav li.active a
{
border: 1px solid white;
}
.main-nav li a:hover
{
border: 1px solid white;
}
.logo img
{
width: 115px;
height: auto;
float: left;
margin-top: 25px;
}
body
{
font-family: monospace;
}
.row
{
max-width: 1200px;
margin: auto;
}
.hero
{
position: absolute;
width: 1200px;
margin-left: 0px;
margin-top: 0px;
}
h1
{
color: white;
text-transform: uppercase;
font-size: 55px;
text-align: center;
margin-top: 275px;
}
.button
{
margin-top: 30px;
margin-left: 440px;
}
.btn
{
border: 1px solid white;
padding: 10px 30px;
color: white;
text-decoration: none;
margin-right: 5px;
font-size: 13px;
text-transform: uppercase;
}
.btn-one
{
font-family: "Roboto",sans-serif;
}
.btn-one:hover
{
background-color: seagreen;
transition: all 0.2s ease-in;
}
.btn-two
{
font-family: "Roboto",sans-serif;
}
.btn-two:hover
{
background-color: darkorange;
transition: all 0.2s ease-in;
}
解决方法
问题的原因是您的 .hero
元素具有绝对位置并且位于左上角(由于应用了默认设置),因此它与大多数其他代码重叠。你应该改变它。
你能看看下面的代码吗?希望它对你有用。在您的代码中,我们添加了类“navbar”以及“row”,并提供了相对位置和 z-index 以实现所需的要求。
请参考此链接:https://jsfiddle.net/yudizsolutions/f96mrp5x/2/
{
margin: 0;
padding: 0;
}
header {
background-image: linear-gradient(rgba(0,0.05),rgba(0,0.05)),url(pic1.jpg);
height: 100vh;
background-size: cover;
background-position: center;
}
.navbar {
position: relative;
z-index: 1;
}
.main-nav {
float: right;
list-style: none;
margin-top: 30px;
}
.main-nav li {
display: inline-block;
}
.main-nav li a {
color: white;
text-decoration: none;
padding: 5px 20px;
font-family: "Roboto",sans-serif;
font-size: 15px;
}
.main-nav li.active a {
border: 1px solid white;
}
.main-nav li a:hover {
border: 1px solid white;
}
.logo img {
width: 115px;
height: auto;
float: left;
margin-top: 25px;
}
body {
font-family: monospace;
}
.row {
max-width: 1200px;
margin: auto;
}
.hero {
position: absolute;
width: 1200px;
margin-left: 0px;
margin-top: 0px;
}
h1 {
color: white;
text-transform: uppercase;
font-size: 55px;
text-align: center;
margin-top: 275px;
}
.button {
margin-top: 30px;
margin-left: 440px;
}
.btn {
border: 1px solid white;
padding: 10px 30px;
color: white;
text-decoration: none;
margin-right: 5px;
font-size: 13px;
text-transform: uppercase;
}
.btn-one {
font-family: "Roboto",sans-serif;
}
.btn-one:hover {
background-color: seagreen;
transition: all 0.2s ease-in;
}
.btn-two {
font-family: "Roboto",sans-serif;
}
.btn-two:hover {
background-color: darkorange;
transition: all 0.2s ease-in;
}
<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<header>
<div class="row navbar">
<div class="logo">
<img src="logo1.png">
</div>
<ul class="main-nav">
<li class="active"><a href=""> HOME </a></li>
<li><a href=""> DOCUMENTS </a></li>
<li><a href=""> LOGIN </a></li>
<li><a href=""> ABOUT </a></li>
</ul>
</div>
<div class="hero">
<h1>Welcome</h1>
<div class="button">
<a href="" class="btn btn-one"> Watch Video </a>
<a href="" class="btn btn-two"> Explore More </a>
</div>
</div>
</header>
</body>
</html>