Using two css files in the same html file

久未见 提交于 2019-12-01 07:11:53

问题


Is it possible to use 2 CSS classes that have the same name for the selectors, etc. in the same HTML file? If so, how do you differentiate between the two when styling elements?


回答1:


Yes this is possible, simply include two css files in the HEAD section of the document. Any styles set in the first will be overwritten in the second, so say you have this:
First file:

 #something{
  background-color: #F00;
  color: #FFF;
 }

And then in the second file:

 #something{
  background-color: #000;
 }

Then the background color for #something will be overwritten in the second file to black but the color will stay the same since the second file doesn't say anything about it.




回答2:


This should work, try it.

<style>
  .foo{
    border:1px solid blue;
    color:red;
  }
  .foo{
    border:4px solid orange !important;
  }
</style>
<div class="foo">this will have an orange border and red text (no blue border)</div>



回答3:


Yes it is possible. The definitions in second file will overwrite the definitions of the first file. There is no way to differentiate between the two but to prepend the class names according to the file.




回答4:


...that have similar names for the selectors

If the names really are similar and not identical then there shouldn't be a problem.




回答5:


do you mean 2 definitions for the same class? or 2 class names on an element?

The first case, no.

<style>
  .foo{
    border:1px solid blue;
    color:red;
  }
  .foo{
    border:4px solid orange;
  }
</style>
<div class="foo">this will have an orange border and red text (no blue border)</div>

The second case, yes

<div class="class1 class2">this is valid</div>


来源:https://stackoverflow.com/questions/372655/using-two-css-files-in-the-same-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!