HTML 画布将 DIV 对齐在右下角



我有一个画布,想在右下角添加一个相对于画布的 Div/按钮。我当前的代码如下所示:

#container {
    margin-top: 5px;
    width: 96%;
    margin: auto;
    background-color: blue;
    height: 300px;
}
#viewer {
    width: 40%;
    background-color: red;
    height: 100%;
}
#button {
    height: 40px;
    width: 40px;
    background-color: green;
    position: absolute;
    bottom: 0;
    right: 0;
}
canvas {
    border: 3px solid black; 
    width: 100%;
    height: 100%;
}
<div id="container">
    <div id="viewer">
        <canvas></canvas>
        <div id="button" onclick="myFunction();"></div>
    </div>
</div>

但到目前为止,我无法为此找到合适的解决方案。如果有人能帮我解决这个问题,那就太好了。

这是你要找的吗?
我命令你查看这篇文章: CSS 布局 - 位置属性

#container{
    margin-top: 5px;
    width: 96%;
    margin: auto;
    background-color: blue;
    height: 300px;
}
#viewer{
    width: 40%;
    background-color: red;
    height: 100%;
    position:relative;
}
#button{
    height: 40px;
    width: 40px;
    background-color: green;
    position: absolute;
    bottom: 0;
    right: 0;
}
canvas{
    border: 3px solid black; 
    width: 100%;
    height: 100%;
}
<div id="container">
    <div id="viewer">
       <canvas></canvas>
       <div id="button" onclick="myFunction();"></div>
    </div>
</div>

我认为 Temani 是对的——这会把按钮放在画布的右下角,这不是你想要的吗?

#container{
	margin-top: 5px;
	width: 96%;
	margin: auto;
	background-color: blue;
	height: 300px;
}
#viewer{
	width: 40%;
	background-color: red;
	height: 100%;
    position: relative;
}
#button{
	height: 40px;
	width: 40px;
	background-color: green;
	position: absolute;
  bottom: 0;
  right: 0;
}
canvas{
border: 3px solid black; 
width: 100%;
height: 100%;
}
<div id="container">
<div id="viewer">
<canvas></canvas>
<div id="button" onclick="myFunction();"></div>
</div>
</div>

就像Temani Afif说的那样。

position:relative

#viewer 为我工作。

最新更新