Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break CSS inheritance?

Tags:

css

I have created a web widget. That my client put in their websites. Basically on load it calls webservice and loads data specific to client.

As a result it looks like:

<div class="widget">
   <div class="container">
   </div>
</div>

Dynamically I apply CSS styles to my widget class. To look consistent with our corporate styling.

The problem is when client application styles overwrite the style I applied run time.

For example if client has, it overwrites my styles:

body {
  margin: 0 auto;
  padding: 0;
  width: 90%;
  font: 70%/1.4 Verdana, Georgia, Times, "Times New Roman";
}

Is there any way to break inheritance? Just to say whatever div with class widget styles has do not inherit parent styles.

like image 430
German Avatar asked Aug 07 '12 16:08

German


People also ask

How do you remove inherited property in CSS?

The unset CSS keyword resets a property to its inherited value if the property naturally inherits from its parent, and to its initial value if not.

How do you stop inheritance?

To prevent inheritance, use the keyword "final" when creating the class. The designers of the String class realized that it was not a candidate for inheritance and have prevented it from being extended.

How do you stop color inheritance in CSS?

The unset value resets a noninherited property to its initial value. The div element has a color property set to green , the html has a color property set to red , and the div. div 's color property is set to unset . The color property is an inherited CSS property, so the unset will reset the div.

How do you stop inheritance in HTML?

If you really want to avoid inheritance, then all: revert has you covered. All divs will be display: block and spans will be inline . All em tags will be italic and strong tags will be bold. Links will be blue and underlined.


1 Answers

I don't think you can break CSS inheritance per se, but you can try to circumvent it by following the rules of CSS specificity. Here's a good article about Specificity.

As a last resort, try adding !important to styles in the widget to give it a higher specificity than the default styles.

#myWidget{
    font: 100%/1 "Times New Roman", Serif !important;
}

If the client is also using !important it could cause problems. If you could setup a jsFiddle with an example, we could help find the issue.

Note, going the route of adding !important should be a last resort as it's the 'nuclear option' of style specificity.

like image 135
jmbertucci Avatar answered Oct 19 '22 06:10

jmbertucci