是什么导致我的画布元素溢出,我该怎么办才能修复它?
我在适当的帆布元素上尺寸尺寸有问题。如下所示的结果是一个帆布元素(绿色轮廓),该元素将其高度扩展到其父级之外。是什么导致了这个问题,我该怎么办才能解决?
html:这是HTML代码,没有太多代码。
<div id="container">
<div id="content">
<div id="menu">
...
</div>
<div id="canvas-container">
<canvas>
</canvas>
</div>
</div>
</div>
scss:更有趣的SCSS代码。
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
font-size: 16px;
font-family: 'Inter', sans-serif,
}
#container {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
#content {
outline: 2px solid rgb(0, 0, 0);
width: 80%;
height: 80%;
#menu {
padding: 1em;
border-bottom: 2px solid black;
select {
padding: 1em;
}
a {
background-color: rgb(91, 134, 195);
text-decoration: none;
font-weight: bold;
padding: 0.8em;
color: white;
}
a:hover {
background-color: rgb(75, 122, 189);
}
}
#canvas-container {
width: 100%;
height: 100%;
canvas {
outline: 2px solid green;
}
}
}
}
javaScript:以及最后的JS代码实际上可以完成帆布元素的大小。
var canvas = document.querySelector('canvas');
fitToContainer(canvas);
function fitToContainer(canvas){
// Make it visually fill the positioned parent
canvas.style.width ='100%';
canvas.style.height='100%';
// ...then set the internal size to match
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
}
I have a problem with sizing my canvas element properly. The result as seen below is a canvas element (green outline) that extends it's height outside of it's parent div. What is causing this problem and what can I do to fix it?
HTML: Here is the HTML code, there isn't much to it.
<div id="container">
<div id="content">
<div id="menu">
...
</div>
<div id="canvas-container">
<canvas>
</canvas>
</div>
</div>
</div>
SCSS: The more interesting SCSS code.
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
font-size: 16px;
font-family: 'Inter', sans-serif,
}
#container {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
#content {
outline: 2px solid rgb(0, 0, 0);
width: 80%;
height: 80%;
#menu {
padding: 1em;
border-bottom: 2px solid black;
select {
padding: 1em;
}
a {
background-color: rgb(91, 134, 195);
text-decoration: none;
font-weight: bold;
padding: 0.8em;
color: white;
}
a:hover {
background-color: rgb(75, 122, 189);
}
}
#canvas-container {
width: 100%;
height: 100%;
canvas {
outline: 2px solid green;
}
}
}
}
Javascript: And lastly the JS code that actually does the sizing of the Canvas element.
var canvas = document.querySelector('canvas');
fitToContainer(canvas);
function fitToContainer(canvas){
// Make it visually fill the positioned parent
canvas.style.width ='100%';
canvas.style.height='100%';
// ...then set the internal size to match
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每当您使用100%的高度,并且您希望根据父母的高度,就需要将父母的位置设置为相对。
另外,您的使用外观的布局将非常适合Flexbox布局。
最后,如果您希望这会做出响应速度,则您将要附加一个调整大小事件以设置画布大小。
以下是使用这些技术的示例,我还提供了一个页脚部分,Flexbox使这些布局变得轻而易举。提示:运行摘要全屏,并调整窗口大小以查看调整大小的操作。
Whenever you use a height of 100% and you want the height based on it's parent, you need to set the parents position to relative.
Also the layout your using looks like it will fit into a flexbox layout nicely.
Lastly, if you want this to be responsive, your going to want to attach a resize event for setting the canvas size.
Below is an example of using these techniques, I've also included a footer section, flexbox makes these layout a breeze. hint: run the snippet fullscreen, and resize the window to see the resizing in action.
尝试以下操作:
Try this: