你可以范围CSS文件,使它们只适用于给定元素的后代吗? [英] Can you scope CSS files so that they only apply to the descendants of a given element?

查看:95
本文介绍了你可以范围CSS文件,使它们只适用于给定元素的后代吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个css文件,是否有一种方法来限定整个文件,使其只适用于给定元素中的元素:

Given a css file, is there a way to scope the entire file so that it only applies to elements within a given element:

给定:

<div id="container">
   <span class="some_element"/>
   <!-- etc -->
</div>

是否有一种方法可以将整个css文件应用于container中的所有元素 #container 到每一个css子句?

Is there a way to scope an entire css file to apply to all elements within "container" without prepending #container to every single css clause?

推荐答案

不。一些CSS预处理器允许你编写代码实现相同的东西,虽然。

I’m afraid not. Some CSS pre-processors allow you to write code that achieves the same thing though.

例如。 LESS 实现嵌套规则:

E.g. LESS implements nested rules:

/* This LESS code... */

#header {
  h1 {
    font-size: 26px;
    font-weight: bold;
  }
  p { font-size: 12px;
    a { text-decoration: none;
      &:hover { border-width: 1px }
    }
  }
}

/* ...produces this CSS */

#header h1 {
  font-size: 26px;
  font-weight: bold;
}
#header p {
  font-size: 12px;
}
#header p a {
  text-decoration: none;
}
#header p a:hover {
  border-width: 1px;
}

并且 Andy 提到了 SASS ,同样的事情。

And Andy mentioned SASS, which does the same thing.

这篇关于你可以范围CSS文件,使它们只适用于给定元素的后代吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