简体   繁体   中英

How to apply a defined class to a new class in my css?

i want to ask if is possible to apply a html class defined in css1.css to another html class who i want to defined in css2.css?

basicly

css1.css contain

div.classPredefined { border: solid 1px #000; }

and css2.css

div.newClass { background: #CCC; apply-class: classPredefined; }

I know there is no exist apply-class in css, but i want in my HTML to apply .newClass over element and automatically apply .classPrededined.

Ignore option <div class="classPrededined newClass"></div>

Thank you

So do you want the new style to always apply? If so, you can use the exact same class name in the file loaded after the first one and the latter style will be added to the first one. So if you have

div.classPredefined { border: solid 1px #000; }

In the css1.css, you can use the same class in css2.css

div.classPredefined { background: #CCC; }

The file will adapt both of the styles.

You could try to explain what you want on this..

Why don't you use.

div.newClass { 
    @import url("css1.css");
    background: #CCC; 
    //border: solid 1px #000; //css from the older class
}

You Can easily add css file within another css file just doing @import("");

I assume that you cannot manually alter the html and you want to add a specific class on a click event.I dont think there is a css method to add class to an existing element,but you can it is achieve using jQuery.

$(".newClass").addClass("classPredefined");

This simply add a predefined class to an element with the class newClass.

If you simply want two elements with different classes to have the same css style, I suppose you could create a new class with the common properties and add this class to necessary elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM