Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to combine classes in CSS? [duplicate]

Tags:

html

css

Suppose I have a en element <div clas="foo bar foobar">[stuff]</div> and I want to replace these three classes with one class (SuperFoo).

While I could find out how foo, bar and foobar are defined, and use each of those elements:

.SuperFoo { color : red } and so on, is it possible to define SuperFoo in terms of foo, bar and foobar?

.SuperFoo { foo bar foobar}?

How can I do this?

like image 593
ale10ander Avatar asked Jun 24 '15 22:06

ale10ander


People also ask

Can an element have 2 CSS classes?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Can you group classes in CSS?

CSS grouping can be used to condense code that has multiple selectors with the same declarations. This makes code easy to read and maintain. Also development time is significantly reduced as there is less code to write. Page load times are reduced as well when using grouping.

Can we declare CSS classes more than once?

We aren't limited to only two here, we can combine as many classes and IDs into a single selector as we want.


2 Answers

Unless you use LESS or SASS's 'mixin' features, there's no real way in plain CSS to "inherit" other classes.

The best you can do is apply all three classes to your DOM element.

If your problem is that you have a ton of those classes already on a page and you want to add properties to all of them, you can do:

.foo, .bar, .foobar {
    color: red;
}

and that will apply color: red to any element that has .foo or .bar or .foobar.

like image 145
br0tat0 Avatar answered Oct 19 '22 23:10

br0tat0


No. But you could use chained classes:

.foo.bar.foobar {
    color: red;
}
<div class="foo bar foobar">[red]</div>
like image 39
Stickers Avatar answered Oct 19 '22 23:10

Stickers