当我将鼠标悬停在链接上时,只有当我直接悬停在链接上时,实际的链接才会改变颜色。不管怎样,当我把鼠标悬停在实际的li上时,我可以让文本改变颜色吗?谢谢。
我还在学习,所以如果你看到我的代码错误,请让我知道。干杯!
body {
background: url('bg.jpg');
margin: 0px;
padding: 0px;
}
nav {
background-color: #ffffff;
width: 80%;
height: 60px;
margin: 200px auto auto auto;
}
nav ul {
list-style-type: none;
margin: auto;
}
nav li {
display: inline-block;
line-height: 60px;
float: right;
}
nav a {
text-decoration: none;
color: #b0b0b0;
font-family: 'verdana', sans-serif;
padding: 0 20px;
font-size: 0.85em;
}
nav a:hover {
color: #ffffff;
transition: color 0.8s ease;
}
nav li:hover {
background-color: #5db0c6;
}
#head-image {
background: url('land.png');
width: 80%;
height: 400px;
margin: auto;
}
#second-header {
width: 80%;
height: 60px;
margin: auto;
background-color: #ffffff;
}
#third-header {
width: 80%;
height: 700px;
margin: auto;
background-color: #ededed;
}
<!DOCTYPE html>
<html>
<head>
<title>site</title>
<meta name="description" content="" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" />
<link href='https://fonts.googleapis.com/css?family=Libre+Franklin' rel='stylesheet' type='text/css'>
<script src="js/jquery.min.js"></script>
<!--[if lt IE 8]>
<![endif]-->
</head>
<body>
<nav>
<ul>
<li><a href="">Gamerscoin</a></li>
<li><a href="">Testimonials</a></li>
<li><a href="">Contact</a></li>
<li><a href="">RS3</a></li>
<li><a href="">RS7</a></li>
<li><a href="">CSGO</a></li>
<li><a href="">POE</a></li>
<li><a href="">LoL</a></li>
<li><a href="">Path of Exile</a></li>
<li><a href="">Habbo</a></li>
</ul>
</nav>
<div id="head-image"></div>
<div id="second-header"></div>
<div id="third-header"></div>
</body>
</html>
使用CSS选择器nav li:hover a
代替。这实际上是说,当导航栏中的li光标悬停在上方时,改变li中的a元素。
属性现在看起来像:
nav li:hover a{
color: #ffffff;
transition: color 0.8s ease;
}
body {
background: url('bg.jpg');
margin: 0px;
padding: 0px;
}
nav {
background-color: #ffffff;
width: 80%;
height: 60px;
margin: 200px auto auto auto;
}
nav ul {
list-style-type: none;
margin: auto;
}
nav li {
display: inline-block;
line-height: 60px;
float: right;
}
nav a {
text-decoration: none;
color: #b0b0b0;
font-family: 'verdana', sans-serif;
padding: 0 20px;
font-size: 0.85em;
}
nav li:hover a{
color: #ffffff;
transition: color 0.8s ease;
}
nav li:hover {
background-color: #5db0c6;
}
#head-image {
background: url('land.png');
width: 80%;
height: 400px;
margin: auto;
}
#second-header {
width: 80%;
height: 60px;
margin: auto;
background-color: #ffffff;
}
#third-header {
width: 80%;
height: 700px;
margin: auto;
background-color: #ededed;
}
<!DOCTYPE html>
<html>
<head>
<title>site</title>
<meta name="description" content="" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" />
<link href='https://fonts.googleapis.com/css?family=Libre+Franklin' rel='stylesheet' type='text/css'>
<script src="js/jquery.min.js"></script>
<!--[if lt IE 8]>
<![endif]-->
</head>
<body>
<nav>
<ul>
<li><a href="">Gamerscoin</a></li>
<li><a href="">Testimonials</a></li>
<li><a href="">Contact</a></li>
<li><a href="">RS3</a></li>
<li><a href="">RS7</a></li>
<li><a href="">CSGO</a></li>
<li><a href="">POE</a></li>
<li><a href="">LoL</a></li>
<li><a href="">Path of Exile</a></li>
<li><a href="">Habbo</a></li>
</ul>
</nav>
<div id="head-image"></div>
<div id="second-header"></div>
<div id="third-header"></div>
</body>
</html>
nav li:hover a {
color: #b0b0b0;
}
简单地改变你的链接的color
时,悬停他们的父母是,它会给人的印象,整个li
是可点击的,它不会。要解决这个问题,将链接的display
属性设置为block
,强制链接占用其父链接的全部可用空间。
nav {
background-color: #ffffff;
width: 80%;
height: 60px;
margin: 200px auto auto auto;
}
nav ul {
list-style-type: none;
margin: auto;
}
nav li {
display: inline-block;
line-height: 60px;
float: right;
}
nav a {
display: block;
text-decoration: none;
color: #b0b0b0;
font-family: 'verdana', sans-serif;
padding: 0 20px;
font-size: 0.85em;
}
nav a:hover {
background-color: #5db0c6;
color: #ffffff;
transition: color 0.8s ease;
}
<nav>
<ul>
<li><a href="">Gamerscoin</a></li>
<li><a href="">Testimonials</a></li>
<li><a href="">Contact</a></li>
<li><a href="">RS3</a></li>
<li><a href="">RS7</a></li>
<li><a href="">CSGO</a></li>
<li><a href="">POE</a></li>
<li><a href="">LoL</a></li>
<li><a href="">Path of Exile</a></li>
<li><a href="">Habbo</a></li>
</ul>
</nav>
您仍然可以在hover
命令后使用标签名称,如:
nav li:hover a{
color: #ffffff;
transition: color 0.8s ease;
}