css - Canvas 忽略父大小

标签 css html canvas

我有一个在容器内显示 HTML canvas 元素的页面。由于 Canvas 中显示的图像可能远远大于我拥有的空间,我强制调整容器的大小并使用其溢出属性滚动 Canvas 的内容。虽然容器和滚动条都正确显示,但 Canvas 只是忽略了它。

我的 HTML:

<div class="sidebar">
   Some content
</div>
<div class="the_wrapper">
    <div class="canvas_wrapper">
       <canvas id="imageCanvas"></canvas>
    </div>
    <div class="another_element">
         Some other content
   </div>
</div>

这是 CSS:

 .sidebar{
    height: 100vh;
    width: 20vw;
    float: left;
 }

.the_wrapper{
    height: 100vh;
    width: 80vw;
    float: right;
}

.canvas_wrapper{
    height: 60vh;
    overflow: scroll;
}

.another_element{
    height: 40vh;
}

这就是我想要的 This is what I want

但我得到的只是这个

This is what I get

最佳答案

.the_wrapper 不需要右浮动。

 .the_wrapper{
    height: 100vh;
    width: 80vw;
    float: right; // Don't do that
}

就这样

.the_wrapper {
    position: absolute;
    right: 0;
    height: 100vh;
    width: 80vw;
}

我还重置了一些属性以获得第一个示例的结果。

* {
   box-sizing: border-box;
   padding: 0;
   margin: 0;
   overflow: hidden;
 }


这里的片段结果:

 * {
   box-sizing: border-box;
   padding: 0;
   margin: 0;
   overflow: hidden;
 }

.sidebar {
    height: 100vh;
    width: 20vw;
    float: left;
    border: 3px solid #000;
 }

.the_wrapper {
    position: absolute;
    right: 0;
    height: 100vh;
    width: 80vw;
}

.canvas_wrapper {
    height: 60vh;
    border: 3px solid green;
}

.another_element {
    height: 40vh;
    border: 3px solid red;
}
<div class="sidebar">Some content</div>
<div class="the_wrapper">
    <div class="canvas_wrapper">Canvas
       <canvas id="imageCanvas"></canvas>
    </div>
    <div class="another_element">Some other content</div>
</div>


您也可以在https://jsfiddle.net/lofeed/6n983g7x/4/中尝试

关于css - Canvas 忽略父大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37611296/

相关文章:

javascript - 如何获取 Canvas 上图像的坐标?

css - 无法弄清楚页面宽度增加的原因

jquery - 如何根据用户悬停在哪个 div 上来设置两个 div 的可见性?

html - 是否可以使 CSS 类相互扩展?

html - twitter bootstrap - 折叠时将对象移动到导航栏 bootstrap 3

html - 客户端 : HTML, DOM 和 CSS?

JavaScript,无法绘制乒乓球中的 Racket 和球。有人可以解释一下为什么吗?

javascript - 如何只绘制这部分圆弧?

css - 内容 : "" is working if is added in html style tag, 但不在 css 文件中

html - 文本不会垂直居中?