html – 使用新的CSS文件来覆盖当前网站的CSS
我的网站目前有3个CSS文件,被自动包含在网站的一部分,我无法访问网站的index.html,但我可以访问我的网站的CSS文件。
我试图使用自己的风格来覆盖我的网站CSS文件,并创建一个新的CSS文件,其中包含我想在我的网站上覆盖的所有样式。 我已经尝试使用@import url(css4.css),我把它放在我最后一个CSS文件的顶部,但不会覆盖最后一个CSS文件的样式。 我该如何实现? <link rel="stylesheet" type="text/css" href="currentCSS1.css"> <link rel="stylesheet" type="text/css" href="currentCSS2.css"> <link rel="stylesheet" type="text/css" href="currentCSS3.css"> <!-- How to add this below just by using CSS? --> <link rel="stylesheet" type="text/css" href="newCSS4.css"> 解决方法
除了使用!重要的是,大多数答案建议您使用,这是一个
CSS specificity的问题
可以由4列优先级表示:
这是一个具有CSS特异性的完整示例的片段 /*demo purposes*/ body {margin: 0;padding: 0} div,article {min-height: 200px;height: 100%;width: 100%} /*CSS Specificity */ /* SPECIFICITY 0/1/0/0 */ #id { background-color: green/*going to be green - overridden by style yellow */ } /* SPECIFICITY 0/0/1/0 */ .class { background-color: yellow /*going to be yellow - overridden by style blue */ } /* SPECIFICITY 0/0/0/1 */ section { background-color: blue /*going to be blue - overridden by style red */ } /* ------------ just to remove inline demo ----------- */ /*now remove background for inline elements we should use !important and a parent in order to make it more specific /* SPECIFICITY 1/0/0/1 */ section > div { background-color: purple !IMPORTANT /*going to be purple - final result */ } <article> <div id="id"> <div class="class"> <section> <div style="background-color:red"> <!--SPECIFICITY 1/0/0/0 - overridden by style purple --> </div> </section> </div> </div> </article> 现在这里是完整的代码片段 ID:绿色 /*demo purposes*/ body {margin: 0;padding: 0} div,article {min-height: 200px;height: 100%;width: 100%} /*CSS Specificity */ /* SPECIFICITY 0/1/0/0 */ #id { background-color: green } <article> <div id="id"> <div class="class"> <section> <div> </div> </section> </div> </div> </article> 类:黄 /*demo purposes*/ body {margin: 0;padding: 0} div,article {min-height: 200px;height: 100%;width: 100%} /*CSS Specificity */ /* SPECIFICITY 0/0/1/0 */ .class { background-color: yellow } <article> <div id="id"> <div class="class"> <section> <div> </div> </section> </div> </div> </article> 元素:蓝色 /*demo purposes*/ body {margin: 0;padding: 0} div,article {min-height: 200px;height: 100%;width: 100%} /*CSS Specificity */ /* SPECIFICITY 0/0/0/1 */ section { background-color: blue } <article> <div id="id"> <div class="class"> <section> <div> </div> </section> </div> </div> </article> 在线样式:红色 /*demo purposes*/ body {margin: 0;padding: 0} div,article {min-height: 200px;height: 100%;width: 100%} <article> <div id="id"> <div class="class"> <section> <div style="background-color:red"> <!--SPECIFICITY 1/0/0/0 --> </div> </section> </div> </div> </article> OVERRIDDEN INLINE STYLE:PURPLE /*demo purposes*/ body {margin: 0;padding: 0} div,article {min-height: 200px;height: 100%;width: 100%} /*CSS Specificity */ /* SPECIFICITY 1/0/0/1 */ section > div { background-color: purple !IMPORTANT } <article> <div id="id"> <div class="class"> <section> <div style="background-color:red"> <!--SPECIFICITY 1/0/0/0 --> </div> </section> </div> </div> </article> 您可以计算您的元素here的特异性 注意: A必须阅读这个主题 > Inheritance and cascade (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |