通过条件注释共享 LESS CSS 变量

发布于 2024-11-30 22:16:44 字数 373 浏览 6 评论 0原文

我有一个 LESS 样式表 styles.less ,其中包含一些我需要在 ie7.css 样式表中访问的变量,该样式表是使用条件注释加载的。

鉴于这些是完全独立的样式表,并且考虑到我必须将 ie7.css 转换为 ie7.less,我是否可以使这些 CSS 变量在跨平台可用两个样式表?

即我希望能够执行以下操作:

# styles.less, always loaded
@labelwidth: 150px;

# ie7.less, sometimes loaded
label{ margin-left: @labelwidth; }

I have a LESS stylesheet styles.less with some variables that I need to access in my ie7.css stylesheet, which is loaded using conditional comments.

Given that these are entirely separate stylesheets, and given that I would have to convert ie7.css to ie7.less, is it possible for me to make these CSS variables available across the two stylesheets?

i.e. I would like to be able to do something like the following:

# styles.less, always loaded
@labelwidth: 150px;

# ie7.less, sometimes loaded
label{ margin-left: @labelwidth; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-12-07 22:16:44

最好的方法是仅使用两种不同的样式表:一种适用于所有人,另一种适用于 ie,其中包括所有人的样式表。因此,在 HTML 中,使用智能条件注释,它看起来像:

<!--[if gt IE 7]><!-->
    <link rel="stylesheet" href="styles.less" />
<!--<![endif]--><!--[if lt IE 8]>
    <link rel=stylesheet href="ie.less">
<![endif]-->

ie.less 中,您可以导入 styles.less

@import url(styles.less);

/* Your styles for IE */

因此,您将获得两个好处:

  1. 您可以获得所有来自 ie.lessstyles.less 的变量。
  2. 您在 IE 上仅收到一个请求(它仅加载一个样式表)。

The best way of doing so is to use only two different stylesheets: one for everyone and one for ie, which includes the everyone's. So, in HTML, using smart Conditional Comments it would look like:

<!--[if gt IE 7]><!-->
    <link rel="stylesheet" href="styles.less" />
<!--<![endif]--><!--[if lt IE 8]>
    <link rel=stylesheet href="ie.less">
<![endif]-->

And in ie.less you can import styles.less:

@import url(styles.less);

/* Your styles for IE */

So, you gain two benefits:

  1. You get all the variables from styles.less in ie.less.
  2. You get only one request on IE (it loads only one stylesheet).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文