Question
Is it possible to make one CSS class inherit styles from another class, or even from multiple classes?
For example:
.something {
display: inline;
}
.else {
background: red;
}
I would like to do something conceptually like this:
.composite {
.something;
.else;
}
The goal is for the .composite class to behave as if it includes both sets of styles, so that it displays inline and also has a red background.
Short Answer
By the end of this page, you will understand why CSS classes do not directly inherit from other classes, what CSS inheritance actually means, and how to reuse styles correctly using multiple classes, grouped selectors, and common real-world CSS patterns.
Concept
In CSS, a class does not inherit from another class in the way classes do in object-oriented programming.
What CSS does have is:
- Inheritance of certain properties from parent elements to child elements in the HTML tree
- Cascade and specificity, which decide which rules apply
- The ability to assign multiple classes to the same element
- Grouped selectors to share declarations across selectors
So this is not valid CSS:
.composite {
.something;
.else;
}
CSS has no built-in syntax for "include this class inside another class." A class selector is just a way to target elements; it is not a reusable object that can be imported into another selector.
What CSS inheritance actually means
In CSS, inheritance usually refers to a child element receiving some property values from its parent element.
For example:
body {
color: blue;
}
Text inside child elements often becomes blue because color is an inheritable property.
But properties like background and display do not normally inherit.
How to reuse styles instead
To achieve the effect you want, you typically use one of these approaches:
- Apply multiple classes to the same HTML element
- Group selectors so multiple classes share the same declarations
- Create a new class that repeats or combines the needed properties
- Use preprocessors like Sass if you want language features such as
@extendor mixins
Why this matters
Style reuse is important in real projects because it helps you:
- avoid duplication
- keep CSS easier to maintain
- build consistent UI patterns
- reduce bugs when updating styles
Understanding the difference between inheritance, cascade, and class composition is a key CSS foundation.
Mental Model
Think of a CSS class like a label on a toolbox, not a toolbox inside another toolbox.
- A class such as
.somethingis a label that says, "Apply these rules to any element with this class." - Another class such as
.elseis a different label. - If you want both sets of tools, you put both labels on the same element.
Example:
<div class="something else">Hello</div>
That element gets styles from both classes.
So instead of one class inheriting another, CSS usually works by combining rules that match the same element.
A second helpful analogy:
- Inheritance in CSS = a child borrowing some values from its parent element
- Multiple classes = wearing multiple badges at once
- Cascade = deciding which badge wins if two badges give conflicting instructions
Syntax and Examples
The most common solution is to give the element multiple classes.
<span class="something else">Text</span>
.something {
display: inline;
}
.else {
background: red;
}
This element will:
- use
display: inline - use
background: red
Example: combine reusable utility classes
.inline {
display: inline;
}
.bg-red {
background: red;
}
<span class="inline bg-red">Warning</span>
Example: share declarations with grouped selectors
If two selectors should always share some styles, write them together:
.something,
.composite {
display: inline;
}
.else,
.composite {
background: red;
}
Now .composite gets both properties:
<span class="composite">Hello</span>
Example: a combined selector in HTML
You can also target elements that have both classes:
.something.else {
border: 1px solid black;
}
This selector means: apply the rule only when an element has both something and else.
<span class="something else">Styled element</span>
Step by Step Execution
Consider this example:
<span class="something else">Hello</span>
.something {
display: inline;
}
.else {
background: red;
}
Here is what happens step by step:
-
The browser reads the HTML element:
- It sees a
span - It sees two classes:
somethingandelse
- It sees a
-
The browser reads the CSS rules.
-
It checks which selectors match the element:
.somethingmatches because the element has classsomething.elsematches because the element has classelse
-
The browser collects declarations from both matching rules:
display: inlinebackground: red
-
Since these properties do not conflict, both are applied.
-
The final result is an inline element with a red background.
What if rules conflict?
.a {
color: blue;
}
.b {
color: red;
}
<p class="a b">Text</p>
Now both rules set color. Because the selectors have the same specificity, the one that appears later in the CSS wins.
So the final color is red.
This is the cascade, not class inheritance.
Real World Use Cases
Reusing styles without class inheritance is common in real projects.
UI utility classes
Many codebases use small reusable classes such as:
.hidden { display: none; }
.text-center { text-align: center; }
.bg-red { background: red; }
.rounded { border-radius: 8px; }
Then developers combine them in HTML:
<div class="bg-red rounded text-center">Alert</div>
Buttons with shared and variant styles
.btn {
padding: 8px 12px;
border: none;
}
.btn-primary {
background: blue;
color: white;
}
.btn-danger {
background: red;
color: white;
}
<button class="btn btn-primary">Save</button>
<button class="btn btn-danger">Delete</button>
State-based styling
A component might have a base class plus state classes:
<div class="card selected"></div>
<div class="card disabled"></div>
This keeps styling flexible without pretending one class inherits another.
Design systems
Large systems often separate styles into:
- base component classes
- modifier classes
- utility classes
- state classes
That structure allows controlled composition and easier maintenance.
Real Codebase Usage
In production CSS, developers usually solve this problem with composition patterns rather than inheritance.
Base class plus modifier class
A very common pattern:
.badge {
display: inline-block;
padding: 4px 8px;
}
.badge-danger {
background: red;
color: white;
}
<span class="badge badge-danger">Error</span>
Utility-first composition
Some teams use many tiny classes and combine them directly in markup.
Example idea:
<div class="inline bg-red rounded">Item</div>
Grouped selectors for shared rules
When two classes should share declarations, developers often write:
.message,
.alert {
padding: 12px;
}
State and guard-style class usage
In JavaScript apps, classes are often toggled based on conditions:
if (hasError) {
element.classList.add('error');
}
Then CSS rules layer on top of the base component styles.
Preprocessor support
In Sass, developers may use features like @extend or mixins for reuse. That is not plain CSS, but it is common in real codebases where teams want abstraction.
The key idea in real projects is:
- keep base styles separate
- add variants with extra classes
- avoid copying the same declarations too many times
- rely on cascade carefully, not accidentally
Common Mistakes
Mistake 1: Expecting CSS classes to behave like programming classes
Broken idea:
.composite {
.something;
.else;
}
This is not valid CSS.
Fix
Use multiple classes in HTML or duplicate/share declarations through selectors.
<span class="something else">Hello</span>
Mistake 2: Confusing inheritance with multiple matching selectors
Beginners often think a class receives another class's styles automatically. It does not.
.parent {
color: blue;
}
.child {
background: red;
}
<div class="parent">
<span class="child">Text</span>
</div>
The span may inherit color: blue from the parent element, but not because .child inherits from .parent.
Mistake 3: Assuming all properties inherit
Not all CSS properties inherit from parent elements.
- Often inherited:
color,font-family,font-size - Usually not inherited:
display,margin,padding,background
Mistake 4: Ignoring conflicts between classes
.a { color: blue; }
.b { color: red; }
<p class="a b">Text</p>
Both classes apply, but only one color value wins.
Fix
Understand that the cascade decides the final value based on:
- specificity
- source order
- importance
Mistake 5: Creating overly specific selectors
div.container .card.primary.special {
color: red;
}
Highly specific selectors make style reuse harder.
Fix
Prefer simpler, composable classes when possible.
Mistake 6: Copy-pasting too much CSS
Repeated declarations increase maintenance cost.
Fix
Use:
- shared selectors
- base classes plus modifiers
- utility classes
- CSS custom properties when useful
Comparisons
| Concept | What it means | Example | Best use |
|---|---|---|---|
| CSS inheritance | Child elements receive some property values from parent elements | color flowing from body to p | Text and font-related styling |
| Multiple classes | One element has several class names | class="something else" | Combining reusable styles |
| Grouped selectors | Several selectors share the same declarations | .a, .b { color: blue; } | Avoiding repeated rules |
| Combined selector | Match elements that have all listed classes | .a.b { font-weight: bold; } | Styling a specific class combination |
| Repeated declarations | A new class manually includes needed properties | .composite { display:inline; background:red; } | Simple one-off cases |
Sass @extend or mixins | Preprocessor features for style reuse | @extend .something; | Projects using Sass, not plain CSS |
Multiple classes vs inheritance
- Multiple classes apply styles because the element matches multiple selectors.
- Inheritance happens through the HTML parent-child relationship.
Plain CSS vs Sass
Plain CSS does not let one class include another.
Sass can offer features like:
.composite {
@extend .something;
}
But that is SCSS/Sass, not standard CSS.
Cheat Sheet
/* Define separate reusable classes */
.something {
display: inline;
}
.else {
background: red;
}
<!-- Combine classes on one element -->
<span class="something else">Hello</span>
Quick rules
- A CSS class does not inherit from another CSS class.
- CSS inheritance happens from parent elements to child elements.
- To reuse styles, usually apply multiple classes to the same element.
- If selectors conflict, the cascade decides the winner.
- Group selectors when multiple classes should share declarations.
Useful patterns
Multiple classes
<div class="btn btn-primary"></div>
Grouped selectors
.a,
.b {
padding: 8px;
}
Match an element with both classes
.a.b {
border: 1px solid black;
}
Combined one-off class
.composite {
display: inline;
background: red;
}
Properties that often inherit
colorfont-familyfont-sizeline-height
Properties that usually do not inherit
displaybackgroundmarginpaddingborder
FAQ
Can a CSS class inherit another class directly?
No. In plain CSS, one class cannot directly inherit or include another class.
How do I make one element use styles from two classes?
Add both class names to the element:
<div class="something else"></div>
Is CSS inheritance the same as class reuse?
No. CSS inheritance is about child elements inheriting some properties from parent elements. Class reuse is usually done by applying multiple classes or sharing declarations.
Why does only one value apply when two classes set the same property?
Because of the CSS cascade. If specificity is equal, the rule written later usually wins.
Can I combine classes into one selector?
Yes. .a.b matches elements that have both classes, but it does not make one class inherit the other.
Is there any way to extend classes in CSS-like syntax?
Not in plain CSS. Preprocessors like Sass provide @extend and mixins for that purpose.
Should I use one big class or many small classes?
It depends on your project, but many codebases prefer composable classes because they are easier to reuse and maintain.
Mini Project
Description
Build a small notification badge system that demonstrates how CSS style reuse works without class inheritance. You will create reusable base and modifier classes, then combine them on HTML elements to produce different visual results.
Goal
Create badges that share common styling and use additional classes for color variants and display behavior.
Requirements
- Create a base badge class with shared padding, font size, and border radius.
- Create at least two modifier classes such as a red error badge and a green success badge.
- Add a reusable class that controls inline display behavior.
- Apply multiple classes to each badge in the HTML.
- Ensure the final badges visibly combine styles from all matching classes.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.