# Running CKEditor 5 inside a native dialog

To run CKEditor 5 inside a modal `<dialog>` element, add an empty, fixed-position element to the dialog and pass it to the editor as [`config.ui.overlayContainer`](../../api/module_core_editor_editorconfig-UiConfig.md#member-overlayContainer). The editor then renders its balloons, tooltips, and dialogs in that element, above the dialog content.

Without this setting, the editor renders them at the end of `document.body`. A dialog opened with `showModal()` covers the whole page and makes everything outside it inert. The link balloon, the toolbar tooltips, and the rest of the floating user interface stay hidden behind the dialog and do not react to clicks. A dialog opened with `show()` is not modal and does not make the page inert, so the editor works in it without this setting.

<a id="demo"></a>

## Demo

Open the dialog and use the editor inside it. Click the link, type `@` to mention someone, open a comment in the [narrow sidebar](../../features/collaboration/annotations/annotations-display-mode.md#narrow-sidebar), insert an image with the [CKBox file manager](../../features/file-management/ckbox.md), or click a table cell and change the cell properties. Every balloon, tooltip, and editor dialog appears above the dialog content.

The demo creates the editor when the page loads, inside the closed dialog, and keeps it when you close the dialog. Your changes and comments are still there when you open the dialog again. The comment balloons of the narrow sidebar use the same overlay container as the rest of the editor user interface.



<!-- AI-AGENT-NOTE: An interactive demo is embedded here but is not represented in this Markdown file. If you need to see it in action, open this page in a browser (e.g. via a browser-automation MCP like Chrome DevTools or Playwright), or let the user know a live demo is available on this page. -->

<a id="setting-up-the-overlay-container"></a>

## Setting up the overlay container

Add an empty element inside the dialog, next to the editor element:

```html
<dialog id="editor-dialog">
	<div id="editor"></div>
	<div class="editor-overlay"></div>
</dialog>
```

Style the element with fixed positioning:

```css
.editor-overlay {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 0;
	z-index: var(--ck-layer-dialog);
}
```

Each declaration has its own job:

* `position: fixed` places the element relative to the viewport. The `overflow: auto` style of a modal dialog does not clip it, and it still renders above the page together with the dialog.
* `width: 100%` gives balloons the full viewport width to lay out in. With no width, a balloon shrinks to its narrowest size, and a toolbar in a balloon wraps into a single column.
* `height: 0` makes sure the element never covers the dialog content, so clicks always reach the editor. Balloons, tooltips, and dialogs are positioned, so they do not give the element any height, and the explicit value keeps it that way.
* `z-index` puts the balloons above the editor content. A fixed element starts a stacking context of its own, so the `z-index` of the balloons counts only inside it. Without a `z-index` on the element itself, widgets such as images paint over the balloons. The example uses `--ck-layer-dialog`, the layer of the editor dialogs. A lower value works too, as long as it is higher than `--ck-layer-panel-above`, the layer of the toolbar dropdowns. Otherwise, balloons and tooltips show behind an open toolbar dropdown.

> **Warning**
>
> The fixed positioning works only while neither the dialog nor any element between the dialog and the overlay container has a style that makes it the reference for fixed elements. Such styles include `transform` (also `translate`, `rotate`, and `scale`), `perspective`, `filter`, `backdrop-filter`, a `contain` value that includes `layout` or `paint`, `content-visibility: auto`, and a `will-change` that names one of these properties. Any of them, even `transform: translateY(0)`, makes the dialog clip the floating user interface again. Elements outside the dialog do not matter, because a modal dialog renders in the top layer of the page. To animate the dialog, animate its `opacity`, or apply the transform to a wrapper inside the dialog and keep the overlay container outside that wrapper.

Pass the element to the editor as the overlay container. You can create the editor before or after you open the dialog:

**NPM**

```js
import { ClassicEditor, Essentials, Paragraph, Bold, Italic, Link } from 'ckeditor5';

const dialog = document.querySelector( '#editor-dialog' );

dialog.showModal();

ClassicEditor
	.create( {
		attachTo: document.querySelector( '#editor' ),
		licenseKey: '<YOUR_LICENSE_KEY>', // Or 'GPL'.
		plugins: [ Essentials, Paragraph, Bold, Italic, Link, /* ... */ ],
		toolbar: [ 'undo', 'redo', '|', 'bold', 'italic', 'link', /* ... */ ],
		ui: {
			overlayContainer: dialog.querySelector( '.editor-overlay' )
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );
```

**CDN**

```js
const { ClassicEditor, Essentials, Paragraph, Bold, Italic, Link } = CKEDITOR;

const dialog = document.querySelector( '#editor-dialog' );

dialog.showModal();

ClassicEditor
	.create( {
		attachTo: document.querySelector( '#editor' ),
		licenseKey: '<YOUR_LICENSE_KEY>',
		plugins: [ Essentials, Paragraph, Bold, Italic, Link, /* ... */ ],
		toolbar: [ 'undo', 'redo', '|', 'bold', 'italic', 'link', /* ... */ ],
		ui: {
			overlayContainer: dialog.querySelector( '.editor-overlay' )
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );
```

Add the overlay container to the document before you create the editor. A closed `<dialog>` element is still part of the document, so an element inside it is already connected. If the container is not in the document yet, the editor logs the `ui-overlay-container-not-connected` warning, and the floating user interface stays hidden until you insert the container.

The [Running CKEditor 5 inside a shadow DOM](shadow-dom.md) guide explains how the overlay layer works in more detail.

<a id="using-the-dialog-element-as-the-overlay-container"></a>

## Using the dialog element as the overlay container

Passing the `<dialog>` element itself as the overlay container needs no extra element and no CSS. However, a modal dialog has the `overflow: auto` style by default, so it cuts off the floating user interface at its edges. Use the fixed-position element from the [Setting up the overlay container](#setting-up-the-overlay-container) section instead.

For example, with the dialog as the container, the [block toolbar](toolbar.md#block-toolbar) button sits just outside the editing area, at the left edge of the dialog, and only half of it shows.

<a id="closing-the-dialog"></a>

## Closing the dialog

Closing the dialog hides the editor but does not destroy it. You can keep the editor for the next opening, as the demo does.

To release the editor instead, create a new editor every time you open the dialog and destroy it in the `close` event listener of the dialog. The event fires for every way of closing the dialog, including the `Esc` key and a `<form method="dialog">` button, so one listener covers them all. Register the listener once per opening, so every editor is destroyed exactly once. Wait for the promise that `create()` returns, so the listener also works when the dialog closes before the editor is ready. The destroyed editor leaves its element empty, so pass the saved data to the next editor as its initial data:

```js
let savedData = '<p>Initial content.</p>';

function openEditorDialog() {
	dialog.showModal();

	const editorPromise = ClassicEditor
		.create( {
			attachTo: document.querySelector( '#editor' ),
			root: {
				initialData: savedData
			},
			// ...
		} );

	dialog.addEventListener( 'close', () => {
		editorPromise
			.then( editor => {
				savedData = editor.getData();

				return editor.destroy();
			} )
			.catch( err => {
				console.error( err );
			} );
	}, { once: true } );
}
```

While the focus is in a balloon, `Esc` acts on the balloon, and the dialog stays open. In a balloon with a text field, such as a comment thread, the first press leaves the field and the second one closes the balloon. When the editing area has the focus, some features handle `Esc` first: it closes the link balloon, the mention list, or a comment balloon of the narrow sidebar, and inside a table cell or an image caption it selects the whole widget. Otherwise, also when only a contextual toolbar such as the image, table, or balloon toolbar is shown, `Esc` closes the dialog.

---

Full index of the CKEditor 5 documentation: [llms.txt](../../../llms.txt)
