I'm looking to style a modal dialog (using UI Dialog) with unique CSS that is separate from the traditional dialog, so in essence to have two jQuery dialogs that each look different.
I've styled one, for example,
<div id="dialog_style1" class="dialog1 hidden">One content</div>
and another
<div id="dialog_style2" class="dialog2 hidden">Another content</div>
Unfortunately I've noticed that using separate CSS to style parts of the dialog box, like
.dialog1 .ui-dialog-titlebar { display:none; }
.dialog2 .ui-dialog-titlebar { color:#aaa; }
doesn't work because .ui-dialog-titlebar
does not have the class .dialog1
, and I can't do an addClass
either without breaking into the plugin.
An alternative would be to have an element like body
have a unique class/id (depending on which one I want), but that would preclude having both dialogs in the same page.
How can I do this?
I created custom styles by just overriding jQuery classes in inline style. So on top of the page, you have the jQuery CSS linked and right after that override the classes you need to modify:
The current version of dialog has the option "dialogClass" which you can use with your id's. For example,
Then the CSS would be
The standard way to do this is with jQuery UI's CSS Scopes:
Unfortunately, the jQuery UI dialog moves the dialog DOM elements to the end of the document, to fix potential z-index issues. This means the scoping won't work (it will no longer have a ".myCssScope" ancestor).
Christoph Herold designed a workaround which I've implemented as a jQuery plugin, maybe that will help.
You can add the class to the title like this:
According to the UI dialog documentation, the dialog plugin generates something like this:
That means what you can add to any class to exactly to first or second dialog using jQuery's closest() method. For example:
and then CSS it.
Run the following immediately after the dialog is called in the Ajax:
This applies just to the dialog that is opened, so it can be changed for each one used.
(This quick answer is based on another response on Stack Overflow.)