简体   繁体   English

为什么我的HTML不使用CSS中定义的最后一个样式?

[英]Why does my HTML not use the last style defined in the CSS?

I have the following CSS that's written in this order: 我有按此顺序编写的以下CSS:

h2:last-child, p:last-child, ul:last-child {
margin-bottom: 0;
}

.content-message {
   margin: 20px -1.667em -1.667em -1.667em;
   margin-bottom: -1.667em;
}

and my HTML: 和我的HTML:

<ul class="content-message">
    <li>xx</li>
</ul>

When I check I can see that the ul is getting its style from the the first CSS and it has a margin-bottom of 0. Can someone explain why this is. 当我检查时,我可以看到ul从第一个CSS中得到其样式,并且其底距为0。有人可以解释为什么会这样。 Also is there a way that I could fix this. 还有一种方法可以解决此问题。

It is done in order of specifity. 它是按指定顺序进行的。 In this case, ul:last-child is more specific because it has two conditions: 在这种情况下,ul:last-child更具体,因为它具有两个条件:

  • It has to be a ul 一定是ul
  • It has to be the last child 必须是最后一个孩子

Whereas your second style only has one condition: 而您的第二种样式只有一个条件:

  • It has to have the class content-message 它必须具有类内容消息

In order to make the second style as specific as the first you need to add another condition: 为了使第二种样式与第一种样式一样具体,您需要添加其他条件:

ul.content-message {
   margin: 20px -1.667em -1.667em -1.667em;
   margin-bottom: -1.667em;
}

Because the two rules are equally as specific, the second rule takes precedence because it is written last. 由于这两个规则的具体含义相同,因此第二个规则具有优先权,因为它是最后写入的。 Note that this is a simplification of css specificity - the ordering works something like this: 请注意,这是CSS特异性的简化-排序工作如下:

  1. Inline styles with !important 重要的内联样式
  2. Stylesheet styles with !important 带有!important的样式表样式
  3. Inline styles without !important 没有!important的内联样式
  4. Selectors containing an id (#myItem) 包含ID(#myItem)的选择器
  5. Classes, tag names, pseudo classes etc. 类,标记名,伪类等

Any selector from higher up the list will take precedence, while two selectors from the same level will be determined by how many of that type there are (so two classes are less specific than an id, but as specific as a style and a tag). 列表中较高位置的任何选择器都将具有优先权,而同一级别的两个选择器将由该类型中的选择器决定(因此,两个类的特定性不如id,但特定于样式和标签) 。 Two styles will the same weighting will defer to the last defined style. 两种样式将具有相同的权重,将遵循最后定义的样式。

I may be wrong on all items in point 5 having equal weighting, but for a better understanding check out http://www.adobe.com/devnet/dreamweaver/articles/css_specificity.html or similar articles found on Google. 对于第5点中权重相等的所有项目,我可能是错的,但是为了更好地理解,请查看http://www.adobe.com/devnet/dreamweaver/articles/css_specificity.html或Google上的类似文章。

See this article that describes css specifity issues- http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html 请参阅描述CSS特殊性问题的文章-http: //www.stuffandnonsense.co.uk/archives/css_specificity_wars.html

Preference order- element selector < class selector < id selector 优先顺序- 元素选择器<类选择器<id选择器

So specifying it as an Id attribute should work 因此,将其指定为Id属性应该可以

   #content-message {
   margin: 20px -1.667em -1.667em -1.667em;
   margin-bottom: -1.667em;

and <ul id="content-message"> <ul id="content-message">

Update- In your case irrespective of the above cases, you inline style- style="margin-bottom: -1.667em;" 更新-无论您遇到哪种情况, style="margin-bottom: -1.667em;"联style- style="margin-bottom: -1.667em;" will override any other css. 将覆盖其他任何CSS。 Surprised to see that doesn't happen with you. 惊讶地发现您没有发生这种情况。

请在您要应用的样式上打上“!重要”标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM