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

VSCode使用Live Server打开页面后div元素不显示该如何解决?

问题原因

  • 百分比高度依赖父元素的明确高度设置:你给.container设置的height: 100%是相对其父元素body的高度,而body的高度默认由内容撑开,在html和body都没有设置明确高度的前提下,.container的高度最终会计算为0,自然看不到任何内容。
  • viewport元标签存在语法错误:你把width=device-width的属性值拆分写到了content属性外面,会导致移动端视口配置失效。

修复方案

给html和body添加100%高度,同时修正viewport标签的语法错误即可。如果需要容器完全铺满窗口,还可以去掉body的默认边距。

修正后的完整代码

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 新增html和body的高度设置 */
        html, body {
            height: 100%;
            margin: 0; /* 可选,去掉body默认边距,让容器铺满窗口 */
        }
        .container{
            background: #81072b;
            width: 100%;
            height: 100%;
        }
        .child{
            background: #05A82C;
            width: 50%;
            height: 50%;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="child"></div>
    </div>
</body>
</html>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 14:00:01