gpt4 book ai didi

html - 无法让 Canvas 显示在 html 后面

转载 作者:可可西里 更新时间:2023-11-01 13:26:30 24 4
gpt4 key购买 nike

我需要一个简单链接站点的帮助,我的 Canvas 不会位于我的文本后面

我认为问题出在我的 html 上

我并没有真正使用 html5 canvas 做很多事情,这是一个拙劣的复制粘贴工作:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<title>jetbrains.xyz</title>
<link href="main.css" rel="stylesheet">
<script src="main.js"></script>
</head>

<body>
<canvas id='c'></canvas>
<div class="mhm">
<div class="centered">



<h1>⎳⎳⎳</h1>



</div>

<ul class="bmenu">
<br />
<li>
<a href="http://steamcommunity.com/id/-sera/">sera</a>
</li>
<br />
<li>
<a href="">zonk</a>
</li>

</ul>
</div>



</body>

</html>

https://jsfiddle.net/83c7npck/

最佳答案

要更改 HTML 元素的堆叠顺序,您需要 z-index CSS property .将 z-index 视为元素深度或 Z 轴相对于 HTML 文档中其他元素的表达。 X 和 Y 轴表示元素的左/右和上/下值。

Z-index 允许您通过分配一个指示“高度”位置的数字来指定“堆叠顺序”。数字越大,该元素将堆叠的用户“越近”。比如说一堆 HTML 元素,每个元素都有一个这样的 z-index 值:

z-index: 1; 
z-index: 999;
z-index: -10;
z-index: 50;

堆叠顺序,从最远到最近,将是:

z-index: -10;
z-index: 1;
z-index: 50;
z-index: 999;

忠告

改变堆叠顺序意味着将元素物理地放置在其他元素之上。这意味着您正在模糊用户查看它们并与之交互的能力!在将元素放在其他元素之后之前仔细考虑,最好只保留视觉效果。

为什么它不起作用

您还没有对您的 <canvas> 应用任何 CSS元素,因此堆叠顺序将默认为元素类型的顺序。如果没有 CSS 干预,HTML 元素将永远不会重叠。

对于 canvas元素,这意味着 block 级。您可以阅读 how a canvas element will behave by default以及如何在 Mozilla 开发者网络上控制它。

如果您想更改元素的堆叠顺序,您需要对 CSS 进行一些更改:

canvas {
position: absolute; // this removes the element from the document flow and allows other elements to float over/under it
z-index:0; // alternately, we can simply make this '-1' to sit behind everything...
width: 100px; // just to make it clearly visible when it's working, very handy!
height:100px;
background-color: red;
}

div.mhm {
position:relative; // z-index only works if a position is set to relative or absolute
background-color: rgba(0,0,238,0.5);
z-index:10; // as long as this value is higher than that of the canvas' z-index, it will appear on top
}

关于html - 无法让 Canvas 显示在 html 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37369016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com