You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CSS背景动画配置异常:登录元素无法显示于Canvas上方及居中

问题解决方案

1. 让表单元素显示在Canvas上方

问题出在层级关系,Canvas默认层级比表单元素高,给包含标签和输入框的登录容器加定位和层级:

.login-container {
  position: relative;
  z-index: 1;
  /* 保留你原有的其他样式 */
}

同时给Canvas设置更低的层级:

canvas {
  position: absolute;
  z-index: 0;
  /* 保留你原有的宽高、动画等样式 */
}

2. 实现元素居中

两种简单方案任选:

  • Flex布局方案:给body加样式,让内部元素自动居中
body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
  • 绝对定位方案:直接给登录容器加定位
.login-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* 保留其他样式 */
}

3. 将Canvas作为登录div的背景

调整HTML结构,把Canvas和登录容器放在同一个父容器内,让Canvas作为背景层:

<div class="bg-wrapper">
  <canvas></canvas>
  <div class="login-container">
    <!-- 你的标签和输入框 -->
    <label>用户名</label>
    <input type="text">
    <label>密码</label>
    <input type="password">
  </div>
</div>

然后给父容器和元素加样式:

.bg-wrapper {
  position: relative;
  width: 100vw;
  height: 100vh;
}

canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.login-container {
  position: relative;
  z-index: 1;
  /* 可选:加个半透明背景让表单更清晰 */
  padding: 2rem;
  background: rgba(255,255,255,0.8);
  border-radius: 8px;
}

内容的提问来源于stack exchange,提问作者PHioNiX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:35:11