本文介绍了优先级,内联还是类的CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站的标题中定义了一个样式表,样式为style.css,并带有选择器:

My website has a stylesheet defined in the header as style.css with a selector:

.myClass {background:#000;}

现在我的div看起来像:

Now my div looks like:

<div class="myClass" style="background:#fff;"> &nbsp; </div>

内联或类是哪个优先级?

Which one has priority, the inline or the class?

推荐答案

一般来说,我们可以说所有样式都将按照以下规则层叠"为新的虚拟"样式表,其中第三个数字具有最高优先级:

Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number three has the highest priority:

  1. 浏览器默认值
  2. 嵌入式和外部样式表.后期优先于早期.嵌入式和外部之间没有任何固有的差异.
  3. 内联样式(在HTML元素内)

来源(原始不正确的信息,由于此处和此处均已纠正): w3schools

Source ( of original incorrect information, since corrected both here and there): w3schools

W3schools解释了很多关于CSS的知识,并介绍了可以使用CSS进行的大多数操作的示例.如果您对某些问题有疑问,请始终提供很好的资源. (值得商,的,它们是原始错误答案的来源.)

W3schools explains a lot about CSS and also goes through and shows examples of most things you can do with CSS. Always a good resource if you have questions about something. ( debatable, they were the source of the original wrong answer.)

这篇关于优先级,内联还是类的CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:38