将样式表限制为一个标记和后代

Roo*_*nan 7 html css styles stylesheet

我想在我正在使用的某个网站上使用twitter bootstrap.此站点需要预设模板才能用于页面的某些部分.

如果我包含twitter bootstrap样式表,它会混淆原始模板.

我想要包含外部bootstrap.cc样式表,但只使它适用于class ="mycontent"后代的标签

<html>
<link href="original stylsheet.css">
<link href="bootstrap.css">
...
...
<div class="header">
original stlesheet to be used here
</div>
<div class="mycontent">
bootstrap css to work for all descendents of my content
</div>
Run Code Online (Sandbox Code Playgroud)

我知道一种方法是编辑css并在每个标记之前添加.mycontent.但我想知道是否有更智能的解决方案

Pav*_*vlo 5

Scoped CSS

如果scope属性存在,则<style>仅适用于其父元素.

<div>
  <style scoped>
  /* your css here */
  <style>
  <!-- content -->
</div>
Run Code Online (Sandbox Code Playgroud)

它缺乏浏览器支持,但有一个polyfill:jQuery Scoped CSS插件.

补充阅读:使用Scoped CSS挽救一天.


当前的浏览器支持:http://caniuse.com/#feat=style-scoped