0

Today I am facing a problem of restricting the scope of css. The

<div class="parent">
<div id="childWithNoCss">
<p>No css</p>
</div>
<div id="childWithCss">
<p>Apply css</p>
</div>
</div>

My css is:

div{

color:red}

p{color:blue}

I need to apply the css specific to the id childWithCss. The css is fixed (I cannot change it) just need to limit its scope only to a specific element. I cannot use scope attribute since it is incompatible in some browsers. Is there any other solution?

Regards

Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102

4 Answers4

1

Are you trying to find a workaround for this?

<style scoped>
  p { 
    //some style
  }
<style>

If so, there are some jQuery plugins that do the same thing and work for IE, at least for IE 9 and above. Here's one example:

jQuery scoped CSS plugin

If that's not what you're trying to do, can you not just add some nested css? Does this help at all with what you are trying to do?

Load an external CSS for a specific DIV

Community
  • 1
  • 1
jtmingus
  • 804
  • 5
  • 10
0

You can reset the CSS of an element with the css code: all:initial.

You could use some javascript to do this at runtime for the elements you want to reset. I see you tagged your question with angularJS, so jquery should be available.

jquery solution to reset all css for the p in the divs WITHOUT #childWithCss within .parent:

$('.parent div:not(#childWithCss) p').css('all','initial')

http://jsfiddle.net/2yXsL/3/

JasperZelf
  • 2,731
  • 1
  • 22
  • 34
0

You should be able to do it with a bit of jquery

if ($('.parent').has('#childWithCss')) { 
    $('#childWithCss').find('p').css('color', 'red')
}
  • Works in this case bt what I was looking for was somefin more dynamic. I do not need a workaround to set the css but to just restrict its scope. – Manish Basdeo Jun 05 '16 at 17:58
0

Reset the color of everything except #childWithCSS to the default text color with a bit of jQuery.

$("p:not(#childWithCSS p)").css("color", "initial");
clickbait
  • 2,818
  • 1
  • 25
  • 61