简体   繁体   English

外部 CSS 不起作用,而内联样式起作用

[英]External CSS doesn't work, while inline styling does work

If I write this in my external css file it does make 'a' text green.如果我在我的外部 css 文件中编写它,它确实会使“a”文本变为绿色。

.ink-navigation ul.menu.black li ul.submenu li a {
   color: green;
}

but this works但这有效

<nav class="ink-navigation"> 
   <ul class="menu black">
     <li>
       <ul class="submenu">
         <li>
           <a href="#" title="" style="color:green">This is a text</a>
        </li>
       </ul>
     </li>
   </ul>
</nav>

Does anybody knows why the external css does not work?有谁知道为什么外部 css 不起作用?

CSS executes in order; CSS按顺序执行; therefore rules at the bottom of your CSS are given higher priority and override any prior rules.因此 CSS 底部的规则被赋予更高的优先级并覆盖任何先前的规则。 Also, inline CSS executes after the stylesheet and since it is seen last, it takes priority.此外,内联 CSS 在样式表之后执行,因为它是最后看到的,所以它具有优先权。

A simple fix would be to add important to the colour... eg:一个简单的解决方法是添加重要的颜色......例如:

  • color: green!important;

On a side note, I highly recommend that you reduce your code by removing unnecessary bloat which actually contributes to such problems as this.附带说明一下,我强烈建议您通过删除实际上导致此类问题的不必要的膨胀来减少代码。 If .ink-navigation is unique you don't need to use your current format.如果.ink-navigation是唯一的,则无需使用当前格式。 Use .ink-navigation ul li ul li a {} or even .ink-navigation .submenu a {} .使用.ink-navigation ul li ul li a {}甚至.ink-navigation .submenu a {} However, if you have existing rules with the larger format then that will override the shorter format, so its important to address all occurrences if you want to shorten your code.但是,如果您有较大格式的现有规则,那么它将覆盖较短的格式,因此如果您想缩短代码,解决所有出现的问题很重要。

In the head tag ,first Place bootstrap file on the top, then css file below.在 head 标签中,首先将 bootstrap 文件放在顶部,然后将 css 文件放在下面。 with this you don't want to use !important in every style code.有了这个,你不想在每个样式代码中使用 !important 。

Where have you attached your css external files?你在哪里附加了你的css外部文件?

You should have something like this in your header您的标题中应该有类似的内容

     <link rel="stylesheet" type="text/css" href="CSS/cssFile.css">

Most likely your external CSS is not linked to correctly in your HTML.很可能您的外部 CSS 在您的 HTML 中没有正确链接。

Example of how to link a CSS file that's in the same folder as your HTML:如何链接与 HTML 位于同一文件夹中的 CSS 文件的示例:

<link href="./main.css" rel="stylesheet">

Also, just a tip - it's usually clearer code to understand and debug if you just put an id or class (ie id="green-title" ) as an attribute for your tag.此外,只是一个提示 - 如果您只是将 id 或类(即id="green-title" )作为标签的属性,通常可以更清晰地理解和调试代码。

#green-title {
color: green;
}

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

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