html - 为什么悬停时整个导航 div 是洋红色而不是蓝色?

标签 html css

<分区>

使用 id 或 class 来选择元素重要吗?整个导航 div 在悬停时应该是蓝色的,因为它是在洋红色之后应用的。

.navigation {
  color: salmon;
  height: 100px;
}

#navigation:hover {
  color: magenta;
}

.navigation:hover {
  color: blue;
}

.navigation li a {
  text-decoration: none;
  color: inherit;
}
  <div id="navigation" class="navigation">I am navigation text!
    <ul>
      <li><a href="home.html">Home</a></li>
      <li><a href="about.html">About</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </div>

最佳答案

请引用此资源:Calculating CSS Specificity Value

... Simply using the class name by itself had a lower specificity value and was trumped by the other selector which targeted the unordered list with the ID value. The important words in that sentence were class and ID. CSS applies vastly different specificity weights to classes and IDs. In fact, an ID has infinitely more specificity value! That is, no amount of classes alone can outweigh an ID.

总之,IDClass 更具体,因此具有相同 CSS 属性的元素(例如 color)在IDClass 将具有 ID 属性规范的值,而不是 Class 规范的值。

Inline 样式再次更加具体,并且将胜过 ID 样式。

enter image description here

来源:https://css-tricks.com/specifics-on-css-specificity/#article-header-id-0

关于html - 为什么悬停时整个导航 div 是洋红色而不是蓝色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635880/

上一篇:c# - 删除 EWS EmailMessage 正文中的 HTML 部分

下一篇:javascript - 我正在尝试制作一个可折叠菜单,一次只打开 1 个纯 javascript

相关文章:

javascript - Rails 中的嵌入式 Google map 没有响应

html - rvest - 在 1 个标签中抓取 2 个类

html - CSS 等高列,一列包含底部 Div

javascript - 根据背景更改固定图像的颜色

javascript - 滚动 JAVASCRIPT 跳过一张图片并转到下一张?

PHP + CSS + Lettering.js 创建曲线文字

html - 圆形按钮之间的水平线

jquery - 如果 <a> 有标题,addClass jQuery?

css - 嵌套没有按预期工作

jquery - 从 json 动态加载数据时,克隆表标题列宽和原始 tbody 列宽未对齐