jquery - widget CSS conflicts with site CSS -


i build widget website , add css dynamically on loading problem conflicting host website css file . following code widget has css file attached on loading :

function main() {     jquery(document).ready(function($) {     /******* load css *******/     var css_link = $("<link>", {              rel: "stylesheet",              type: "text/css",              href: "http://example.com/widget/green.css"         });         css_link.appendto('head');         var txt;         txt='<div id="wrapper">';         txt=txt+'<ul class="tabs">';         txt=txt+'<li id="fixtures_tab"><a href="#fixtures">hepsi</a></li>';         txt=txt+'<li id="live_tab"><a href="#live">canlı</a></li>';         txt=txt+'<li id="finished_tab"><a href="#finished">bitmiş</a></li>';         txt=txt+'<li id="program_tab"><a href="#program">başlamamış</a></li>';         txt=txt+'<li id="postpond_tab"><a href="#postpond">ertelenen</a></li>';         txt=txt+'<li id="selected_tab"><a id="f" href="#fav">oyunlarım (0)</a></li>';         txt=txt+'</ul>';         txt=txt+'<div class="tab-container">';         txt=txt+'<div id="fixtures" class="tab-content"><script type="text/javascript">get_all_today_matches();</script></div>';         txt=txt+'<div id="live" class="tab-content"><script type="text/javascript"></script></div>';         txt=txt+'<div id="finished" class="tab-content"><script type="text/javascript"></script></div>';         txt=txt+'<div id="program" class="tab-content"><script type="text/javascript"></script></div>';         txt=txt+'<div id="postpond" class="tab-content"><script type="text/javascript"></script></div>';         txt=txt+'<div id="fav" class="tab-content"><script type="text/javascript"></script></div>';         txt=txt+'</div>';         txt=txt+'</div>';         $('#widget-container').html(txt);     }); }  

i want know there way isolate widget's css ??

by way main function called anonymous function (for isolation purpose)

ps: used css reset still same problem .

you can namespace of css. avoid having issues common css class names container or wrapper, example:

.my-amazing-widget--tab-container {     height: 100px; } 

if of css namespaced above, not website have widget name in css.

i avoid using declarations !important user suggested in comments—it possible if override common class styles end negatively affecting website's styles.

i avoid global declarations importing css reset widget. css resets override elements body, div, etc. can negatively affect host website's css.

you need update related html use namespace. css namespacing best approach problem , best practice when developing widgets embedded in other websites , applications.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -