0

In CSS, I was wondering if there was some way how one could use the properties given to an element via a class that would overwrite those given from a class. For example, it would look something like this:

.div_class {
  font-family: Arial;
  color: white;
}

.special_class {
  font-family: "Times New Roman";
  color: green;
}

And the HTML would look something like this:

<div class="div_class">
  <h3>Normal</h3>
  <h3 class="special_class">Special  </h3>
  <h3>Normal</h3>
</div>

Apologies if I've made an error in my code, but this is the basic gist. Thanks!

8
  • Have you tried !important ? Commented Feb 6, 2017 at 12:55
  • 1
    I am not sure what you are asking. Can element class property overwrite parent's properties? Commented Feb 6, 2017 at 12:55
  • Your sample code seems good enough Commented Feb 6, 2017 at 12:56
  • 4
    you can use .div_class .special_class {} to overwrite, please try to avoind !important - it can get your code messy Commented Feb 6, 2017 at 12:57
  • 1
    You're code is working as expected codepen.io/sujanadiga/pen/ygqBWv Commented Feb 6, 2017 at 12:58

1 Answer 1

3

You've got some mistakes in your code, as unclosed tag h3 and unclosed css rules, I mean you didn't put a closing braces in the rules. Here is a fixed code

.div_class {
  font-family: Arial;
  color: yellow;
  text-shadow: 1px 1px 0px #000;
}
.special_class {
  font-family: "Times New Roman";
  color: green;
  text-shadow: initial;
}
<div class="div_class">
  <h3>Normal</h3>
  <h3 class="special_class">Special</h3>
  <h3>Normal</h3>
</div>

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

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.