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

首次建站遇.Main div背景色不显示问题求助

问题排查:.Main div高度为0导致背景色不显示

问题原因

你的.Main容器高度为0是因为内部的.banner使用了position: fixed——fixed定位会让元素完全脱离正常文档流,不再占据父容器的空间;另外.cursor是position: absolute,同样脱离文档流。这就导致.Main没有任何“实际占据空间”的子元素,自然高度塌陷为0,橙色背景也就看不到了。

解决方案

给.Main设置最小高度为视口高度,让它撑满整个页面,这样就能显示出橙色背景。修改CSS中.Main的样式:

.Main{
    cursor: none;
    background-color: orange;
    min-height: 100vh; /* 添加这行,让容器高度至少铺满视口 */
    width: 100%; /* 确保宽度铺满页面 */
}

如果不需要.banner的fixed定位,也可以改用flex布局让.Main作为容器居中内容,避免文档流脱离问题,但上述方法是最直接的修复方案。

修改后的完整代码示例

HTML

<div class="Main">
    <div class="banner">
        <h2 class="title">Thank you!</h2>
        <p class="subheading">I really appreciate you contacting me. I will be in touch shortly.</p>
        <svg class="smiley" xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" viewBox="0 0 16 16">
            <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zM7 6.5C7 7.328 6.552 8 6 8s-1-.672-1-1.5S5.448 5 6 5s1 .672 1 1.5zM4.285 9.567a.5.5 0 0 1 .683.183A3.498 3.498 0 0 0 8 11.5a3.498 3.498 0 0 0 3.032-1.75.5.5 0 1 1 .866.5A4.498 4.498 0 0 1 8 12.5a4.498 4.498 0 0 1-3.898-2.25.5.5 0 0 1 .183-.683zM10 8c-.552 0-1-.672-1-1.5S9.448 5 10 5s1 .672 1 1.5S10.552 8 10 8z"/>
        </svg>
    </div>
    <div class="cursor"></div>
</div>

CSS

body {
    font-family: 'Montserrat', sans-serif;
    overflow-x: hidden;
    width: 100%;
    margin: 0; /* 移除默认边距,避免不必要的滚动条 */
}

.Main{
    cursor: none;
    background-color: orange;
    min-height: 100vh;
    width: 100%;
}

.cursor{ 
    position: absolute;  
    width: 50px;
    height: 50px;
    top: 0;
    left: 0; 
    background-color: #84E6BC;
    border-radius: 50%;
    pointer-events: none;
} 

.cursor.hide {
    opacity: 0;
    pointer-events: none;
}

.cursor-text {
    cursor: text;
}

.banner {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 60%;
    height: 40%;
    background-color: #FFFFFF;
    border-radius: 60px;
    box-shadow: 0px 6px 20px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
  
.title {
    font-size: 2em;
    font-weight: bold;
    text-align: center;
    margin-bottom: 40px;
}
  
.subheading {
    font-size: var(--p-size);
    text-align: center;
    margin-bottom: 40px;
}
  
.smiley {
    fill: #84E6BC;
}

JavaScript

const cursor = document.querySelector('.cursor')
const main = document.querySelector('.Main');

let cursorSize = 50;

document.addEventListener('mousemove', (event) => {
  
    cursor.style.left = `${event.clientX - cursorSize / 2}px`;
    cursor.style.top = `${event.clientY - cursorSize / 2}px`;

    const isSelectable = event.target.tagName === 'P' || event.target.tagName === 'H2';
    if (isSelectable) {
        cursor.classList.add('hide');
        main.classList.add('cursor-text');
    } else {
        cursor.classList.remove('hide');
        main.classList.remove('cursor-text');
    }
});

document.addEventListener('mousedown', (event) => {
    cursorSize = 20;
    pointerStyle(event, cursorSize)
});
  
document.addEventListener('mouseup', (event) => {
    cursorSize = 50;
    pointerStyle(event, cursorSize)
});

let pointerStyle = (event, size) => {
    let newX = event.clientX - size / 2;
    let newY = event.clientY - size / 2;
    
    cursor.style.width = `${size}px`;
    cursor.style.height = `${size}px`;
    cursor.style.left = `${newX}px`;
    cursor.style.top = `${newY}px`;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 00:07:00