Is there any way to stop the conflict between same class or id of multiple css files. As I am explaining below for better understanding:
There is a master web page which has several <div>
but there is a <div class"dynamic">
which always reload the contents including css files
. Let's suppose if any class of master page has the same name to reloaded elements' class while properties are different. Then how should I handle this to stop the conflict.
master.html
<html>
<head> //attached master.css file here </head>
<body>
<div class="myClass"> </div>
<div class="dynamic"> /* often reload elements by ajax */ </div>
</body>
</html>
master.css
.myClass { height: 100px; width: 150px; background : red;}
.dynamic { height: 200p; width: 200px; }
now i am showing the reloaded html elements & css files into dynamic div
of master page
reloaded tag line by ajax : <div class"myClass"> </div>
reload.css
.myClass{height: 30px; width: 25px; background: yellow; }
Now as you can see there are two classes with same name but different properties. Then how should I stop the confliction?
@Edit Thanks everyone for your support & time but my problem is different here.
the dynamic reloaded contents & css files are streaming from the client/user machine while master html page & it's css
streaing directly from server.
so whatever the contents loads in dynamic div, it's ing from client side (e.g. tag lines & css, js
). in that case i am not able to handle the css file which is just reloaded by ajax()
so i think it can be sort out using js/jQuery fn()
.
Is there any way to stop the conflict between same class or id of multiple css files. As I am explaining below for better understanding:
There is a master web page which has several <div>
but there is a <div class"dynamic">
which always reload the contents including css files
. Let's suppose if any class of master page has the same name to reloaded elements' class while properties are different. Then how should I handle this to stop the conflict.
master.html
<html>
<head> //attached master.css file here </head>
<body>
<div class="myClass"> </div>
<div class="dynamic"> /* often reload elements by ajax */ </div>
</body>
</html>
master.css
.myClass { height: 100px; width: 150px; background : red;}
.dynamic { height: 200p; width: 200px; }
now i am showing the reloaded html elements & css files into dynamic div
of master page
reloaded tag line by ajax : <div class"myClass"> </div>
reload.css
.myClass{height: 30px; width: 25px; background: yellow; }
Now as you can see there are two classes with same name but different properties. Then how should I stop the confliction?
@Edit Thanks everyone for your support & time but my problem is different here.
the dynamic reloaded contents & css files are streaming from the client/user machine while master html page & it's css
streaing directly from server.
so whatever the contents loads in dynamic div, it's ing from client side (e.g. tag lines & css, js
). in that case i am not able to handle the css file which is just reloaded by ajax()
so i think it can be sort out using js/jQuery fn()
.
-
as i know, if i write like this in
reload.css : .dynamic> .myClass {...}
then problem will be sort out but i want to know other method – user1010399 Commented Nov 22, 2011 at 10:05
4 Answers
Reset to default 2You could apply the cascading rules of the CSS:
In your case, div.myClass
inside div.dynamic
should override div.myClass
belongs to the body
.
you adjust the reload.css
rules to
.dynamic .myClass{height: 30px; width: 25px; background: yellow; }
The cascading rules which are applied when determine which rules should apply to html div could be referenced here
Updated 11.23
As the OP only have control over master.css
, the above solution won't work. Thus, I suggest use child selector to limit the CSS rules to only the outer div.myClass
. Modify the rule in your master.css
to:
body > .myClass {...}
This rule will only apply to the .myClass
which is the child of body
. It leaves the spaces of styling for inner .myClass
div.
Option 1: A more specific selector
.dynamic .myClass { }
This selector selects the .myClass
element that is a descendent of .dynamic
.
.dynamic > .myClass { }
This selector selects the .myClass
element that is a direct child of .dynamic
.
Option 2: Inline CSS
<div class="dynamic">
<div class="myClass" style="background-color: yellow;"></div>
</div>
Option 3: Use a different class.
UPDATE
If you want to avoid the previous defined property to be overwritten by a later defined value, you can use the !important
syntax.
.myClass { background-color: red !important; } /* Sets the property to red */
.myClass { background-color: yellow; } /* Property is NOT overwritten */
If I understand your question correctly, this should sort it.
So you should add !important
to the properties that seem to be overwritten.
div.myclass { ble ble }
div.main div.myclass { ble ble }
<body>
<div class="myclass"></div>
<div class="main><div class="myclass"></div></div>
</body>
Whichever css class of the same name is loaded last will overwrite anything set by the earlier class. However, if you use an inline style attribute this will always take precedence over anything set by the css file (so using an inline style is one option).
You could also use different style names or clarify your style with tag names div.myClass
or id's #myDiv.myClass
.