如何在不复制的情况下在另一个类中重用CSS类的内容? [英] How to reuse css class content in another class without copying?

查看:75
本文介绍了如何在不复制的情况下在另一个类中重用CSS类的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将现有的CSS类用作另一个类中的内容?

Is it possible to use existing css class as content in another class ?

我的意思是:

Contained in some library:
.class1 { text-indent: 100 }

 I can not change this:
 <span class="class2" />

class2的定义也包含在另一个库中.因此,我无法直接更改它.

The definition for class2 is also contained in another library. So I could not change it directly.

But I want to do something like that in my CSS file:
.class2 { .class1 }

我知道不可能以这种形式.但是,也许可以使用某种技巧来实现该行为而无需复制class1的内容?我需要这个,因为我想用另一个CSS类的内容重新定义类.我们的项目也使用JQuery,但我宁愿使用CSS.

I know it is not possible in that form. But maybe one can use some trick to achieve the behaviour without copying of the content of class1 ? I need this because I want to redefine class with content from another CSS class. Our project use JQuery as well, but I would do it rather with CSS.

编辑:我应该解释更多,我不能更改.class1的定义方式,因为该类在库中,并且我不能更改span类的标记.

EDIT: I should explain more, I could not change how .class1 is defined, because this class is in a library, and I could not change mark up on span class.

推荐答案

由于没有纯继承,因此无法在标准CSS中执行您要注释的内容.

It is imposible to do in standard CSS what you are commenting, as there is not pure inheritance.

尽管它不适用于您的代码限制,但这是更接近的方法:

Despite it doesn't apply with your code restrictions, this is the closer way to do it:

    .class1, .class2 { text-indent: 100 }

    .class2 { 
            /* Styles you want to have only in class 2 */
    }

    <span class="class2" />

另一方面,为@A. Wolff指出,您仍然可以使用js/jq将类添加到特定元素:$(function(){$('.class2').addClass('class1')});然后只需为这些元素设置特定的CSS规则即可.

On the other hand, as @A. Wolff has pointed out, you can still use js/jq to add class to specific elements: $(function(){$('.class2').addClass('class1')}); Then just set a specifc CSS rule for these elements.

如果您不想使用JS,则需要使用SASS或类似的东西,这些东西会编译"为CSS.

In case you don't want to use JS, for something like that you'd need to use SASS or similar, which "compiles" to CSS.

这篇关于如何在不复制的情况下在另一个类中重用CSS类的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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