Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSS Selector Styles in Another Selector? (NOT @import)

Tags:

css

Is there a way to import the styling of a single CSS selector into another CSS selector and add to it or rewrite properties from it.

Let's say:

.original_class{
background:black;
color:white;
}
.overwrite{
@import(.original_class); /* I know this doesn't work */
color:blue;
border:1px solid green;
}

I can accomplish this by just redeclaring the .original_class and assigning new values (since CSS styles are rewritten from top to bottom), but this will replace the attributes of the original CSS class. What I want is to inherit its properties into another class without having to write them again (duplicate).

like image 801
Dzhuneyt Avatar asked May 25 '12 09:05

Dzhuneyt


2 Answers

Not directly, no.

You could do something like this in your HTML:

<div class="original_class overwrite">...</div>

This will have the same effect, but you will have to do this for every element you want styled that way.

There is also the option of using a CSS pre-processor, like SASS, which supports inheritance/mixins.

like image 94
Jan Hančič Avatar answered Oct 18 '22 10:10

Jan Hančič


You can add the .overwrite selector to the first rule by separating it from the existing selector with a comma (grouping selectors), so the selector rule becomes .original_class, .overwrite:

.original_class,
.overwrite {
background: black;
color: white;
}
.overwrite {
color: blue;
border: 1px solid green;
}

Also, when you write:

this will replace the attributes of the original CSS class

there is no such thing as attributes and class in CSS, not with the intended meaning of OOP I guess. There are rules, selector rules (to select HTML id, classes, elements, attributes and other pseudos), declarations, properties and values.

like image 43
FelipeAls Avatar answered Oct 18 '22 10:10

FelipeAls