# Can I render dom elements in babylonjs?

**URL:** <https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426>\
**Category:** Questions\
**Created:** [August 30, 2021, 2:55am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426 "2021-08-30T02:55:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ywlee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ywlee/32/18273_2.png) [@ywlee](https://forum.babylonjs.com/u/ywlee)\
**Post date:** [August 30, 2021, 2:55am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/1 "2021-08-30T02:55:00Z")

</div>

Hi everyone! I’m new to babylonjs  
I would like to know whether I can render dom elements for example, a div with some paragraphs , in babylonjs ?  
Thanks for reading my newbie question!!

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [August 30, 2021, 6:33am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/2 "2021-08-30T06:33:21Z")

</div>

Hello!  
You want to render a dom element on a 3D object or just display it above the BabylonJS canvas? I think, you are after the latter. You can use your BabylonJS canvas in a background layer and render the dom elements above. One solution is set the canvas style like this: position: absolute, top:0, left:0, and set right, bottom or width, height to the desired size of the canvas. Don’t forget about the z-index property. If you are still unsure how to do it, just let me know, I can create a little example for you.

---

<div class="post-metadata">

**Author:** ![ywlee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ywlee/32/18273_2.png) [@ywlee](https://forum.babylonjs.com/u/ywlee)\
**Post date:** [September 1, 2021, 2:09am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/3 "2021-09-01T02:09:21Z")

</div>

Thanks so much for your reply and sorry I didn’t make my question clear  
But in this way my dom element will always in front of the scene

Is there any way to make it truely inside the canvas?  
For example I can hide the div behind a sphere in the canvas?

---

<div class="post-metadata">

**Author:** ![musk](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/musk/32/17156_2.png) [@musk](https://forum.babylonjs.com/u/musk)\
**Post date:** [September 1, 2021, 5:30am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/4 "2021-09-01T05:30:14Z")

</div>

To make billboards? I think using GUI is a better choice.

---

<div class="post-metadata">

**Author:** ![tanpopo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tanpopo/32/32025_2.png) [@tanpopo](https://forum.babylonjs.com/u/tanpopo)\
**Post date:** [September 1, 2021, 5:48am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/5 "2021-09-01T05:48:10Z")

</div>

Or maybe something like this?

> [@Youtube videos on a mesh (port of CSS3DRenderer.js)](https://forum.babylonjs.com/t/youtube-videos-on-a-mesh-port-of-css3drenderer-js/10600):
>
> We built this Youtube video player to use in our virtual world Cryptovoxels. There are a few people asking about it in these forums so we made a playground so anyone here can use it. Its a port of CSS3DRenderer.js which was what three.js used. @bnolan (founder of Cryptovoxels) also contributed to this code Edit: Old updates This one allows controlling Babylonjs camera and interact with contents of iframe New update Takes into account changes to rotation

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [September 1, 2021, 6:45am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/6 "2021-09-01T06:45:52Z")

</div>

@ywlee we are glad to help you and welcome to the BabylonJS community!  
Yes you can:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/c/a/ca43bdd26f021f72959b98451d4730a1e1a6e6ff.jpeg)

Those are two `h1` elements mixed with the BabylonJS content.

You have to set the z-indexes accordingly. The div must be positioned absolute or fixed to create the layering effect.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/d/3de5b47e72867741b50282f541b8b9c438f4e0f5.png)

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/4/9429b50cdcec15c60d0022d02339ef752ebee526.png)

And don’t forget to set the `alpha` in your clear color to zero:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/8/c/8ce82fa622db73a404f540b871bb52e3ffb2d353.png)

Hope this helps!  
R.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [September 1, 2021, 9:40pm UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/7 "2021-09-01T21:40:07Z")

</div>

There is also the possibility of using [DynamicTexture](https://doc.babylonjs.com/divingDeeper/materials/using/dynamicTexture) with the drawText method, but using the Babylon GUI would probably be easier in a simple case.

---

<div class="post-metadata">

**Author:** ![ozRocker](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ozrocker/32/186_2.png) [@ozRocker](https://forum.babylonjs.com/u/ozRocker)\
**Post date:** [September 2, 2021, 1:33am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/8 "2021-09-02T01:33:18Z")

</div>

Can you provide more details? Its not clear if you want the DOM elements to display flat, like a web-page in the background, or if you want the DOM elements to be manipulated like a 3D object

---

<div class="post-metadata">

**Author:** ![ywlee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ywlee/32/18273_2.png) [@ywlee](https://forum.babylonjs.com/u/ywlee)\
**Post date:** [September 2, 2021, 5:16am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/9 "2021-09-02T05:16:33Z")

</div>

Thank you guys for all the replies, I have read them all!  
I would like to do some math animation in 3d, I need to render formulas (using KaTeX), and the numbers in formulas might change while the formula is following some moving object in the scene  
Actually I have tried rasterized the formulas to canvas then use it as texture, but that was a disaster because converting dom element to texture takes way too much time  
I think what @tanpopo mentioned is the thing I’m looking for, I’ll do some research

So the rumor is true, babylonjs has a very supportive community…thank you guys again!!(especially @roland, providing such good example, I’ll use it in my project too!)

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [September 2, 2021, 7:07am UTC](https://forum.babylonjs.com/t/can-i-render-dom-elements-in-babylonjs/23426/10 "2021-09-02T07:07:45Z")

</div>

You’re welcome dude!

One very important thing here to note. DOM elements are not rendered on VR/AR.

I always try to use the simplest solution. If I can make it in plain HTML and JS I will not opt for a framework or a custom solution. If it gets bigger and you have a complicated DOM tree it’s handy to use some GUI framework. You can use any of the big players available nowadays whether it’s React, Angular, Vue, Svelte, etc.

The BJS GUI is a bit verbose, but after creating some helpers classes/methods you can do very cool things with it very easily.

When mixing HTML layers and making changes to the DOM you have to pay attention to browser reflow and repaint events. Especially CSS animations can degrade performance if not used wisely.  
Check this out:

> **[Understanding Repaint and Reflow in JavaScript](https://medium.com/swlh/what-the-heck-is-repaint-and-reflow-in-the-browser-b2d0fb980c08)**
>
> In this post, I’ll try to answer what exactly repaint and reflow is and doe’s really Real DOM is faster than React’s Virtual DOM?

Happy coding!
