简体   繁体   English

如何使 HTML 文件的一部分遵循特定的 css 样式表

[英]How to make a part of an HTML file follow a specific css stylesheet

So I have looked for a while but i can't find the answer.所以我找了一段时间,但我找不到答案。 To explain more precisely, I have an HTML file where there are <style> tags in the head of the file.为了更准确地解释,我有一个 HTML 文件,其中文件头部有<style>标签。 I want for the header of the HTML file to folow a dedicated CSS file.我希望 HTML 文件的 header 遵循专用的 CSS 文件。 Right now the tags in the head override the CSS file.现在,头部中的标签会覆盖 CSS 文件。

CSS is applied through a specific set of programmatic rules. CSS 通过一组特定的编程规则应用。

  1. in-line CSS.在线 CSS。 This has highest priority and overrides both stylesheets and <style> tag rules.这具有最高优先级并覆盖样式表和<style>标记规则。
  2. <style> tag has second highest priority and overrides stylesheets. <style>标签具有第二高优先级并覆盖样式表。
  3. Stylesheet has the lowest priority and will only come into effect if the 2 prior cases aren't available.样式表的优先级最低,只有在前两种情况不可用时才会生效。

Outside of those 3 points, another set of rules also apply.除了这 3 点之外,还适用另一套规则。 Specificity, and top-down.特异性,自上而下。 The most specified rules take precedence, and afterwards it's whichever rule was specified last.最指定的规则优先,然后是最后指定的规则。

In action:在行动:

 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/> <style>.btn-danger { background-color: blue; border: 5px solid black; } </style> <button class="btn btn-danger" style="background-color: green;">CSS TESTER</button>

What you experience in the above snippet, is that we pull in a Bootstrap stylesheet, and style our button with the btn-danger class to give it a red background and subtle border with red color.您在上面的代码片段中体验到的是,我们引入了一个 Bootstrap 样式表,并使用btn-danger class 为我们的按钮设置样式,使其具有红色背景和带有红色的微妙边框。

We then add a <style> tag, make the button blue and change the border styling.然后我们添加一个<style>标签,将按钮设为蓝色并更改边框样式。

Lastly, we make an in-line style rule to make the button green, making the end-result a button with a green background color, and a big black border, overriding the Bootstrap class from the stylesheet.最后,我们制定了一个内联样式规则,使按钮变为绿色,使最终结果成为具有绿色背景颜色和黑色大边框的按钮,覆盖样式表中的 Bootstrap class。

Find a quick read on the problem you are experiencing here .此处快速阅读您遇到的问题。

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

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