简体   繁体   English

如何在joomla模板中覆盖CSS

[英]How to override CSS in joomla template

In my Joomla template I would like to override some styles, so I've created my own css and added styles, also media queries. 在我的Joomla模板中,我想覆盖一些样式,因此我创建了自己的CSS并添加了样式,还添加了媒体查询。 Most of all works only if I use the !important keyword and something does not work at all. 只有当我使用!important关键字并且某些功能根本无效时,大多数功能才起作用。 For example: 例如:

<div id="content_right" class="span3">

In this div I have class and id. 在这个div中,我有class和id。 I cannot modify the class, because it affects other divs into the template, so I would like to add some style by id. 我无法修改该类,因为它会影响模板中的其他div,因此我想通过id添加一些样式。 I've added it but it does not work. 我添加了它,但是它不起作用。 I've inspected with Firebug and css is correctly loaded. 我检查了Firebug,并正确加载了CSS。 what could be the problem? 可能是什么问题呢?

The problem you are facing is called CSS specificity . 您面临的问题称为CSS特异性

Yes, the !important rule will override all other styles, but shouldn't be used for this reason. 是的, !important规则将覆盖所有其他样式,但由于这个原因不应使用。

First, you'll need to call your stylesheet after the Joomla stylesheet. 首先,您需要在Joomla样式表之后调用样式表。

Then, you'll need to use the same specificity (or better) in your stylesheet, as is used in the Joomla stylesheet. 然后,您需要在样式表中使用与Joomla样式表相同的特异性(或更优)。

To clarify, consider this sample code: 为了澄清,请考虑以下示例代码:

<div id="content">
   <div id="subcontent"> ... </div>
</div>

Combined with this CSS: 与此CSS结合:

#content #subcontent {
    background: yellow;
}
#subcontent {
    background: red;
}

 #content #subcontent { background: yellow; } #subcontent { background: red; } 
 <div id="content"> <div id="subcontent">...</div> </div> 

The first selector is clearly more specific than the second one. 第一个选择器显然比第二个选择器更具体。 Therefor, will the background be yellow, and not red. 因此,背景将是黄色而不是红色。

You are facing the same problem. 您正面临着同样的问题。 The first selector is the Joomla stylesheet. 第一个选择器是Joomla样式表。 So you need to specify the second selector the same (or better) in order to override it: 因此,您需要指定第二个选择器相同(或更优)以覆盖它:

#content #subcontent {
  background: yellow;
}
#content #subcontent {
  background: red;
}

 #content #subcontent { background: yellow; } #content #subcontent { background: red; } 
 <div id="content"> <div id="subcontent">...</div> </div> 

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

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