Lightining component Canvas width based on screen width

auralightninglightning-aura-components

how can i set the width of a canvas in a lightning component to be 100% of the screen width? If i apply the value "100%" it doesn't work.

            <canvas aura:id="canvas" width="300" height="250" style="border:1px solid #ddd;background: white;">
</canvas>

Best Answer

You can either use a container element with relative/absolute positioning:

<div style="position: relative; height: 100px; width: 100%">
    <canvas style="position: absolute; width: 100%; height: 100%">
    </canvas>
</div>

Or you can specify 100vw:

<canvas style="width: 100vw; height: 50vh">
</canvas>

Where 100 units of vw is 100% of the viewport width, and 100 units of vh is 100% of the viewport height (here, demonstrated to be set to 50%).

Related Topic