0

How would I use a CSS inherit when I am using a selector method of styling.

so if I want to style the label I can simply do like this: .stylethis label{style}. But I want to inherit the styles from a different class, like how I am attempting in my CSS, I am trying to inherit style from the class "usethis" to '.stylethis label' .. kind of like this

/* Make second-level headers green */
h2 { color: green; }

/* ...but leave those in the sidebar alone so they use their parent's */
#sidebar h2 { color: inherit; } 

I want to do like that, except in the place of #sidebar h2 I would put #sidebar 'my class name'?

.usethis{
color:red;
font-size:12px;
}

.stylethis label .usethis{
color:inherit;
font-size:inherit;

}
<div class="stylethis">

<label>jay chacko</label>

</div>

6
  • That's not how CSS works. Commented Nov 21, 2017 at 21:41
  • You might be looking for developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables Commented Nov 21, 2017 at 21:42
  • This is CSS not OOPS. I can't understand what you want. Please clarify Commented Nov 21, 2017 at 21:43
  • @BASEERHAIDER I added an image to the question (#sidebar...). I want to do like that, except in the place of #sidebar h2 I would put #sidebar 'my class name'? Commented Nov 21, 2017 at 21:51
  • 1
    Do not place images of code in your question. Place the actual code in text in the question. Commented Nov 21, 2017 at 21:54

2 Answers 2

1

Inheritance of CSS values will only work from parent to child, based on the DOM structure (or the HTML if you will). This a working example of CSS inheritance:

<div class="parent">
    <div class="child"></div>
</div>
.parent {
    color: red;
}
.child {
    color: inherit;
}

I might have misunderstood parts of your question, but if you are asking if you can "inherit" values from one selector to another, the answer will be no. Unless you are using a preprocessor such as SASS or LESS, which support variables to be used across CSS selectors. (And unless we are talking about a child element inheriting its parents styles as above)

To elaborate on your provided example, you may wrap a <h2> inside a sidebar and inherit the sidebars font color like below:

h2 {
  color: green;
}
.sidebar {
  color: red;
}
.sidebar h2 {
  color: inherit;
}
<h2>Regular H2</h2>

<div class="sidebar">
  <h2>Sidebar H2</h2>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad this could be of help :o) If you need to know more about how CSS inheritance works you may want to check out MDN's explanation on CSS Cascade and Inheritance
0

It's quite likely I don't understand your problem but perhaps you need to know that you can have multiple classes on elements. It sounds like all you need to do is add the class of the other object whose styles you want.

<div class="stylethis usethis">
    <label>jay chacko</label>
</div>

If this overrides a style you didn't want to override then you can tweak your CSS to get it back by using the CSS selector that targets only the element with both those classes like this

.stylethis.usethis {
    /* styles here */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.