html - 悬停后如何保持原始字体颜色?

标签 html css css-selectors

大家好,我试图在此悬停中保持白色,但我做不到?当我将光标移动到元素内的下一个元素时,悬停字体会改变它的颜色,我一直在做很多不同的方法来尝试保持我想要的颜色,但似乎没有任何效果,请帮忙。 我展示了这张照片中发生的事情,我会很感激一些帮助。 https://ibb.co/nDLQ7w https://ibb.co/j5NBfG 抱歉我的英语不好,我来自日本。

@import url('https://fonts.googleapis.com/css?family=Spectral+SC');

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
html {
  font-size: 22px;
  line-height: 32px;
  background-color: #212121;
  color: #555;
  word-wrap: break-word;
  font-family: 'Spectral sc', sans-serif;
}
h1 {
  font-size: 60px;
  line-height: 70px;
  color: #fff;
  margin-top: 100px;
  text-align: center;
}
h3 {
  font-size: 30px;
  line-height: 34px;
  color: #fff;
  text-align: center;
}
p {text-align: center;}
nav {
  padding: 0;
  margin: 0;
  background-color: #fff;
}
nav:after {
  content: " ";
  display: table;
  clear: both;
}
nav ul {
  float: right;
  padding: 0;
  margin: 0;
  list-style: none;
}
nav ul li {
  float: left;
  display: inline-block;
  margin: 0;
  background-color: #fff;
}

