I am struggling with adjusting the margins of my canvas to ensure equal spacing on both sides without any horizontal scroll bars.
Desired Outcome: Equal margin on both sides of canvas without any horizontal scroll bars.
Issue: The margin-right
property is not working as expected. While some solutions suggest specifying a fixed width, that is not suitable for my needs. I require the canvas to dynamically adjust its dimensions based on the window size.
To address this, I am using the following Javascript function:
window.addEventListener('resize' , resizeCanvas , false);
function resizeCanvas(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight/1.2;
}
Do you have any alternative solutions to this problem?
Regarding the overflow issue, adding overflow-x: hidden
to the body
only hides the scrollbar but does not fix the problem. The canvas still extends beyond the screen, making the right border invisible.
View here
Below is the code I am working with:
HTML
<body onload="start()">
<canvas id="myCanvas"></canvas>
</body>
CSS
body{
}
canvas{
border: 1px solid black;
border-radius: 5px;
background-color: #fff;
margin: auto 50px auto 50px; /* left margin works, but right does not */
}
Thank you!
Additional Note:
I have refrained from setting width: 100%
for the canvas to prevent distortion of the content inside it.