Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse css class content in another class without copying?

Tags:

html

css

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

I mean something like:

/* Contained in some library: */
.class1 { text-indent: 100 }
/* I can not change this: */
<span class="class2">

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

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

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 uses jQuery as well, but I would do it rather with CSS.

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.

like image 374
Tony Avatar asked Dec 04 '13 12:12

Tony


People also ask

Can CSS classes be reused?

To wrap up, the key benefits from using CSS subclasses: They're scalable, flexible and reusable.

How do I override a CSS class with another class?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.

Can you use the same CSS class on multiple elements?

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.

Can an element have 2 CSS classes?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.


2 Answers

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" />

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.

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.

like image 151
Chexpir Avatar answered Oct 01 '22 18:10

Chexpir


CSS has no means to reference one rule-set from another.

Your options include:

Using multiple selectors for things with common rules

.menu,
.nav {
    font-weight: bold;
}

.nav {
    display: inline-block;
}

Using multiple classes on a single element

.menu {
     font-weight: bold;
}

.nav {
     display: inline-block;
}

<li class="menu nav">

Generating your CSS programatically

For example, with SASS

@mixin menu {
    font-weight: bold;
}

.nav {
    display: inline-block;
    @include menu;
}
like image 9
Quentin Avatar answered Oct 01 '22 18:10

Quentin