nav a {
  display: block;
  padding: 10px 20px;
  color: #555;
  font-size: 20px;
  line-height: 60px;
  text-decoration: none;
}
.icon {
  color: #fff;
  font-size: 25px;
}
.toggle, [id^=drop] {display: none;}
nav ul li ul li:hover {background-color: #000;}
nav a:hover {
  color: #fff;
  background-color: #000;}
nav ul ul {
  display: none;
  position: absolute;
  top: 80px;
}
nav ul li:hover > ul {display: inherit;}
nav ul ul li {
  width: 170px;
  float: none;
  display: list-item;
  position: relative;
}
nav ul ul ul li {
  position: relative;
}

.logo {
  height: 70px;
  width: auto;
  margin: 5px 15px;
  float: left;
}
.container {
    display: inline-block;
    cursor: pointer;
}

.bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #fff;
    margin: 6px 0;
    transition: 0.4s;
    border-radius: 5px;
}
.toggle-nav {display: none;}
.change .bar1 {
    -webkit-transform: rotate(-45deg) translate(-9px, 6px);
    transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {opacity: 0;}

.change .bar3 {
    -webkit-transform: rotate(45deg) translate(-8px, -8px);
    transform: rotate(45deg) translate(-8px, -8px);
}
@media all and (max-width : 767px) {
  nav {margin: 0;}
  nav ul{float: left;clear:both;}
  .toggle + a, .menu {display: none;}
  .toggle {
    display: block;
    background-color: #254441;
    padding: 10px 20px;
    color: #fff;
    font-size: 20px;
    line-height: 60px;
    text-decoration: none;
    border: none;
  }
  .toggle-nav {
    display: block;
    margin-top: 20px;
    margin-right: 25px;
    color: #fff;
  }
  .toggle:hover {background-color: #000;}
  [id^=drop]:checked + ul {display: block;}
  .logo {
   left: 50%;
   position: relative;
   transform: translateX(-50%);
  }
  nav ul li {
    display: block;
    width: 100%;
  }
  nav ul {
    width: 100%;
  }
  nav ul ul .toggle,
  nav ul ul a {padding: 0 40px;}
  nav ul ul ul a {padding: 0 80px;}

  nav a:hover,
  nav ul ul ul a {background-color: #000;}
  nav ul li ul li .toggle,
  nav ul ul a {background-color: #212121;}

  nav ul ul {
    float: none;
    position: static;
    color: #fff;
  }
  nav ul ul li:hover > ul,
  nav ul li:hover > ul {display: none;}

  nav ul ul li {
    display: block;
    width: 100%;
  }
  nav ul ul ul li {position: static;}
}
@media all and (max-width : 330px) {
  nav ul li {
    display: block;
    width: 94%;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,400" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="resources/css/grid.css">
    <link rel="stylesheet" type="text/css" href="resources/css/ionicons.min.css">
    <link rel="stylesheet" type="text/css" href="resources/css/normalize.css">
    <link rel="stylesheet" type="text/css" href="resources/css/style.css">
    <link rel="stylesheet" type="text/css" href="resources/css/queries.css">
	<title>Web title</title>
</head>
<body>
	<nav>
		<img src="https://upload.wikimedia.org/wikipedia/commons/a/ab/Logo_TV_2015.png" class="logo">
		<label for="drop" class="toggle-nav" style="float: right;"><div class="container" onclick="myFunction(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div></label>
		<input type="checkbox" id="drop">
		<ul class="menu">
			<li><a href="#">Home</a></li>
			<li>
				<label for="drop-1" class="toggle"">Services +</label>
				<a href="#">Services +</a>
				<input type="checkbox" id="drop-1">
				<ul>
					<li><a href="#">Service 1</a></li>
					<li><a href="#">Service 2</a></li>
					<li><a href="#">Service 3</a></li>
				</ul>
			</li>
			<li>
				<label for="drop-2" class="toggle"">Portfolio +</label>
				<a href="#">Portfolio +</a>
				<input type="checkbox" id="drop-2">
				<ul>
					<li><a href="#">Service 1</a></li>
					<li><a href="#">Service 2</a></li>
					<li><a href="#">Service 3</a></li>
					<li>
						<label for="drop-3" class="toggle"">Port +</label>
						<a href="#">Port+</a>
						<input type="checkbox" id="drop-3">
						<ul>
							<li><a href="#">Service 1</a></li>
							<li><a href="#">Service 2</a></li>
							<li><a href="#">Service 3</a></li>	
						</ul>				
					</li>
				</ul>				
			</li>
			<li><a href="#">Service 1</a></li>
			
		</ul>
	</nav>
</body>

<script>
function myFunction(x) {
    x.classList.toggle("change");
}
</script>
</html>

最佳答案

考虑将 :hover 伪状态 添加到 containing 列表项 (li) 而不是 anchor 元素 (a) 本身,并使用 子组合器 (>) Ref 选择器以直接嵌套的 anchor 元素为目标(这样并不是所有嵌套在给定列表项中的 anchor 元素都会受到影响),例如:

nav li:hover > a {
  color: #fff;
  background-color: #000;
}

这样,当光标离开具有嵌套下拉菜单的列表项的 anchor 元素时,声明的规则仍然适用,因为光标仍然悬停在包含的列表项上。

代码片段演示:

展开“整页”

@import url('https://fonts.googleapis.com/css?family=Spectral+SC');

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
html {
  font-size: 22px;
  line-height: 32px;
  background-color: #212121;
  color: #555;
  word-wrap: break-word;
  font-family: 'Spectral sc', sans-serif;
}
h1 {
  font-size: 60px;
  line-height: 70px;
  color: #fff;
  margin-top: 100px;
  text-align: center;
}
h3 {
  font-size: 30px;
  line-height: 34px;
  color: #fff;
  text-align: center;
}
p {text-align: center;}
nav {
  padding: 0;
  margin: 0;
  background-color: #fff;
}
nav:after {
  content: " ";
  display: table;
  clear: both;
}
nav ul {
  float: right;
  padding: 0;
  margin: 0;
  list-style: none;
}
nav ul li {
  float: left;
  display: inline-block;
  margin: 0;
  background-color: #fff;
}

nav a {
  display: block;
  padding: 10px 20px;
  color: #555;
  font-size: 20px;
  line-height: 60px;
  text-decoration: none;
}
.icon {
  color: #fff;
  font-size: 25px;
}
.toggle, [id^=drop] {display: none;}
nav ul li ul li:hover {background-color: #000;}

/* Modified */
nav li:hover > a {
  color: #fff;
  background-color: #000;
}


nav ul ul {
  display: none;
  position: absolute;
  top: 80px;
}
nav ul li:hover > ul {display: inherit;}
nav ul ul li {
  width: 170px;
  float: none;
  display: list-item;
  position: relative;
}
nav ul ul ul li {
  position: relative;
}

.logo {
  height: 70px;
  width: auto;
  margin: 5px 15px;
  float: left;
}
.container {
    display: inline-block;
    cursor: pointer;
}

.bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #fff;
    margin: 6px 0;
    transition: 0.4s;
    border-radius: 5px;
}
.toggle-nav {display: none;}
.change .bar1 {
    -webkit-transform: rotate(-45deg) translate(-9px, 6px);
    transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {opacity: 0;}

.change .bar3 {
    -webkit-transform: rotate(45deg) translate(-8px, -8px);
    transform: rotate(45deg) translate(-8px, -8px);
}
@media all and (max-width : 767px) {
  nav {margin: 0;}
  nav ul{float: left;clear:both;}
  .toggle + a, .menu {display: none;}
  .toggle {
    display: block;
    background-color: #254441;
    padding: 10px 20px;
    color: #fff;
    font-size: 20px;
    line-height: 60px;
    text-decoration: none;
    border: none;
  }
  .toggle-nav {
    display: block;
    margin-top: 20px;
    margin-right: 25px;
    color: #fff;
  }
  .toggle:hover {background-color: #000;}
  [id^=drop]:checked + ul {display: block;}
  .logo {
   left: 50%;
   position: relative;
   transform: translateX(-50%);
  }
  nav ul li {
    display: block;
    width: 100%;
  }
  nav ul {
    width: 100%;
  }
  nav ul ul .toggle,
  nav ul ul a {padding: 0 40px;}
  nav ul ul ul a {padding: 0 80px;}

  nav a:hover,
  nav ul ul ul a {background-color: #000;}
  nav ul li ul li .toggle,
  nav ul ul a {background-color: #212121;}

  nav ul ul {
    float: none;
    position: static;
    color: #fff;
  }
  nav ul ul li:hover > ul,
  nav ul li:hover > ul {display: none;}

  nav ul ul li {
    display: block;
    width: 100%;
  }
  nav ul ul ul li {position: static;}
}
@media all and (max-width : 330px) {
  nav ul li {
    display: block;
    width: 94%;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Lato:100,300,400" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="resources/css/grid.css">
    <link rel="stylesheet" type="text/css" href="resources/css/ionicons.min.css">
    <link rel="stylesheet" type="text/css" href="resources/css/normalize.css">
    <link rel="stylesheet" type="text/css" href="resources/css/style.css">
    <link rel="stylesheet" type="text/css" href="resources/css/queries.css">
	<title>Web title</title>
</head>
<body>
	<nav>
		<img src="https://upload.wikimedia.org/wikipedia/commons/a/ab/Logo_TV_2015.png" class="logo">
		<label for="drop" class="toggle-nav" style="float: right;"><div class="container" onclick="myFunction(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div></label>
		<input type="checkbox" id="drop">
		<ul class="menu">
			<li><a href="#">Home</a></li>
			<li>
				<label for="drop-1" class="toggle">Services +</label>
				<a href="#">Services +</a>
				<input type="checkbox" id="drop-1">
				<ul>
					<li><a href="#">Service 1</a></li>
					<li><a href="#">Service 2</a></li>
					<li><a href="#">Service 3</a></li>
				</ul>
			</li>
			<li>
				<label for="drop-2" class="toggle">Portfolio +</label>
				<a href="#">Portfolio +</a>
				<input type="checkbox" id="drop-2">
				<ul>
					<li><a href="#">Service 1</a></li>
					<li><a href="#">Service 2</a></li>
					<li><a href="#">Service 3</a></li>
					<li>
						<label for="drop-3" class="toggle">Port +</label>
						<a href="#">Port+</a>
						<input type="checkbox" id="drop-3">
						<ul>
							<li><a href="#">Service 1</a></li>
							<li><a href="#">Service 2</a></li>
							<li><a href="#">Service 3</a></li>	
						</ul>				
					</li>
				</ul>				
			</li>
			<li><a href="#">Service 1</a></li>
			
		</ul>
	</nav>
</body>

<script>
function myFunction(x) {
    x.classList.toggle("change");
}
</script>
</html>

注意:还要检查 label 元素上的标记,其中类 .toggle 以双撇号结束。

关于html - 悬停后如何保持原始字体颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47765324/

相关文章:

html - 对齐网格底部的按钮

css - 更改单个顶部栏元素及其下拉菜单的背景

jquery - 如何在动态 CSS 网站中将 jQuery LightGallery 的缩略图居中

javascript - 页眉到 margin-top 的高度

html - 为每边设置不同的轮廓偏移

javascript - 防止提交非数字字段的表单

css - R Shiny : resizing the entire app by 50% as if you're doing it on the browser

css - 当鼠标悬停在子元素上时,我将如何选择父元素?

html - 如何使用 :not() 的 css first-child

html - 在 CSS 中旋转地球