Mikhail Kopylov
Mikhail Kopylov

Reputation: 2068

How to refer css style from another?

Here's the sample:

.my-class {
  font-size: 12px;
}

.my-another-class {
  /* here I want to include .my-class style */
  .my-class;
  border: 0;
}

Can I include one css class into another or not?

Upvotes: 0

Views: 134

Answers (3)

Florin Pop
Florin Pop

Reputation: 5135

You can't, but you can do something like this:

.my-class, .my-another-class{
  font-size: 12px;
}

.my-another-class {
  border: 0;
}

Upvotes: 0

Joe
Joe

Reputation: 15812

You can define multiple targets for the .my-class rule, then specify further rules just for .my-another-class:

.my-class,
.my-another-class {
  font-size: 12px;
}

.my-another-class {
  border: 0;
}

You can even then override certain properties, for example

.my-class,
.my-another-class {
  color: red;
  font-size: 12px;
}

.my-another-class {
  border: 0;
  color: blue; /* overrides color: red; on .my-another-class */
}

Upvotes: 3

joews
joews

Reputation: 30330

You can't use a construction like this in plain CSS.

Preprocessors such as Less and Sass support this behaviour with mixins.

Upvotes: 0

Related Questions