How to override the properties of a CSS class using another CSS class

后端 未结 6 851
灰色年华
灰色年华 2020-11-28 02:56

I am fairly new to CSS3 and I want to be able to do the following:

When I add a class into a an element, it overrides the properties of another class used in this sp

6条回答
  •  离开以前
    2020-11-28 03:38

    You should override by increasing Specificity of your styling. There are different ways of increasing the Specificity. Usage of !important which effects specificity, is a bad practice because it breaks natural cascading in your style sheet.

    Following diagram taken from css-tricks.com will help you produce right specificity for your element based on a points structure. Whichever specificity has higher points, will win. Sounds like a game - doesn't it?

    Checkout sample calculations here on css-tricks.com. This will help you understand the concept very well and it will only take 2 minutes.

    If you then like to produce and/or compare different specificities by yourself, try this Specificity Calculator: https://specificity.keegan.st/ or you can just use traditional paper/pencil.

    For further reading try MDN Web Docs.

    All the best for not using !important.

提交回复
热议问题