html - 重要的!烦恼

标签 html css

我还是不明白,“test1”段落是哪种颜色,为什么?

<style>
  p {
    color: red; 
    font-style: italic;
  }
</style>

<div style="color: green;!important">
  <p style="color: blue;">test1</p>
  <p>test2</p>
</div>

最佳答案

重要性不级联。

即使父元素(<div>)有一个!important属性(请注意,由于语法错误,它实际上没有),它仍将被应用于子元素的任何属性覆盖。

!important只能覆盖应用于相同 元素的其他规则。

因此,第一个<p>将是蓝色的,因为没有什么可以覆盖它的内联样式。

关于html - 重要的!烦恼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002402/

相关文章:

html - fabric.js 如何使用 png 图像屏蔽或裁剪 Canvas

css - 顶部固定且相对左侧的菜单

html - 溢出(滚动)以使用负 `left` 和 `top` 定位元素

css - CSS 选择器 `:enabled` 与 `not(:disabled)` 有区别吗

html - float :left; vs display:inline; vs display:inline-block; vs display:table-cell;

javascript - 使用 iframe 获取 Jira Scrum Board

html - 单击后文本框不可编辑

html - Reactjs:如何将 HTML 放入列表项的 primaryText 中?

javascript - 是真的吗 "JavaScript files are not reliably cached by browsers, CSS files are"

html - 悬停效果但禁用文本效果