html - 将 Canvas 的高度和宽度设置为其包含的 div 的高度和宽度

标签 html css

我有以下代码,并希望使用类 canvas-wrap 使 Canvas 与我的 div 大小相同:

#overlay {
  position: absolute;
  top: 20px;
  width: 100%;
  height: 100%;
}

.canvas-wrap {
  position: absolute;
  top: 20px;
  width: 100%;
  height: 100%;
}
<div class="canvas-wrap">
  <div class="row" id="overlay">
  </div>
  <canvas id="myCanvas"></canvas>
</div>

不幸的是,目前它似乎默认为 150x300。如果它与类为 canvas-wrapdiv 大小相同会更好。

有人可以解释我做错了什么并修复错误吗?

最佳答案

您没有在 Canvas 本身上设置任何样式定义。

#overlay {
  position: absolute;
  top: 20px;
  width: 100%;
  bottom: 0;
}

.canvas-wrap {
  position: absolute;
  top: 20px;
  width: 100%;
  bottom: 0px;
  box-sizing: border-box;
}

.canvas-wrap canvas {
  border: 1px solid magenta;
  width: 100%;
  height: 100%;
  display: inline-block;
  box-sizing: border-box;
  
}

body, .canvas-wrap {
  padding: 0;
  margin: 0;
}
<div class="canvas-wrap">
  <div clas="row" id="overlay">
  </div>
  <canvas id="myCanvas"></canvas>
</div>

关于html - 将 Canvas 的高度和宽度设置为其包含的 div 的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347245/

相关文章:

php - 如何在一个wordpress主题中连接多个html页面?

html - div 上的 CSS 光标指针不起作用

css - 具有不同背景颜色的光滑幻灯片的盒子元素

JavaFx - 发现意外的 token LPAREN

html - 在 fullpage.js 中设置部分的高度

javascript - Ext js 重写 init 组件函数

javascript - HTML:了解居中 Div 的绝对位置

html - : hover IE 7 issue

html - 数字在 iPhone 上的 Safari 中看起来不同

javascript - 解析字符串中的样式属性