Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
Canvas takes up space of parent div

I've done a lot of searching, but I am unable to solve this problem I'm having.

I have a canvas nested in a div, with the intention of having the div serve as the background for the canvas, so that I can draw objects on it and whatnot.

When I run my code, the canvas appears side by side with the div, and I am unsure what to change to fix it.

// html
<body>
    <div id="root">
        <div id='board'>
           <div>test</div>
           <canvas></canvas>
        </div>
    </div>
</body>

// css
#board {
    position: relative;
    display: flex;
    width: 640px;
    height: 640px;
}

// js
let canvas = document.querySelector('canvas')
let ctx = canvas.getContext('2d')

canvas.width = canvas.offsetWidth
canvas.height = canvas.offsetHeight

Looked up similar questions on here with little success. Insight would be very appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can add this styling for example, to center your canvas and then add more items as you need to the background div.


canvas {
  background-color: green;
  width: 200px;
  height: 200px;
  align-self: center;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  left: 50%;
}

I added an image of the result.

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!