Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

html - How do I get these canvases to align properly?

I am trying to display the three canvases over one another, but they just display beside each other. I have very little experience with css. What do I need to add?

<body>
<div id="canvas_container">
<canvas id="Background" width="330" height="330"></canvas>
<canvas id="mycanvas" width="330" height="330"></canvas>
<canvas id="Overlay" width="330" height="330"></canvas>
</div>

<style>
.canvas_container {
  position: relative;
}

.container > canvas {
  position: absolute;
  top: 0;
  left: 0;
}
</style>

<div id="codeinput">
<br>
The "X" goes here:<input type="text" id="shapeX">

<br>
The "Y" goes here:<input type="text" id="shapeY">

<br>
Choose the color: <input type="text" id="shapeColor" value="green">


<script>
unrelated javascript
</script>

<br>
<button onclick=makeSquare();>Draw shape</button>
</div>
</body>

question from:https://stackoverflow.com/questions/65921596/how-do-i-get-these-canvases-to-align-properly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your css ok but you set wrong selector. You has id = canvas_container but set styles for class canvas_container and class container

#canvas_container {
  position: relative;
  width: 330px;
  height: 330px;
}

#canvas_container > canvas {
  position: absolute;
  top: 0;
  left: 0;
  border: 1px solid red;
}
<div id="canvas_container">
  <canvas id="Background" width="330" height="330"></canvas>
  <canvas id="mycanvas" width="330" height="330"></canvas>
  <canvas id="Overlay" width="330" height="330"></canvas>
</div>

<div id="codeinput">
  <br>
  The "X" goes here:<input type="text" id="shapeX">
  <br>
  The "Y" goes here:<input type="text" id="shapeY">
  <br>
  Choose the color: <input type="text" id="shapeColor" value="green">
  <br>
  <button onclick=makeSquare();>Draw shape</button>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...