html - 为什么位置绝对div的底部有几个像素的空白边距

标签 html css scrollbar

我在 HTML 中有两个框

<div id="left" class="component">
</div>
<div id="game" class="component">
    <canvas id="game-canvas"></canvas>
</div>

左边一个,右边一个。这是我用于定位它们的 CSS:

.component {
    position: absolute;
    width: 50%;
    height: 100%;
    top: 0px;
}

#game {
    left: 50%;
    background-color: black;
}

#game-canvas {
    width: 100%;
    height: 100%;
}

#left {
  left: 0px;
  background-color: green;
  color: white;
}

我还使用以下 CSS 去除任何填充或边距

* {
    padding:0px;
    margin:0px;
}

这是一个 JSFiddle 链接:https://jsfiddle.net/odgo22yb/2/

由于某种原因,虽然有一个意外的垂直滚动条。

当您向下滚动时,两个框的底部会出现几个像素的空白/边距。

有谁知道为什么会这样,我该如何解决?

提前致谢。

最佳答案

滚动条是由 Canvas 引起的,解决这个问题需要添加display: block;

#game-canvas {
    width: 100%;
    height: 100%;
    display: block;
}

See jsfiddle

关于html - 为什么位置绝对div的底部有几个像素的空白边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388041/

相关文章:

html - 使容器 "clear"成为包含元素的 margin-top 而不会溢出 :hidden?

html - 无法弄清楚为什么每个跨度在动画期间重叠

iframe - 如何删除timeline.js iframe 上的滚动条?

css - 新 Google 文档 UI 中的滚动条是如何设计的(尤其是箭头按钮)?

html - 当我不知道它有多高时,如何从固定图像为 div 编写带纹理的、不重复的背景?

html - 如何在导航栏内的列内的Bootstrap 3中对齐图像中心?

javascript - Bootstrap 字形错误 : square instead of icon

HTML + CSS - 将图像发送到后台

html - 在 div 内水平居中绝对定位的图像,不显示水平滚动条(在移动设备中也是如此)

html - Flexbox - 滚动条移动居中内容