diff --git a/ts/handlers/html/HTMLDocument.ts b/ts/handlers/html/HTMLDocument.ts
index 2e9b04bd3..e5d0ff9cc 100644
--- a/ts/handlers/html/HTMLDocument.ts
+++ b/ts/handlers/html/HTMLDocument.ts
@@ -278,10 +278,16 @@ export class HTMLDocument extends AbstractMathDocument {
* Add any elements needed for the document
*/
protected addPageElements() {
- const body = this.adaptor.body(this.document);
+ const adaptor = this.adaptor;
+ const body = adaptor.body(this.document);
const node = this.documentPageElements();
if (node) {
- this.adaptor.append(body, node);
+ const child = adaptor.firstChild(body);
+ if (child) {
+ adaptor.insert(node, child);
+ } else {
+ adaptor.append(body, node);
+ }
}
}