Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Widget Affecting Style of the Page it is embedded in

Tags:

css

widget

I'm currently developing a widget that can be embedded within a page. However, when embedded, it affects the style (font, text, layout, etc.) of the page it is embedded in.

I wonder how Clearspring and other widget frameworks encapsulate their widgets so as not to affect the embedding page.

Thanks.

like image 499
dannyroa Avatar asked May 26 '09 05:05

dannyroa


People also ask

What is a JavaScript widget?

You use the JavaScript widget to embed JavaScript code in your page. JavaScript is used to enhance the functionality of your website. For example, you can use JavaScript to validate user input. After you place the JavaScript widget on your page, you configure which JavaScript to include.


1 Answers

Make your widget, say under one div with a unique Id (or class if there will be multiple) that is least likely to clash with others on the host page. A good example might be #company-widjet-name. See how jQuery UI does it (.ui-widget input).

Then you might need to perform a sort of localised reset, to avoid the parent page's CSS from stuffing up your design. Modify something like Eric Meyer's reset to suit. Please avoid the #uniqueId * { padding: 0, margin: 0 } as it can cause headaches.

As long as you do

#uniqueId a {
    property: value;
}

The specificity should be strong enough to style the elements correctly without letting the host page's CSS from changing it unintentionally.

like image 195
alex Avatar answered Nov 15 '22 06:11

alex