简体   繁体   English

覆盖.css样式表文件(不是内联)

[英]Override .css stylesheet file (not inline)

Is it possible to somehow override the default (in this case liquid) CSS stylesheet, but without using inline styles? 有可能以某种方式覆盖默认(在这种情况下是液体)CSS样式表,但不使用内联样式? Also I want that style to apply only to that specific page, and not any others. 此外,我希望该样式仅适用于该特定页面,而不是任何其他页面。

Yes, I can use !important inside <style> tag on that specific page, but I would need to reset a lot of things. 是的,我可以在该特定页面上的<style>标签内使用!important ,但我需要重置很多东西。 It just too much work, and looks very messy. 它只是太多的工作,看起来非常凌乱。

So is there any way I could reset ALL elements to default property values, and use my own style just on that one page? 那么有什么方法可以将所有元素重置为默认属性值,并在该页面上使用我自己的样式?

Well you can't modify your .css files, but you can replace files dynamically using js . 那么你不能修改你的.css文件,但你可以使用js动态替换文件。

function replaceFile(newFile, fileLink) {
    var oldcss = document.getElementsByTagName("link").item(fileLink);
    var newcss = document.createElement("link");
    newcss.setAttribute("rel", "stylesheet");
    newcss.setAttribute("type", "text/css");
    newcss.setAttribute("href", newFile);
    document.getElementsByTagName("head").item(0).replaceChild(newcss, oldcss);
}

Another solution, would be to add the desired styles using jQuery or js . 另一种解决方案是使用jQueryjs添加所需的样式。

$('.demo').css({'background-color':'red','color':'white');

css stands for Cascading Style Sheets so once you've added a style you can only change or remove it by using another style. css代表层叠样式表,所以一旦你添加了一个样式,你只能通过使用另一种样式来改变或删除它。 to answer your question specifically no you can not. 具体回答你的问题你不行。 or as mentioned in the comments just remove the stylesheet from that specific page. 或者如评论中所述,只需从该特定页面中删除样式表。

You can select all elements inside a wrapper with * and override: 您可以使用*和覆盖选择包装器内的所有元素:

Example: 例:

 .mylink{ color: red; } .mylink:hover{ color: black; } .reset *{ color: orange; } .reset *:hover{ color: silver; } 
 This is the link without reset: <a href="#" class="mylink">Link</a> <div class="reset"> This is the link with reset: <a href="#" class="mylink">Link</a> </div> 

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

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